go.bigb.es/auxilia

v0.2.0
Doc Versions Source

Index

Variables

var ErrNotStarted = errors.New("async: group not started")

ErrNotStarted is returned when operating on a group that hasn't been started.

Functions

f func ContextID

src

ContextID retrieves the integer ID from the context. Panics if absent.

f func RunningCount

src
func RunningCount() int

RunningCount returns the number of currently executing async tasks.

f func SelectSlice

src
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.

f func SelectSliceBi

src
func SelectSliceBi[T any](ctx context.Context, inp []chan T) (int, T, bool)

SelectSliceBi is like SelectSlice but accepts bidirectional channels.

f func WithContextID

src

WithContextID stores an integer ID in the context.

Types

T type ErrFunc

src
type ErrFunc[T any] func(context.Context) error

ErrFunc is a function that only returns an error.

T type Func

src
type Func[T any] func(context.Context) (T, error)

Func is a function that returns a value and an error.

f func WrapErrFunc

src
func WrapErrFunc[T any](f ErrFunc[T]) Func[T]

WrapErrFunc wraps an ErrFunc into a Func that returns the zero value of T.

T type Group

src
type Group[T any] struct {
	// contains filtered or unexported fields
}

Group manages a set of async tasks with collective start/stop/wait semantics.

f func NewGroup

src
func NewGroup[T any]() *Group[T]

NewGroup creates an empty Group.

f func NewGroupFixed

src
func NewGroupFixed[T any](executors ...Func[T]) *Group[T]

NewGroupFixed creates a Group pre-populated with the given executors.

f func NewGroupRepeated

src
func NewGroupRepeated[T any](executor Func[T], count int) *Group[T]

NewGroupRepeated creates a Group with count copies of the same executor.

m func (*Group[T]) AddExecutor

src
func (g *Group[T]) AddExecutor(executor Func[T])

AddExecutor adds an executor to the group. Panics if the group is already started.

m func (*Group[T]) All

src
func (g *Group[T]) All(ctx context.Context) error

All blocks until all tasks in the group complete or ctx is cancelled.

m func (*Group[T]) Any

src
func (g *Group[T]) Any(ctx context.Context) error

Any blocks until any one task in the group completes or ctx is cancelled.

m func (*Group[T]) FirstError

src
func (g *Group[T]) FirstError() error

FirstError polls all tasks and returns the first non-nil error found, or nil.

m func (*Group[T]) Start

src
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.

m func (*Group[T]) Stop

src
func (g *Group[T]) Stop(ctx context.Context) error

Stop cancels the group's shared context and waits for all tasks to finish.

T type Registry

src
type Registry struct {
	// contains filtered or unexported fields
}

Registry stores metadata about all running tasks for investigation.

f func NewRegistry

src

NewRegistry creates a new Registry.

m func (*Registry) All

src
func (r *Registry) All() []*TaskInfo

All returns a snapshot of all currently running tasks.

m func (*Registry) ByTag

src
func (r *Registry) ByTag(tag string) []*TaskInfo

ByTag returns all running tasks that have the given tag.

m func (*Registry) Get

src
func (r *Registry) Get(id uint64) *TaskInfo

Get returns the TaskInfo for a task by ID, or nil if not found.

m func (*Registry) Len

src
func (r *Registry) Len() int

Len returns the number of currently registered tasks.

T type Task

src
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.

f func NewDummy

src
func NewDummy[T any](ctx context.Context) *Task[T]

NewDummy creates a task that immediately completes with the zero value.

f func Start

src
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.

f func StartWith

src
func StartWith[T any](ctx context.Context, registry *Registry, executor Func[T], tags ...string) *Task[T]

StartWith creates and starts a new Task, registering it in the given Registry with the provided tags for later investigation.

m func (*Task[T]) C

src
func (p *Task[T]) C() <-chan struct{}

C returns the channel that is closed when the task completes.

m func (*Task[T]) Cancel

src
func (p *Task[T]) Cancel()

Cancel cancels the task's internal context.

m func (*Task[T]) CancelAndWait

src
func (p *Task[T]) CancelAndWait(ctx context.Context) (T, bool, error)

CancelAndWait cancels the task and waits for it to finish.

m func (*Task[T]) ID

src
func (p *Task[T]) ID() uint64

ID returns the task's registry ID, or 0 if not registered.

m func (*Task[T]) IsDone

src
func (p *Task[T]) IsDone() bool

IsDone reports whether the task has finished.

m func (*Task[T]) Poll

src
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.

m func (*Task[T]) Wait

src
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).

T type TaskInfo

src
type TaskInfo struct {
	ID          uint64
	Tags        []string
	StartTime   time.Time
	GoroutineID uint64
	// contains filtered or unexported fields
}

TaskInfo holds metadata about a running task for debugging and investigation.

m func (*TaskInfo) IsRunning

src
func (ti *TaskInfo) IsRunning() bool

IsRunning reports whether the task is still running.

m func (*TaskInfo) Stacktrace

src
func (ti *TaskInfo) Stacktrace() string

Stacktrace returns the stack trace of the goroutine running this task. Returns empty string if the task has already finished.