Documentation
¶
Index ¶
- func AwaitAll[F FutureDyn](ctx context.Context, futures ...F) status.Status
- func AwaitAny[F Future[T], T any](ctx context.Context, futures ...F) (T, int, status.Status)
- func AwaitAnyDyn[F FutureDyn](ctx context.Context, futures ...F) (int, status.Status)
- func AwaitError[F FutureDyn](ctx context.Context, futures ...F) (int, status.Status)
- func StdContext(ctx Context) context_.Context
- func StopAll[R Stopper](routines ...R)
- func StopWait[R Stopper](r R)
- func StopWaitAll[R Stopper](routines ...R)
- type CancelContext
- type Context
- type ContextCallback
- type Flag
- type Func
- type Func1
- type FuncVoid
- type FuncVoid1
- type Future
- type FutureDyn
- type FutureGroup
- func (g FutureGroup[T]) Await(ctx context.Context) status.Status
- func (g FutureGroup[T]) AwaitAny(ctx context.Context) (T, int, status.Status)
- func (g FutureGroup[T]) AwaitError(ctx context.Context) (int, status.Status)
- func (g FutureGroup[T]) AwaitResults(ctx context.Context) ([]Result[T], status.Status)
- func (g FutureGroup[T]) Results() []Result[T]
- func (g FutureGroup[T]) Statuses() []status.Status
- type FutureGroupDyn
- type Lock
- type MutFlag
- type NextFunc
- type Pool
- type Promise
- type Queue
- type Result
- type Routine
- type RoutineDyn
- type RoutineGroup
- func (g RoutineGroup[T]) Await(ctx Context) status.Status
- func (g RoutineGroup[T]) AwaitAny(ctx Context) (T, int, status.Status)
- func (g RoutineGroup[T]) AwaitError(ctx Context) (int, status.Status)
- func (g RoutineGroup[T]) AwaitResults(ctx Context) ([]Result[T], status.Status)
- func (g RoutineGroup[T]) Results() []Result[T]
- func (g RoutineGroup[T]) Statuses() []status.Status
- func (g RoutineGroup[T]) Stop()
- func (g RoutineGroup[T]) StopWait()
- func (g RoutineGroup[T]) Values() []T
- type RoutineGroupDyn
- func (g RoutineGroupDyn) Await(ctx Context) status.Status
- func (g RoutineGroupDyn) AwaitAny(ctx Context) (int, status.Status)
- func (g RoutineGroupDyn) AwaitError(ctx Context) (int, status.Status)
- func (g RoutineGroupDyn) Statuses() []status.Status
- func (g RoutineGroupDyn) Stop()
- func (g RoutineGroupDyn) StopWait()
- type RoutineVoid
- type Runner
- type Service
- type StopGroup
- func (g *StopGroup) Add(s Stopper)
- func (g *StopGroup) AddMany(stoppers ...Stopper)
- func (g *StopGroup) AddService(s Service)
- func (g *StopGroup) AddTicker(t *time.Ticker)
- func (g *StopGroup) AddTimer(t *time.Timer)
- func (g *StopGroup) Stop()
- func (g *StopGroup) StopWait()
- func (g *StopGroup) Wait()
- type Stopper
- type Stream
- type Variable
- type WaitLock
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AwaitAll ¶
AwaitAll waits for the completion of all futures. The method the context status if the context is cancelled.
func AwaitAny ¶
AwaitAny waits for the completion of any future, and returns its result. The method returns -1 and the context status if the context is cancelled.
func AwaitAnyDyn ¶
AwaitAnyDyn awaits completion of any future, and returns its result. The method returns -1 and the context status if the context is cancelled.
func AwaitError ¶
AwaitError awaits failure of any future, and returns its error. The method returns -1 and the context status if the context is cancelled.
func StdContext ¶
StdContext returns a standard library context from an async one.
func StopAll ¶
func StopAll[R Stopper](routines ...R)
StopAll stops all routines, but does not await their stop.
func StopWait ¶
func StopWait[R Stopper](r R)
StopWait stops a routine and awaits it stop.
Usually used with defer:
w := runWorker() defer StopWait(w)
func StopWaitAll ¶
func StopWaitAll[R Stopper](routines ...R)
StopWaitAll stops and awaits all routines.
Usually used with defer:
w0 := runWorker() w1 := runWorker() defer StopWaitAll(w0, w1)
Types ¶
type CancelContext ¶
type CancelContext = context.CancelContext
CancelContext is a cancellable async context.
func CancelledContext ¶
func CancelledContext() CancelContext
CancelledContext returns a cancelled context.
func NextContext ¶
func NextContext(parent Context) CancelContext
NextContext returns a child context.
type Context ¶
Context is an async cancellation context.
func DeadlineContext ¶
DeadlineContext returns a context with a deadline.
func NextDeadlineContext ¶
NextDeadlineContext returns a child context with a deadline.
func NextTimeoutContext ¶
NextTimeoutContext returns a child context with a timeout.
func TimeoutContext ¶
TimeoutContext returns a context with a timeout.
type ContextCallback ¶
ContextCallback is called when the context is cancelled.
type Flag ¶
Flag is a read-only boolean flag that can be waited on until set.
Example:
serving := async.UnsetFlag() func handle(ctx Context, req *request) { if !serving.Get() { // just to show Get in example select { case <-ctx.Wait(): return ctx.Status() case <-serving.Wait(): } } // ... handle request }
func ReverseFlag ¶
ReverseFlag returns a new flag which reverses the original one.
type Future ¶
type Future[T any] interface { // Done returns true if the future is complete. Done() bool // Wait returns a channel which is closed when the future is complete. Wait() <-chan struct{} // Result returns a value and a status. Result() (T, status.Status) // Status returns a status or none. Status() status.Status }
Future represents a result available in the future.
type FutureDyn ¶
type FutureDyn interface { // Done returns true if the future is complete. Done() bool // Wait returns a channel which is closed when the future is complete. Wait() <-chan struct{} // Status returns a status or none. Status() status.Status }
FutureDyn is a future interface without generics, i.e. Future[?].
type FutureGroup ¶
FutureGroup is a group of futures of the same type. Use FutureGroupDyn for a group of futures of different types.
func (FutureGroup[T]) Await ¶
func (g FutureGroup[T]) Await(ctx context.Context) status.Status
Await waits for the completion of all futures in the group. The method returns the context status if the context is cancelled.
func (FutureGroup[T]) AwaitAny ¶
AwaitAny waits for the completion of any future in the group, and returns its result. The method returns -1 and the context status if the context is cancelled.
func (FutureGroup[T]) AwaitError ¶
AwaitError waits for any failure of the futures in the group, and returns the error. The method returns ok if all futures are successful.
func (FutureGroup[T]) AwaitResults ¶
AwaitResults waits for the completion of all futures in the group, and returns the results. The method returns nil and the context status if the context is cancelled.
func (FutureGroup[T]) Results ¶
func (g FutureGroup[T]) Results() []Result[T]
Results returns the results of all futures in the group.
func (FutureGroup[T]) Statuses ¶
func (g FutureGroup[T]) Statuses() []status.Status
Statuses returns the statuses of all futures in the group.
type FutureGroupDyn ¶
type FutureGroupDyn []FutureDyn
FutureGroupDyn is a group of futures of different types. Use FutureGroup for a group of futures of the same type.
func (FutureGroupDyn) Await ¶
func (g FutureGroupDyn) Await(ctx context.Context) status.Status
Await waits for the completion of all futures in the group. The method returns nil and the context status if the context is cancelled.
func (FutureGroupDyn) AwaitAny ¶
AwaitAny waits for the completion of any future in the group, and returns its status. The method returns -1 and the context status if the context is cancelled.
func (FutureGroupDyn) AwaitError ¶
AwaitError waits for any failure of the futures in the group, and returns the error. The method returns ok if all futures are successful.
func (FutureGroupDyn) Statuses ¶
func (g FutureGroupDyn) Statuses() []status.Status
Statuses returns the statuses of all futures in the group.
type Lock ¶
Lock is a channel-based lock, which be used directly as a channel, or via the Lock/Unlock methods. To lock a lock receive an empty struct from it, to unlock a lock send an empty struct to it.
Example:
lock := async.NewLock() select { case <-lock: case <-cancel: return status.Cancelled } defer lock.Unlock()
type MutFlag ¶
MutFlag is a routine-safe boolean flag that can be set, reset, and waited on until set.
Example:
serving := async.UnsetFlag() func serve() { s.serving.Set() defer s.serving.Unset() // ... start server ... } func handle(ctx Context, req *request) { select { case <-ctx.Wait(): return ctx.Status() case <-serving.Wait(): } // ... handle request }
type Promise ¶
type Promise[T any] interface { Future[T] // Reject rejects the promise with a status. Reject(st status.Status) bool // Resolve completes the promise with a result and ok. Resolve(result T) bool // Complete completes the promise with a status and a result. Complete(result T, st status.Status) bool }
Promise is a future which can be completed.
type Queue ¶
type Queue[T any] interface { // Len returns the number of elements in the queue. Len() int // Clear clears the queue. Clear() // Push adds an element to the queue. Push(v T) // Poll removes an element from the queue, returns false if the queue is empty. Poll() (T, bool) // Wait returns a channel which is notified on new elements. Wait() <-chan struct{} }
Queue is an unbounded FIFO queue guarded by a mutex.
type Result ¶
Result is an async result which combines a value and a status.
type Routine ¶
type Routine[T any] interface { Future[T] // Start start the routine, if not started or stopped yet. Start() // Stop requests the routine to stop and returns a wait channel. // The method does not call the on-stop callbacks if the routine has not started. Stop() <-chan struct{} // OnStop adds a callback which is called when the routine stops. // The callback is called by the routine goroutine, and is not called // if the routine has not started. OnStop(fn func(Routine[T])) bool }
Routine is an async routine which returns the result as a future, recovers on panics, and can be cancelled.
func NewRoutine ¶
NewRoutine returns a new routine, but does not start it.
type RoutineDyn ¶
type RoutineDyn interface { FutureDyn // Start starts the routine, if not stopped already. Start() // Stop requests the routine to stop and returns a wait channel. Stop() <-chan struct{} }
RoutineDyn is a routine interface without generics, i.e. Routine[?].
type RoutineGroup ¶
RoutineGroup is a group of routines of the same type. Use RoutineGroupDyn for a group of routines of different types.
func (RoutineGroup[T]) Await ¶
func (g RoutineGroup[T]) Await(ctx Context) status.Status
Await waits for the completion of all routines in the group. The method returns the context status if the context is cancelled.
func (RoutineGroup[T]) AwaitAny ¶
func (g RoutineGroup[T]) AwaitAny(ctx Context) (T, int, status.Status)
AwaitAny waits for the completion of any routine in the group, and returns its result. The method returns -1 and the context status if the context is cancelled.
func (RoutineGroup[T]) AwaitError ¶
func (g RoutineGroup[T]) AwaitError(ctx Context) (int, status.Status)
AwaitError waits for any failure of the routines in the group, and returns the error. The method returns ok if all routines are successful.
func (RoutineGroup[T]) AwaitResults ¶
func (g RoutineGroup[T]) AwaitResults(ctx Context) ([]Result[T], status.Status)
AwaitResults waits for the completion of all routines in the group, and returns the results. The method returns nil and the context status if the context is cancelled.
func (RoutineGroup[T]) Results ¶
func (g RoutineGroup[T]) Results() []Result[T]
Results returns the results of all routines in the group.
func (RoutineGroup[T]) Statuses ¶
func (g RoutineGroup[T]) Statuses() []status.Status
Statuses returns the statuses of all routines in the group.
func (RoutineGroup[T]) StopWait ¶
func (g RoutineGroup[T]) StopWait()
StopWait stops and awaits all routines in the group.
func (RoutineGroup[T]) Values ¶
func (g RoutineGroup[T]) Values() []T
Values returns the values of all results in the group.
type RoutineGroupDyn ¶
type RoutineGroupDyn []RoutineDyn
RoutineGroupDyn is a group of routines of different types. Use RoutineGroup for a group of routines of the same type.
func (RoutineGroupDyn) Await ¶
func (g RoutineGroupDyn) Await(ctx Context) status.Status
Await waits for the completion of all routines in the group. The method returns the context status if the context is cancelled.
func (RoutineGroupDyn) AwaitAny ¶
func (g RoutineGroupDyn) AwaitAny(ctx Context) (int, status.Status)
AwaitAny waits for the completion of any routine in the group, and returns its result. The method returns -1 and the context status if the context is cancelled.
func (RoutineGroupDyn) AwaitError ¶
func (g RoutineGroupDyn) AwaitError(ctx Context) (int, status.Status)
AwaitError waits for any failure of the routines in the group, and returns the error. The method returns ok if all routines are successful.
func (RoutineGroupDyn) Statuses ¶
func (g RoutineGroupDyn) Statuses() []status.Status
Statuses returns the statuses of all routines in the group.
func (RoutineGroupDyn) StopWait ¶
func (g RoutineGroupDyn) StopWait()
StopWait stops and awaits all routines in the group.
type RoutineVoid ¶
type RoutineVoid = Routine[struct{}]
RoutineVoid is a routine which has no result.
func NewRoutineVoid ¶
func NewRoutineVoid(fn func(ctx Context) status.Status) RoutineVoid
NewRoutineVoid returns a new routine without a result, but does not start it.
func RunVoid ¶
func RunVoid(fn FuncVoid) RoutineVoid
RunVoid runs a procedure in a new routine, recovers on panics.
func RunVoid1 ¶
func RunVoid1[A any](fn FuncVoid1[A], arg A) RoutineVoid
RunVoid1 runs a procedure in a new routine, recovers on panics.
func StoppedVoid ¶
func StoppedVoid(st status.Status) RoutineVoid
StoppedVoid returns a routine without a result which has stopped with the given status.
type Runner ¶
type Runner interface {
// Run runs the function.
Run()
}
Runner is an interface that provides a run method.
func RunnerFunc ¶
func RunnerFunc(fn func()) Runner
RunnerFunc returns a new runner from a function.
type Service ¶
type Service interface { // Status returns the stop status or none. Status() status.Status // Running indicates that the service routine is running. Running() Flag // Stopped indicates that the service is stopped. Stopped() Flag // Start starts the service if not running. Start() status.Status // Stop requests the service to stop and returns its routine or a stopped routine. Stop() <-chan struct{} // Wait returns a channel which is closed when the service is stopped. Wait() <-chan struct{} }
Service is a service which can be started and stopped.
type StopGroup ¶
type StopGroup struct {
// contains filtered or unexported fields
}
StopGroup stops all operations in the group and awaits their completion.
func (*StopGroup) Add ¶
Add adds a stopper to the group, or immediately stops it if the group is stopped.
func (*StopGroup) AddMany ¶
AddMany adds multiple stoppers to the group, or immediatelly stops them if the group is stopped.
func (*StopGroup) AddService ¶
AddService adds a service to the group, or immediately stops it if the group is stopped.
func (*StopGroup) AddTicker ¶
AddTicker adds a ticker to the group, or immediately stops it if the group is stopped.
func (*StopGroup) AddTimer ¶
AddTimer adds a timer to the group, or immediately stops it if the group is stopped.
type Stopper ¶
type Stopper interface { // Wait returns a channel which is closed when the future is complete. Wait() <-chan struct{} // Stop requests the routine to stop and returns a wait channel. Stop() <-chan struct{} }
Stopper stops a routine and awaits its stop.
type Stream ¶
type Stream[T any] interface { // Next returns the next value from the stream, or false if the stream has ended. Next(ctx context.Context) (T, bool, status.Status) // Free frees the stream. Free() }
Stream is an async stream of values.
func NewStreamFree ¶
NewStreamFree returns a new stream with a free function.
type Variable ¶
type Variable[T any] interface { // Get returns the current value/error, or false if pending. Get() (T, bool, status.Status) // GetWait returns the current value or waits for it or an error. GetWait(ctx context.Context) (T, status.Status) // Clear clears the variable. Clear() // Complete sets the value and status. Complete(value T, st status.Status) // Fail sets the error. Fail(st status.Status) // Set sets the value. Set(value T) // Wait returns a channel that is closed when the variable is set or failed. Wait() <-chan struct{} }
[Experimental] Variable is an asynchronous variable which can be set, cleared, or failed.
func NewVariable ¶
NewVariable returns a new pending async variable.
type WaitLock ¶
WaitLock is a lock which allows others to wait until it is unlocked.
WaitLock does not guarantee that the lock is not acquired by another writer after its waiters are notified.
WaitLock can be used, for example, to execute a single operation by one writer, while other writers wait until the operation is completed.
Example:
lock := async.NewWaitLock() func flush(cancel <-chan struct{}) { select { case <-lock.Lock(): // Acquired lock default: // Await flushing end select { case <-lock.Wait(): case <-cancel: return status.Cancelled } } defer lock.Unlock() // ... Do work ... }