wait

package
v1.14.8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2019 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package wait provides tools for polling or listening for changes to a condition.

Index

Constants

This section is empty.

Variables

View Source
var ErrWaitTimeout = errors.New("timed out waiting for the condition")

ErrWaitTimeout is returned when the condition exited without success.

View Source
var ForeverTestTimeout = time.Second * 30

For any test of the style:

...
<- time.After(timeout):
   t.Errorf("Timed out")

The value for timeout should effectively be "forever." Obviously we don't want our tests to truly lock up forever, but 30s is long enough that it is effectively forever for the things that can slow down a run on a heavily contended machine (GC, seeks, etc), but not so long as to make a developer ctrl-c a test run if they do happen to break that test.

View Source
var NeverStop <-chan struct{} = make(chan struct{})

NeverStop may be passed to Until to make it never stop.

Functions

func ExponentialBackoff

func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error

ExponentialBackoff repeats a condition check with exponential backoff.

It checks the condition up to Steps times, increasing the wait by multiplying the previous duration by Factor.

If Jitter is greater than zero, a random amount of each duration is added (between duration and duration*(1+jitter)).

If the condition never returns true, ErrWaitTimeout is returned. All other errors terminate immediately.

func Forever

func Forever(f func(), period time.Duration)

Forever calls f every period for ever.

Forever is syntactic sugar on top of Until.

func Jitter

func Jitter(duration time.Duration, maxFactor float64) time.Duration

Jitter returns a time.Duration between duration and duration + maxFactor * duration.

This allows clients to avoid converging on periodic behavior. If maxFactor is 0.0, a suggested default value will be chosen.

func JitterUntil

func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding bool, stopCh <-chan struct{})

JitterUntil loops until stop channel is closed, running f every period.

If jitterFactor is positive, the period is jittered before every run of f. If jitterFactor is not positive, the period is unchanged and not jittered.

If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.

Close stopCh to stop. f may not be invoked if stop channel is already closed. Pass NeverStop to if you don't want it stop.

func JitterUntilWithContext added in v1.14.0

func JitterUntilWithContext(ctx context.Context, f func(context.Context), period time.Duration, jitterFactor float64, sliding bool)

JitterUntilWithContext loops until context is done, running f every period.

If jitterFactor is positive, the period is jittered before every run of f. If jitterFactor is not positive, the period is unchanged and not jittered.

If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.

Cancel context to stop. f may not be invoked if context is already expired.

func NonSlidingUntil

func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{})

NonSlidingUntil loops until stop channel is closed, running f every period.

NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter factor, with sliding = false (meaning the timer for period starts at the same time as the function starts).

func NonSlidingUntilWithContext added in v1.14.0

func NonSlidingUntilWithContext(ctx context.Context, f func(context.Context), period time.Duration)

NonSlidingUntilWithContext loops until context is done, running f every period.

NonSlidingUntilWithContext is syntactic sugar on top of JitterUntilWithContext with zero jitter factor, with sliding = false (meaning the timer for period starts at the same time as the function starts).

func Poll

func Poll(interval, timeout time.Duration, condition ConditionFunc) error

Poll tries a condition func until it returns true, an error, or the timeout is reached.

Poll always waits the interval before the run of 'condition'. 'condition' will always be invoked at least once.

Some intervals may be missed if the condition takes too long or the time window is too short.

If you want to Poll something forever, see PollInfinite.

func PollImmediate

func PollImmediate(interval, timeout time.Duration, condition ConditionFunc) error

PollImmediate tries a condition func until it returns true, an error, or the timeout is reached.

PollImmediate always checks 'condition' before waiting for the interval. 'condition' will always be invoked at least once.

Some intervals may be missed if the condition takes too long or the time window is too short.

If you want to immediately Poll something forever, see PollImmediateInfinite.

func PollImmediateInfinite

func PollImmediateInfinite(interval time.Duration, condition ConditionFunc) error

PollImmediateInfinite tries a condition func until it returns true or an error

PollImmediateInfinite runs the 'condition' before waiting for the interval.

Some intervals may be missed if the condition takes too long or the time window is too short.

func PollImmediateUntil added in v1.11.0

func PollImmediateUntil(interval time.Duration, condition ConditionFunc, stopCh <-chan struct{}) error

PollImmediateUntil tries a condition func until it returns true, an error or stopCh is closed.

PollImmediateUntil runs the 'condition' before waiting for the interval. 'condition' will always be invoked at least once.

func PollInfinite

func PollInfinite(interval time.Duration, condition ConditionFunc) error

PollInfinite tries a condition func until it returns true or an error

PollInfinite always waits the interval before the run of 'condition'.

Some intervals may be missed if the condition takes too long or the time window is too short.

func PollUntil

func PollUntil(interval time.Duration, condition ConditionFunc, stopCh <-chan struct{}) error

PollUntil tries a condition func until it returns true, an error or stopCh is closed.

PollUntil always waits interval before the first run of 'condition'. 'condition' will always be invoked at least once.

func Until

func Until(f func(), period time.Duration, stopCh <-chan struct{})

Until loops until stop channel is closed, running f every period.

Until is syntactic sugar on top of JitterUntil with zero jitter factor and with sliding = true (which means the timer for period starts after the f completes).

func UntilWithContext added in v1.14.0

func UntilWithContext(ctx context.Context, f func(context.Context), period time.Duration)

UntilWithContext loops until context is done, running f every period.

UntilWithContext is syntactic sugar on top of JitterUntilWithContext with zero jitter factor and with sliding = true (which means the timer for period starts after the f completes).

func WaitFor

func WaitFor(wait WaitFunc, fn ConditionFunc, done <-chan struct{}) error

WaitFor continually checks 'fn' as driven by 'wait'.

WaitFor gets a channel from 'wait()”, and then invokes 'fn' once for every value placed on the channel and once more when the channel is closed. If the channel is closed and 'fn' returns false without error, WaitFor returns ErrWaitTimeout.

If 'fn' returns an error the loop ends and that error is returned. If 'fn' returns true the loop ends and nil is returned.

ErrWaitTimeout will be returned if the 'done' channel is closed without fn ever returning true.

When the done channel is closed, because the golang `select` statement is "uniform pseudo-random", the `fn` might still run one or multiple time, though eventually `WaitFor` will return.

Types

type Backoff

type Backoff struct {
	// The initial duration.
	Duration time.Duration
	// Duration is multiplied by factor each iteration. Must be greater
	// than or equal to zero.
	Factor float64
	// The amount of jitter applied each iteration. Jitter is applied after
	// cap.
	Jitter float64
	// The number of steps before duration stops changing. If zero, initial
	// duration is always used. Used for exponential backoff in combination
	// with Factor.
	Steps int
	// The returned duration will never be greater than cap *before* jitter
	// is applied. The actual maximum cap is `cap * (1.0 + jitter)`.
	Cap time.Duration
}

Backoff holds parameters applied to a Backoff function.

func (*Backoff) Step added in v1.14.0

func (b *Backoff) Step() time.Duration

Step returns the next interval in the exponential backoff. This method will mutate the provided backoff.

type ConditionFunc

type ConditionFunc func() (done bool, err error)

ConditionFunc returns true if the condition is satisfied, or an error if the loop should be aborted.

type Group added in v1.8.0

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

Group allows to start a group of goroutines and wait for their completion.

func (*Group) Start added in v1.8.0

func (g *Group) Start(f func())

Start starts f in a new goroutine in the group.

func (*Group) StartWithChannel added in v1.8.0

func (g *Group) StartWithChannel(stopCh <-chan struct{}, f func(stopCh <-chan struct{}))

StartWithChannel starts f in a new goroutine in the group. stopCh is passed to f as an argument. f should stop when stopCh is available.

func (*Group) StartWithContext added in v1.8.0

func (g *Group) StartWithContext(ctx context.Context, f func(context.Context))

StartWithContext starts f in a new goroutine in the group. ctx is passed to f as an argument. f should stop when ctx.Done() is available.

func (*Group) Wait added in v1.8.0

func (g *Group) Wait()

type WaitFunc

type WaitFunc func(done <-chan struct{}) <-chan struct{}

WaitFunc creates a channel that receives an item every time a test should be executed and is closed when the last test should be invoked.

Jump to

Keyboard shortcuts

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