Skip to content

A WaitGroup with support for group cancellation and context cancellation.

License

Notifications You must be signed in to change notification settings

jabolopes/go-cancelgroup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-cancelgroup

PkgGoDev

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

$ 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:

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:

// 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()

Releases

No releases published

Packages

No packages published

Languages