goroutine pool in golang

a goroutine pool in golang

install

go get github.com/wksw/go-pool

use

package main

import (
	"fmt"
	"time"

	gopool "github.com/wksw/go-pool"
)

type job struct {
	Name string
}

var _ gopool.JobHandler = &job{}

// Handle job handler
func (j *job) Handle() (interface{}, error) {
	fmt.Println("job", j.Name, "handle")
	time.Sleep(100 * time.Millisecond)
	// panic("---abc")
	return nil, nil
}

func main() {
	pool := gopool.NewPool(100, 4).
		WithExitCallback(func(reason string) {
			fmt.Println("pool exit because", reason)
		}).
		WithPanicCallback(func(r interface{}) {
			fmt.Println("panic", r)
		}).
		WithEventCallback(gopool.EventLevelDebug, func(event *gopool.Event) {
			fmt.Println(event)
		})

	for i := 0; i < 10; i++ {
		pool.AddJob(gopool.NewJob("job", &job{Name: fmt.Sprintf("job-%d", i)}))
		fmt.Println("job ", i, "added")
	}
	pool.Close("finish")

}

use with pipeline

B ", err.Error()) } if err := jobC.When(func(self *gopool.Job) bool { for _, job := range self.GetUpstreams() { if job.GetStatus() != gopool.JobSuccess { return false } } return true }).After(jobA); err != nil { log.Fatal("A -> C ", err.Error()) } if err := jobE.When(func(self *gopool.Job) bool { for _, job := range self.GetUpstreams() { if job.GetStatus() != gopool.JobSuccess { return false } } return true }).After(jobB, jobC); err != nil { log.Fatal("A, B, C, D -> E ", err.Error()) } if err := jobD.After(jobA, jobB, jobC, jobE); err != nil { log.Fatal("A, B, C, E->D ", err.Error()) } pool := gopool.NewPool(3, 5). WithEventCallback(gopool.EventLevelDebug, func(event *gopool.Event) { // fmt.Println(event) }). WithPanicCallback(func(r interface{}) { fmt.Println("panic", r) }). WithExitCallback(func(reason string) { fmt.Println("pool exist beacuse", reason) }) pipeline, err := gopool.NewPipeline("pipeline", jobA, jobB, jobC, jobD) if err != nil { log.Fatal(err.Error()) } pool.AddPipeline(pipeline) // 起床 1 秒 // 洗脸 2 秒 // 刷牙 3 秒 // 上班 1 秒 // 先起床, 然后同时洗脸刷牙,按刷牙最大耗时计算, 共 3+1=4秒 // 如果在4秒钟之前pool exit 则出不了门 // 否则没穿衣服就出门了 time.Sleep(5 * time.Second) pool.Close("没穿衣服") } ">
package main

import (
	"fmt"
	"log"
	"time"

	gopool "github.com/wksw/go-pool"
)

type jobA struct {
	Name string
	A    time.Duration
}

type jobB struct {
	Name string
	B    int
}

type jobC struct {
	Name string
	C    int64
}

type jobD struct {
	Name string
	D    string
}

type jobE struct {
	Name string
	E    string
}

var _ gopool.JobHandler = &jobA{}
var _ gopool.JobHandler = &jobB{}
var _ gopool.JobHandler = &jobC{}
var _ gopool.JobHandler = &jobD{}
var _ gopool.JobHandler = &jobE{}

func (j *jobA) Handle() (interface{}, error) {
	time.Sleep(j.A)
	fmt.Println(j.Name, "花了", j.A.Seconds(), "秒")
	return nil, nil
}

func (j *jobB) Handle() (interface{}, error) {
	time.Sleep(time.Duration(j.B) * time.Second)
	fmt.Println(j.Name, "花了", j.B, "秒")
	return nil, nil
}

func (j *jobC) Handle() (interface{}, error) {
	time.Sleep(time.Duration(j.C) * time.Second)
	fmt.Println(" ", j.Name, "花了", j.C, "秒")
	return nil, nil
}

func (j *jobD) Handle() (interface{}, error) {
	fmt.Println(j.Name, j.D)
	return nil, nil
}

func (j *jobE) Handle() (interface{}, error) {
	time.Sleep(time.Second)
	fmt.Println(j.Name, j.E)
	return nil, nil
}

func main() {

	jobA := gopool.NewJob("起床", &jobA{Name: "起床", A: time.Second})
	jobB := gopool.NewJob("洗脸", &jobB{Name: "洗脸", B: 2})
	jobC := gopool.NewJob("刷牙", &jobC{Name: "刷牙", C: 3})
	jobD := gopool.NewJob("深呼吸", &jobD{Name: "深呼吸", D: "一大口"})
	jobE := gopool.NewJob("上班", &jobE{Name: "上班", E: ""})

	if err := jobB.When(func(self *gopool.Job) bool {
		for _, job := range self.GetUpstreams() {
			if job.GetStatus() != gopool.JobSuccess {
				return false
			}
		}
		return true
	}).After(jobA); err != nil {
		log.Fatal("A -> B ", err.Error())
	}

	if err := jobC.When(func(self *gopool.Job) bool {
		for _, job := range self.GetUpstreams() {
			if job.GetStatus() != gopool.JobSuccess {
				return false
			}
		}
		return true
	}).After(jobA); err != nil {
		log.Fatal("A -> C ", err.Error())
	}

	if err := jobE.When(func(self *gopool.Job) bool {
		for _, job := range self.GetUpstreams() {
			if job.GetStatus() != gopool.JobSuccess {
				return false
			}
		}
		return true
	}).After(jobB, jobC); err != nil {
		log.Fatal("A, B, C, D -> E ", err.Error())
	}

	if err := jobD.After(jobA, jobB, jobC, jobE); err != nil {
		log.Fatal("A, B, C, E->D ", err.Error())
	}

	pool := gopool.NewPool(3, 5).
		WithEventCallback(gopool.EventLevelDebug, func(event *gopool.Event) {
			// fmt.Println(event)
		}).
		WithPanicCallback(func(r interface{}) {
			fmt.Println("panic", r)
		}).
		WithExitCallback(func(reason string) {
			fmt.Println("pool exist beacuse", reason)
		})

	pipeline, err := gopool.NewPipeline("pipeline", jobA, jobB, jobC, jobD)
	if err != nil {
		log.Fatal(err.Error())
	}

	pool.AddPipeline(pipeline)

	// 起床 1 秒
	// 洗脸 2 秒
	// 刷牙 3 秒
	// 上班 1 秒
	// 先起床, 然后同时洗脸刷牙,按刷牙最大耗时计算, 共 3+1=4秒
	// 如果在4秒钟之前pool exit 则出不了门
	// 否则没穿衣服就出门了
	time.Sleep(5 * time.Second)
	pool.Close("没穿衣服")
}
Owner
wksw
love cloud compute
wksw
Similar Resources

Go-miningcore-pool - Miningcore Pool written in GOlang

Go-Miningcore-Pool (COMING SOON) Miningcore Pool written in GOlang 0x01 Configur

Apr 24, 2022

Go-ldap-pool - A simple connection pool for go-ldap

Basic connection pool for go-ldap This little library use the go-ldap library an

Dec 17, 2022

Work pool channlege - An url hash retriever worker pool for getting hash digest for a collection of urls

Code challenge The aim of this project is to provide an url hash retriever worke

Feb 16, 2022

errgroup with goroutine worker limits

neilotoole/errgroup neilotoole/errgroup is a drop-in alternative to Go's wonderful sync/errgroup but limited to N goroutines. This is useful for inter

Dec 15, 2022

Waiting group for collecting goroutine information.

在go语言waitGroup和errGroup都是用来控制goroutine的并发的方式,前者只能等待所有goroutine执行完成之后再执行Wait()函数后面的代码并且不

Dec 3, 2021

A simple and useful goroutine concurrent library.

Taskgroup A simple and useful goroutine concurrent library. Installation go get github.com/anthhub/taskgroup

May 19, 2021

Provides some convenient API, includes Goid(), AllGoid(), and LocalStorage, which is a goroutine's local storage, just like ThreadLocal in other languages.

routine 中文版 routine encapsulates and provides some easy-to-use, high-performance goroutine context access interfaces, which can help you access corout

Dec 30, 2022

A universal mechanism to manage goroutine lifecycles

A universal mechanism to manage goroutine lifecycles

Dec 29, 2022

A cross goroutine storage tool with very simple implementation and function.

Simple-goroutine-local is a cross goroutine storage tool with very simple implementation and function (the concept is similar to Java ThreadLocal). Ge

Jan 13, 2022
Related tags
golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

Dec 19, 2022
Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 9, 2023
Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 2, 2023
goroutine pool in golang

goroutine pool in golang

Nov 1, 2021
🐝 A Highly Performant and easy to use goroutine pool for Go
🐝 A Highly Performant and easy to use goroutine pool for Go

gohive Package gohive implements a simple and easy to use goroutine pool for Go Features Pool can be created with a specific size as per the requireme

Sep 26, 2022
Lightweight Goroutine pool

grpool Lightweight Goroutine pool Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with proc

Dec 6, 2022
Minimalistic and High-performance goroutine worker pool written in Go

pond Minimalistic and High-performance goroutine worker pool written in Go Motivation This library is meant to provide a simple way to limit concurren

Dec 22, 2022
A goroutine pool for Go
A goroutine pool for Go

Tunny is a Golang library for spawning and managing a goroutine pool, allowing you to limit work coming from any number of goroutines with a synchrono

Dec 31, 2022
Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Dec 28, 2022
Golang Implementation of Worker Pool/ Thread Pool

Golang Implementation of Worker Pool/ Thread Pool

Jun 18, 2022