Documentation ¶
Overview ¶
Package throttlegroup is a variation of the errgroup package (golang/x/sync/errgroup) that uses throttling to control the active number of goroutines at the same time. This is useful while running CPU and/or memory intensive code concurrently (e.g.: if you launch too many goroutines, you will end up of resources).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
Group is a throttle group. The zero value should not be used.
Example ¶
package main import ( "fmt" "time" "github.com/andreynering/goext/syncext/throttlegroup" ) func main() { g := throttlegroup.WithThrottle(2) for i := 0; i < 10; i++ { i := i g.Go(func() error { fmt.Println(i) time.Sleep(time.Second) return nil }) } if err := g.Wait(); err != nil { panic(err) } }
Output:
func Default ¶
func Default() *Group
Default returns a throttle group sized by the number of CPU cores of the device.
func DefaultWithContext ¶
DefaultWithContext returns a throttle group and a context, given a parent context. The size of the group will be the number of the CPU cores of the device.
func WithContext ¶
WithContext returns a throttle group and a context, given a parent context and a throttle size.
func WithThrottle ¶
New returns a throttle group sized by the given number. E.g.: throttle of 4 means 4 active goroutines at the same time.