A Go module for managing concurrent tasks with automatic cancellation when any task completes or fails.
rungroup
is a Go module that provides a way to manage and synchronize
concurrent tasks. Its primary feature is the ability to cancel all goroutines
when any one of them completes. This package is particularly useful for
scenarios where you need to run multiple operations concurrently but want to
stop all of them as soon as any one operation completes or fails.
- Manage multiple goroutines as a group
- Automatic cancellation of all tasks when one completes
- Support for timeouts
- Easy-to-use API
To install the rungroup
module, use go get
:
go get github.com/goaux/rungroup
Here's a basic example of how to use the rungroup
module:
package main
import (
"context"
"fmt"
"time"
"github.com/goaux/rungroup"
"github.com/goaux/timer"
)
func main() {
// Create a new group with no timeout
rg := rungroup.New(context.Background())
// Add tasks to the group
rg.Go(func(ctx context.Context) error {
if err := timer.Sleep(ctx, 150*time.Millisecond); err != nil {
fmt.Println("task1 canceled")
return err
}
fmt.Println("task1 done")
return nil
})
rg.Go(func(ctx context.Context) error {
if err := timer.Sleep(ctx, 300*time.Millisecond); err != nil {
fmt.Println("task2 canceled")
return err
}
fmt.Println("task2 done")
return nil
})
// Wait for all tasks to complete or be canceled
err := rg.Wait()
if err != nil {
fmt.Printf("must not error: %v\n", err)
} else {
fmt.Println("ok")
}
}
In this example, the first task completes after 150 milliseconds, which
triggers the cancellation of the second task. The Wait
method returns when
all tasks have either completed or been canceled.
Creates a new Group
with the given context.
Creates a new Group
with the given context and timeout duration.
Starts a new goroutine in the Group
.
Blocks until all tasks in the Group
have completed or been canceled.
Explicitly cancels the Group
's context, causing all tasks to be interrupted.
- A
Group
must be created byNew
orNewTimeout
, zero value must not be used. - A
Group
must not be copied after first use. - A
Group
must not be reused after callingWait
.