poller

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultFactor = 2

DefaultFactor is the growth of a Backoff with a zero Factor.

Variables

View Source
var ErrBackoff = errors.New("poller: invalid backoff")

ErrBackoff is returned by Run and Backoff.Validate for a Backoff that cannot be run.

View Source
var ErrInterval = errors.New("poller: invalid interval")

ErrInterval is returned by Run for a negative interval.

View Source
var ErrStop = errors.New("poller: stop")

ErrStop is returned by a tick to end Run with a nil error.

Functions

func Clamp

func Clamp(value float64) float64

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

func NewTicker(interval time.Duration) (<-chan time.Time, func())

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

func Refused(err error) bool

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

func Run(ctx context.Context, cfg *Config, tick func(ctx context.Context) error) error

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.

func Wait

func Wait(ctx context.Context, d time.Duration, wake <-chan struct{}) error

Wait is the default Waiter: a timer.

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

func (b Backoff) Delay(failures int) time.Duration

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

func (b Backoff) Validate() error

Validate tells whether the backoff can be run: the error wraps ErrBackoff. Run calls it.

func (Backoff) Wait

func (b Backoff) Wait(failures int) time.Duration

Wait is the delay after the failures, spread by the jitter: delay times Floor plus a random share of the rest.

type Config

type Config struct {
	Wake        <-chan struct{}
	Wait        Waiter
	Rand        func() float64
	Ticks       <-chan time.Time
	Backoff     Backoff
	Interval    time.Duration
	Spread      float64
	MinWait     time.Duration
	StartSpread time.Duration
	Immediate   bool
}

Config configures Run.

type Waiter

type Waiter func(ctx context.Context, d time.Duration, wake <-chan struct{}) error

Waiter blocks for d, until wake delivers (a nil channel never does), or until ctx is done. It returns nil when d passed or wake delivered, and an error otherwise.

Jump to

Keyboard shortcuts

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