go.bigb.es/auxilia
Index
- func ContextID(ctx context.Context) int
- func RunningCount() int
- func SelectSlice(ctx context.Context, inp []<-chan T) (int, T, bool)
- func SelectSliceBi(ctx context.Context, inp []chan T) (int, T, bool)
- func WithContextID(ctx context.Context, id int) context.Context
- type ErrFunc
- type Func
- type Group
- func NewGroup() *Group[T]
- func NewGroupFixed(executors ...Func[T]) *Group[T]
- func NewGroupRepeated(executor Func[T], count int) *Group[T]
- func (*Group[T]) AddExecutor(executor Func[T])
- func (*Group[T]) All(ctx context.Context) error
- func (*Group[T]) Any(ctx context.Context) error
- func (*Group[T]) FirstError() error
- func (*Group[T]) Start(ctx context.Context) error
- func (*Group[T]) Stop(ctx context.Context) error
- type Registry
- type Task
- func NewDummy(ctx context.Context) *Task[T]
- func Start(ctx context.Context, executor Func[T]) *Task[T]
- func StartWith(ctx context.Context, registry *Registry, executor Func[T], tags ...string) *Task[T]
- func (*Task[T]) C() <-chan struct{}
- func (*Task[T]) Cancel()
- func (*Task[T]) CancelAndWait(ctx context.Context) (T, bool, error)
- func (*Task[T]) ID() uint64
- func (*Task[T]) IsDone() bool
- func (*Task[T]) Poll() (T, bool, error)
- func (*Task[T]) Wait(ctx context.Context) (T, bool, error)
- type TaskInfo
Variables
var ErrNotStarted = errors.New("async: group not started")
Functions
func ContextID(ctx context.Context) int
ContextID retrieves the integer ID from the context. Panics if absent.
func RunningCount() int
RunningCount returns the number of currently executing async tasks.
func SelectSlice[T any](ctx context.Context, inp []<-chan T) (int, T, bool)
SelectSlice waits for the first channel in inp to become ready, or for ctx to be cancelled. Returns the index of the ready channel (-1 on cancellation), the received value, and whether the channel was open.
func SelectSliceBi[T any](ctx context.Context, inp []chan T) (int, T, bool)
SelectSliceBi is like SelectSlice but accepts bidirectional channels.
func WithContextID(ctx context.Context, id int) context.Context
WithContextID stores an integer ID in the context.
Types
type ErrFunc[T any] func(context.Context) error
ErrFunc is a function that only returns an error.
type Func[T any] func(context.Context) (T, error)
Func is a function that returns a value and an error.
type Group[T any] struct { // contains filtered or unexported fields }
Group manages a set of async tasks with collective start/stop/wait semantics.
func NewGroupFixed[T any](executors ...Func[T]) *Group[T]
NewGroupFixed creates a Group pre-populated with the given executors.
func NewGroupRepeated[T any](executor Func[T], count int) *Group[T]
NewGroupRepeated creates a Group with count copies of the same executor.
AddExecutor adds an executor to the group. Panics if the group is already started.
func (g *Group[T]) All(ctx context.Context) error
All blocks until all tasks in the group complete or ctx is cancelled.
func (g *Group[T]) Any(ctx context.Context) error
Any blocks until any one task in the group completes or ctx is cancelled.
func (g *Group[T]) FirstError() error
FirstError polls all tasks and returns the first non-nil error found, or nil.
func (g *Group[T]) Start(ctx context.Context) error
Start launches all executors concurrently. Each task receives a context with its index stored via WithContextID. Panics on double start.
func (g *Group[T]) Stop(ctx context.Context) error
Stop cancels the group's shared context and waits for all tasks to finish.
type Registry struct { // contains filtered or unexported fields }
Registry stores metadata about all running tasks for investigation.
All returns a snapshot of all currently running tasks.
ByTag returns all running tasks that have the given tag.
Get returns the TaskInfo for a task by ID, or nil if not found.
type Task[T any] struct { // contains filtered or unexported fields }
Task is a generic async task that runs a function in a goroutine and provides methods to wait for, poll, or cancel it.
func NewDummy[T any](ctx context.Context) *Task[T]
NewDummy creates a task that immediately completes with the zero value.
func Start[T any](ctx context.Context, executor Func[T]) *Task[T]
Start creates and starts a new Task with the given executor. The task runs in a new goroutine with a derived cancellable context. Not thread-safe — do not call concurrently for the same Task instance.
func StartWith[T any](ctx context.Context, registry *Registry, executor Func[T], tags ...string) *Task[T]
func (p *Task[T]) C() <-chan struct{}
C returns the channel that is closed when the task completes.
func (p *Task[T]) CancelAndWait(ctx context.Context) (T, bool, error)
CancelAndWait cancels the task and waits for it to finish.
func (p *Task[T]) ID() uint64
ID returns the task's registry ID, or 0 if not registered.
func (p *Task[T]) IsDone() bool
IsDone reports whether the task has finished.
func (p *Task[T]) Poll() (T, bool, error)
Poll returns the task result without blocking. The second return value indicates whether the task has completed.
func (p *Task[T]) Wait(ctx context.Context) (T, bool, error)
Wait blocks until the task completes or ctx is cancelled. The second return value indicates whether the task completed (true) or the context was cancelled (false).
ErrNotStarted is returned when operating on a group that hasn't been started.