Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
LICENSE | H A D | 22-Aug-2018 | 1.1 KiB | |||
README.md | H A D | 22-Aug-2018 | 1.3 KiB | |||
sizedwaitgroup.go | H A D | 22-Aug-2018 | 2.3 KiB | |||
sizedwaitgroup_test.go | H A D | 22-Aug-2018 | 1.4 KiB |
README.md
1# SizedWaitGroup 2 3[![GoDoc](https://godoc.org/github.com/remeh/sizedwaitgroup?status.svg)](https://godoc.org/github.com/remeh/sizedwaitgroup) 4 5`SizedWaitGroup` has the same role and API as `sync.WaitGroup` but it adds a limit of the amount of goroutines started concurrently. 6 7`SizedWaitGroup` adds the feature of limiting the maximum number of concurrently started routines. It could for example be used to start multiples routines querying a database but without sending too much queries in order to not overload the given database. 8 9# Example 10 11``` 12package main 13 14import ( 15 "fmt" 16 "math/rand" 17 "time" 18 19 "github.com/remeh/sizedwaitgroup" 20) 21 22func main() { 23 rand.Seed(time.Now().UnixNano()) 24 25 // Typical use-case: 26 // 50 queries must be executed as quick as possible 27 // but without overloading the database, so only 28 // 8 routines should be started concurrently. 29 swg := sizedwaitgroup.New(8) 30 for i := 0; i < 50; i++ { 31 swg.Add() 32 go func(i int) { 33 defer swg.Done() 34 query(i) 35 }(i) 36 } 37 38 swg.Wait() 39} 40 41func query(i int) { 42 fmt.Println(i) 43 ms := i + 500 + rand.Intn(500) 44 time.Sleep(time.Duration(ms) * time.Millisecond) 45} 46``` 47 48# License 49 50MIT 51 52# Copyright 53 54Rémy Mathieu © 2016 55