Documentation
¶
Index ¶
- func Backoff(ctx context.Context, fn func(ctx context.Context, attempt int) (bool, error), ...) error
- func Call[T funcType](fn T) error
- func Go[T funcType](fn T)
- func GoWithDeadline(deadline time.Time, fns ...func())
- func GoWithTimeout(timeout time.Duration, fns ...func())
- func Recover()
- type Goroutines
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Backoff ¶ added in v2.6.0
func Backoff(ctx context.Context, fn func(ctx context.Context, attempt int) (bool, error), retry int, baseDelay, maxDelay time.Duration) error
Backoff calls fn with an exponentially increasing delay.
The delay starts at baseDelay, doubles after every attempt and is capped at maxDelay. Retrying stops when fn returns next as false, when ctx is cancelled, or after retry attempts have been made. The attempt argument passed to fn starts at 1.
It returns the error of the last fn call, or ctx.Err() when ctx has been cancelled.
func Call ¶
func Call[T funcType](fn T) error
Call invokes fn safely.
It recovers from any panic raised while fn runs: a runtime error (runtime.Error) is logged as a fatal error, while any other panic is logged together with its error message.
func Go ¶ added in v2.1.0
func Go[T funcType](fn T)
Go runs fn in a new goroutine.
fn is executed in its own goroutine and any panic is recovered by Call, so a crashing goroutine does not bring down the whole process.
func GoWithDeadline ¶ added in v2.2.3
GoWithDeadline runs fns concurrently and blocks until all of them finish or the deadline is reached.
Unlike Go, it blocks the calling goroutine until every fn has completed or the deadline is reached, at which point it stops waiting.
func GoWithTimeout ¶ added in v2.2.3
GoWithTimeout runs fns concurrently and blocks until all of them finish or the timeout elapses.
Unlike Go, it blocks the calling goroutine until every fn has completed or the timeout is reached.
func Recover ¶ added in v2.6.5
func Recover()
Recover recovers from any panic raised in the current goroutine.
It logs the panic error as a fatal error. If the panic is a runtime error, it logs it as a fatal error. If the panic is any other type, it logs it as a panic error. If the panic is nil, it does nothing.
Types ¶
type Goroutines ¶ added in v2.2.3
type Goroutines struct {
// contains filtered or unexported fields
}
Goroutines is a goroutine group that manages a batch of functions to run concurrently.
func NewGoroutines ¶ added in v2.2.3
func NewGoroutines() *Goroutines
NewGoroutines returns a new Goroutines group.
func (*Goroutines) Add ¶ added in v2.2.3
func (g *Goroutines) Add(fns ...func()) *Goroutines
Add appends the given functions to the group and returns the group itself so that calls can be chained.
func (*Goroutines) Run ¶ added in v2.2.3
func (g *Goroutines) Run(ctx context.Context, timeout ...time.Duration)
Run starts every function the group holds, each in its own goroutine, and waits for them according to ctx.
The group is emptied before the functions run, so the same instance can be reused. When no timeout is given, Run blocks until all functions have finished; when a timeout is given, it returns early on timeout without waiting for the remaining functions.