Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fatih/semgroup
Like errgroup/waitgroup, but only runs a maximum of tasks at any time.
https://github.com/fatih/semgroup
concurrency errgroup go golang semaphore waitgroup
Last synced: 21 days ago
JSON representation
Like errgroup/waitgroup, but only runs a maximum of tasks at any time.
- Host: GitHub
- URL: https://github.com/fatih/semgroup
- Owner: fatih
- License: bsd-3-clause
- Created: 2022-03-04T11:27:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-13T12:33:40.000Z (6 months ago)
- Last Synced: 2024-08-04T01:10:14.274Z (3 months ago)
- Topics: concurrency, errgroup, go, golang, semaphore, waitgroup
- Language: Go
- Homepage: https://pkg.go.dev/github.com/fatih/semgroup
- Size: 15.6 KB
- Stars: 292
- Watchers: 5
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# semgroup [![](https://github.com/fatih/semgroup/workflows/build/badge.svg)](https://github.com/fatih/semgroup/actions) [![PkgGoDev](https://pkg.go.dev/badge/github.com/fatih/semgroup)](https://pkg.go.dev/github.com/fatih/semgroup)
semgroup provides synchronization and error propagation, for groups of goroutines working on subtasks of a common task. It uses a weighted semaphore implementation to make sure that only a number of maximum tasks can be run at any time.
Unlike [golang.org/x/sync/errgroup](https://pkg.go.dev/golang.org/x/sync/errgroup), it doesn't return the first non-nil error, rather it accumulates all errors and returns a set of errors, allowing each task to fullfil their task.
# Install
```bash
go get github.com/fatih/semgroup
```# Example
With no errors:
```go
package mainimport (
"context"
"fmt""github.com/fatih/semgroup"
)func main() {
const maxWorkers = 2
s := semgroup.NewGroup(context.Background(), maxWorkers)visitors := []int{5, 2, 10, 8, 9, 3, 1}
for _, v := range visitors {
v := vs.Go(func() error {
fmt.Println("Visits: ", v)
return nil
})
}// Wait for all visits to complete. Any errors are accumulated.
if err := s.Wait(); err != nil {
fmt.Println(err)
}// Output:
// Visits: 2
// Visits: 10
// Visits: 8
// Visits: 9
// Visits: 3
// Visits: 1
// Visits: 5
}
```With errors:
```go
package mainimport (
"context"
"errors"
"fmt""github.com/fatih/semgroup"
)func main() {
const maxWorkers = 2
s := semgroup.NewGroup(context.Background(), maxWorkers)visitors := []int{1, 1, 1, 1, 2, 2, 1, 1, 2}
for _, v := range visitors {
v := vs.Go(func() error {
if v != 1 {
return errors.New("only one visitor is allowed")
}
return nil
})
}// Wait for all visits to complete. Any errors are accumulated.
if err := s.Wait(); err != nil {
fmt.Println(err)
}// Output:
// 3 error(s) occurred:
// * only one visitor is allowed
// * only one visitor is allowed
// * only one visitor is allowed
}
```