Documentation
¶
Overview ¶
Package supervisor runs fallible functions until they succeed, retrying with exponential backoff when they fail.
Index ¶
- func Fatal(err error) error
- func Fatalf(format string, args ...any) error
- func Jitter(d time.Duration) time.Duration
- func Persistent(err error) error
- func Persistentf(format string, args ...any) error
- func Run(ctx context.Context, logger *slog.Logger, fn func(context.Context) error) error
- type Group
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fatal ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.