Documentation
¶
Overview ¶
Package poller is the one loop that the module runs on a schedule: a tick function, a wait between ticks with an optional spread (jitter), a longer wait after a failed tick (a Backoff that grows and resets on success), an optional wake channel that ends the wait at once, and a stop by context.
Run blocks and owns nothing: the caller owns the goroutine. When Run returns the tick function is not running and will not be called again. A tick that panics is not recovered: the panic goes up through Run to the caller, which decides what a failed loop means. All waits go through one Waiter, so a test that replaces it (or runs the loop in a testing/synctest bubble with the default one) drives the time.
Index ¶
- Constants
- Variables
- func Clamp(value float64) float64
- func NewTicker(interval time.Duration) (<-chan time.Time, func())
- func Refused(err error) bool
- func Run(ctx context.Context, cfg *Config, tick func(ctx context.Context) error) error
- func Wait(ctx context.Context, d time.Duration, wake <-chan struct{}) error
- type Backoff
- type Config
- type Waiter
Constants ¶
const DefaultFactor = 2
DefaultFactor is the growth of a Backoff with a zero Factor.
Variables ¶
var ErrBackoff = errors.New("poller: invalid backoff")
ErrBackoff is returned by Run and Backoff.Validate for a Backoff that cannot be run.
var ErrInterval = errors.New("poller: invalid interval")
ErrInterval is returned by Run for a negative interval.
var ErrStop = errors.New("poller: stop")
ErrStop is returned by a tick to end Run with a nil error.
Functions ¶
func Clamp ¶
Clamp returns v in [0, 1): a NaN or a negative value is 0, and a value of 1 or more is the largest value below 1.
func NewTicker ¶
NewTicker returns the channel of a time.Ticker and the function that stops it: the default source of a caller that keeps a seam for Config.Ticks.
func Refused ¶
Refused tells whether the error of Run is a refused configuration (ErrInterval or ErrBackoff): the loop did not run, which is a failure to report; any other error of Run is the end of the context.
func Run ¶
Run calls tick until ctx is done or tick returns ErrStop. It returns nil for a stop by tick, and an error that wraps the cause of the context for the other stops. Other errors of tick are not returned: they count as a failure for the backoff. A refused Config.Interval or Config.Backoff is returned before the first tick.
Types ¶
type Backoff ¶
type Backoff struct {
Rand func() float64
Base time.Duration
Factor float64
Max time.Duration
Floor float64
}
Backoff is the wait after failures in a row: Base for the first, growing by Factor for each next one, up to Max. The zero value is valid and never waits (Base 0). Factor 0 means DefaultFactor; any other Factor must be finite and above 1. Base, Max and Floor must not be negative, and a Max (0 means none) must not be below Base.
func (Backoff) Delay ¶
Delay is the delay after the given number of failures in a row (1 is the first), without jitter. It never overflows. Below 1 failure it is zero.
func (Backoff) Validate ¶
Validate tells whether the backoff can be run: the error wraps ErrBackoff. Run calls it.