async

package
v0.38.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(ctx context.Context, fns ...func(context.Context) error) error

All runs fns concurrently and returns the first error (or nil). First error cancels remaining work via a derived context. Functions capture output via closures to pre-declared outer variables. Panics are recovered and converted to errors.

func Fire

func Fire(fn func())

Fire spawns a goroutine with panic recovery. Panics logged to slog.Default().

func Map

func Map[T, R any](ctx context.Context, items []T, fn func(context.Context, T) (R, error)) ([]R, error)

Map applies fn to every item concurrently and returns results in order. First error cancels remaining work. Panics are recovered and converted to errors.

func Settle

func Settle(ctx context.Context, fns ...func(context.Context) error) []error

Settle runs fns concurrently and returns every error. Never short-circuits — all functions run to completion. Functions capture output via closures to pre-declared outer variables. Panics are recovered and converted to errors.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group manages fire-and-forget goroutines with panic recovery.

func NewGroup

func NewGroup(opts ...GroupOption) *Group

NewGroup creates a Group with the given options.

func (*Group) Close

func (g *Group) Close()

Close waits for all in-flight goroutines to finish.

func (*Group) Go

func (g *Group) Go(fn func())

Go spawns work; blocks if at limit; no-op after Close.

type GroupMetrics

type GroupMetrics interface {
	Blocked()                         // semaphore contention occurred (fired after the wait completes)
	Dispatched(blocked time.Duration) // job accepted; worker goroutine about to spawn (blocked=0 if no wait)
	Completed(duration time.Duration) // job finished without panic recovery
	Panicked(duration time.Duration)  // job finished via panic recovery
}

GroupMetrics receives lifecycle callbacks from Group.Go.

Every Dispatched call is followed by exactly one Completed or Panicked call. Blocked fires at most once per Dispatched, immediately before it. No callbacks fire for calls to Go after Close (true no-op).

WARNING: callbacks run synchronously on the Group hot path and during worker teardown. They are not isolated in a separate goroutine. Implementations must be safe for concurrent use, must not block, must not perform slow I/O, and must not panic. A bad implementation can stall Go(), delay or deadlock Close(), and hold semaphore slots longer than expected.

type GroupOption

type GroupOption func(*Group)

GroupOption configures a Group.

func WithGroupLogger

func WithGroupLogger(l *slog.Logger) GroupOption

WithGroupLogger sets the logger for panic recovery output.

func WithLimit

func WithLimit(n int) GroupOption

WithLimit caps concurrent goroutines via a semaphore. Values less than 1 are clamped to 1.

func WithMetrics

func WithMetrics(m GroupMetrics) GroupOption

WithMetrics sets the metrics observer for Group lifecycle events. See GroupMetrics for the synchronous callback warning and constraints.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL