supervisor

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package supervisor runs fallible functions until they succeed, retrying with exponential backoff when they fail.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fatal

func Fatal(err error) error

Fatal returns an error that causes Run to stop retrying and return err.

It is used for failures that retrying can not recover from.

func Fatalf

func Fatalf(format string, args ...any) error

Fatalf returns an error formatted according to format, as per fmt.Errorf, that causes Run to stop retrying and return it.

It is used for failures that retrying can not recover from.

func Jitter

func Jitter(d time.Duration) time.Duration

Jitter returns a random duration between half and all of d, inclusive.

It is used to spread out delays that would otherwise end at the same time, to avoid thundering herds.

d must not be negative.

func Persistent

func Persistent(err error) error

Persistent returns an error that causes Run to retry fn with a backoff suited to failures that persist until something outside the engine changes, such as a handler's code or data.

Run waits longer between retries after such a failure than after others.

func Persistentf

func Persistentf(format string, args ...any) error

Persistentf returns an error formatted according to format, as per fmt.Errorf, that causes Run to retry fn with the backoff described by Persistent.

func Run

func Run(
	ctx context.Context,
	logger *slog.Logger,
	fn func(context.Context) error,
) error

Run calls fn until it succeeds, fails fatally, or ctx is canceled.

A nil return from fn is success. An error created by Fatal or Fatalf is returned to the caller without retrying. Any other error is logged and fn is called again after a delay. Once ctx is canceled, fn is not called again regardless of what it returned.

The delay doubles with each consecutive failure, up to a limit. The limit is higher when the error was created by Persistent or Persistentf, so that failures unlikely to resolve themselves are retried less often. The delay returns to its initial value once fn runs for long enough before failing.

Types

type Group

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

Group runs supervised functions in their own goroutines, and stops them all if any of them fails fatally.

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new group, and a context derived from ctx that is canceled when a function started in the group fails fatally, or when Group.Wait returns.

func (*Group) Go

func (g *Group) Go(
	logger *slog.Logger,
	fn func(context.Context) error,
)

Go calls Run in a new goroutine, with the group's context.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until every function started with Group.Go has returned.

It returns the error from the first function to fail fatally, or nil if none did.

Jump to

Keyboard shortcuts

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