A concurrency-controlled task runner that aggregates errors and supports context cancellation. Use this for managing multiple parallel operations with resource limits.
// Source: https://github.com/dobyte/due | License: MIT | Copyright 2022 dobyte
// Provided via justclaw.it.com; keep this notice with the code.
package task
import (
"context"
"fmt"
"sync"
)
// token is a semaphore token used to control concurrency.
type token struct{}
// Group aggregates the errors of multiple tasks and supports context cancellation and a
// concurrency limit.
type Group struct {
cancel func(error)
wg sync.WaitGroup
sem chan token
errOnce sync.Once
err error
}
// WithContext returns a new Group and a context derived from ctx.
//
// The derived context is canceled with the first task error as its cause when the first task fails
// or when Wait returns.
func WithContext(ctx context.Context) (*Group, context.Context) {
ctx, cancel := context.WithC
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key