Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/remeh/sizedwaitgroup
SizedWaitGroup has the same role and close to the same API as the Golang sync.WaitGroup but it adds a limit on the amount of goroutines started concurrently.
https://github.com/remeh/sizedwaitgroup
Last synced: 4 days ago
JSON representation
SizedWaitGroup has the same role and close to the same API as the Golang sync.WaitGroup but it adds a limit on the amount of goroutines started concurrently.
- Host: GitHub
- URL: https://github.com/remeh/sizedwaitgroup
- Owner: remeh
- License: mit
- Created: 2016-11-19T13:49:25.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-03-17T13:12:43.000Z (almost 3 years ago)
- Last Synced: 2025-01-03T16:06:21.957Z (11 days ago)
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 419
- Watchers: 7
- Forks: 47
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - SizedWaitGroup - concurrency control (Open source library / Concurrency)
README
# SizedWaitGroup
[![GoDoc](https://godoc.org/github.com/remeh/sizedwaitgroup?status.svg)](https://godoc.org/github.com/remeh/sizedwaitgroup)
`SizedWaitGroup` has the same role and API as `sync.WaitGroup` but it adds a limit of the amount of goroutines started concurrently.
`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.
# Example
```go
package mainimport (
"fmt"
"math/rand"
"time""github.com/remeh/sizedwaitgroup"
)func main() {
rand.Seed(time.Now().UnixNano())// Typical use-case:
// 50 queries must be executed as quick as possible
// but without overloading the database, so only
// 8 routines should be started concurrently.
swg := sizedwaitgroup.New(8)
for i := 0; i < 50; i++ {
swg.Add()
go func(i int) {
defer swg.Done()
query(i)
}(i)
}swg.Wait()
}func query(i int) {
fmt.Println(i)
ms := i + 500 + rand.Intn(500)
time.Sleep(time.Duration(ms) * time.Millisecond)
}
```# License
MIT
# Copyright
Rémy Mathieu © 2016