Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jabolopes/go-cancelgroup
A WaitGroup with support for group cancellation and context cancellation.
https://github.com/jabolopes/go-cancelgroup
go go-library golang golang-library synchronization utility waitgroup waitgroups
Last synced: 9 days ago
JSON representation
A WaitGroup with support for group cancellation and context cancellation.
- Host: GitHub
- URL: https://github.com/jabolopes/go-cancelgroup
- Owner: jabolopes
- License: bsd-3-clause
- Created: 2021-01-12T20:13:07.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-20T22:37:43.000Z (over 2 years ago)
- Last Synced: 2024-06-20T14:17:45.592Z (6 months ago)
- Topics: go, go-library, golang, golang-library, synchronization, utility, waitgroup, waitgroups
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-cancelgroup
[![PkgGoDev](https://pkg.go.dev/badge/github.com/jabolopes/go-cancelgroup)](https://pkg.go.dev/github.com/jabolopes/go-cancelgroup)
This package provides `cancelgroup.Group`, which is a `sync.WaitGroup` with
support for group cancellation and context cancellation. When the context is
cancelled, the `cancelgroup.Group` is also cancelled and attempting to further
schedule goroutines on this group is a no-op.## Installation
```sh
$ go get github.com/jabolopes/go-cancelgroup
```You can use `go get -u` to update the package. If you are using Go modules, you
can also just import the package and it will be automatically downloaded on the
first compilation.## Examples
Typical usage of a `cancelgroup.Group` with group cancellation:
```go
g := cancelgroup.New(ctx)// Schedule some goroutines.
for i := 0; i < 10; i++ {
g.Go(func(ctx context.Context) { ... })
}// Do something else...
// Cancel group.
g.Cancel()// Wait for active goroutines to finish.
g.Wait()
```Example of a usage of a `cancelgroup.Group` with Context cancellation:
```go
// Create a context with cancellation. Can also be a `context.WithDeadline`, a `context.WithTimeout`, etc.
ctx, cancel := context.WithCancel(ctx)g := cancelgroup.New(ctx)
// Schedule some goroutines.
for i := 0; i < 10; i++ {
g.Go(func(ctx context.Context) { ... })
}// Cancel the context.
cancel()// Scheduling goroutines on the already cancelled group is a no-op
g.Go(func(ctx context.Context) { ... })// Wait for active goroutines to finish.
g.Wait()
```