A simple and useful goroutine concurrent library.

Taskgroup

A simple and useful goroutine concurrent library.

Installation

go get github.com/anthhub/taskgroup

Usage

	count := 100
	// create a taskgroup
	// set max error count to 1
	g := New(&Option{MaxErrorCount:1})

	for i := 0; i < count; i++ {
		g.Go(func() (interface{}, error) {
			// your work function
			return worker()
		})
	}

	// g.Fed() declare the end of tasks producing
	// 
	// g.Result() will receive a message when a task of the group return an error or
	// till all tasks finish.
	if p := <-g.Fed().Result(); p.Err != nil {
		return
	}
	// ...

Advanced Usage

Useful Options

	count := 100
	// configure group timeout
	ctx, _ := context.WithTimeout(context.Background(), time.Second)
	// create a taskgroup with context
	g, ctx = WithContext(ctx, &Option{
		// limit the count of goroutine workers; default is infinity
		Limit: 5,
		// set max error count to 5; default is infinity
		MaxErrorCount: 5,
		// disable recover panic; default will recover panic
		DisableRecover: true,
	})

	// the loop need be wrapped by a goroutine when the limit less than your tasks count,
	// else dead lock will be created.
	go func() {
		for i := 0; i < count; i++ {
			g.Go(func() (interface{}, error) {
				// your work function that return data and error
				// panic will be recover and just return a error
				return worker(ctx)
			})
		}

		// it is to tell consumer that consuming the all tasks and then close itself.
		// 
		// it is very important else the consuming never end without g.Fed() when all 
		// tasks have finished.
		g.Fed()
	}()

	// you can directly for-range g.Result(), it will break the loop when all tasks id finished
	// or error count is to 5.
	for p := range g.Result() {
		if p.Err != nil {
			glog.Errorf("error")
		}
		// you can cancel the group and break the loop in advanced when you want.
		if [condition] {
			g.Cancel()
			break
		}
		data := p.Data
		// consume data from worker
		consume(p.Data)
		// ...
	}
	// ...

Producer & Consumer Mode

	func main() {
		g := provider()

		consumer(g)

		// some logic ...
		delay(10)

		// cancel provider and consumer
		g.Cancel()
	}

	func provider() Group {
		g := New(&Option{MaxErrorCount: 1})

		go func() {
			for {
				select {
				case <-g.Ctx().Done():
					return
				default:
					g.Go(func() (interface{}, error) {
						// get group inner ctx
						return worker(g.Ctx())
					})
				}
				delay(1)
			}
		}()

		return g
	}

	func consumer(g Group) {	
		go func() {	
			for p := range g.Result() {	
				// it is just consuming all message from provider till provider want to stop, so	
				// g.Fed() is not necessary	
				if p.Err != nil {
					continue
				}
				// consume data from provider
				consume(p.Data)
			}	
		}()
	}

If you want to learn more about taskgroup, you can read test cases and source code.

Similar Resources

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

Waiting group for collecting goroutine information.

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

Dec 3, 2021

A universal mechanism to manage goroutine lifecycles

A universal mechanism to manage goroutine lifecycles

Dec 29, 2022

goroutine pool in golang

goroutine pool in golang

Nov 1, 2021

Goroutine Leak Detector

Leaktest Refactored, tested variant of the goroutine leak detector found in both net/http tests and the cockroachdb source tree. Takes a snapshot of r

Dec 26, 2022

A lib for monitoring runtime goroutine stack

Overview A lib for monitoring runtime goroutine stack. Such as wait for goroutines to exit, leak detection, etc. Features context.Context first design

Oct 31, 2022

go routine control, abstraction of the Main and some useful Executors.如果你不会管理Goroutine的话,用它

go routine control, abstraction of the Main and some useful Executors.如果你不会管理Goroutine的话,用它

routine Routine Architecture Quick Start package main import ( "log" "context" "github.com/x-mod/routine" ) func main(){ if err := routine.Main

Dec 6, 2022

Unlimited job queue for go, using a pool of concurrent workers processing the job queue entries

kyoo: A Go library providing an unlimited job queue and concurrent worker pools About kyoo is the phonetic transcription of the word queue. It provide

Dec 21, 2022
Related tags
🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。
🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。

A goroutine pool for Go English | ???? 中文 ?? Introduction Library ants implements a goroutine pool with fixed capacity, managing and recycling a massi

Jan 2, 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 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
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
🐝 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
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
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
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
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
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