Back to / pkg / util / async

Package async

Latest Go to latest

The latest major version is .

Published: Oct 14, 2020 | License: Apache-2.0 | Module:


type BoundedFrequencyRunner

type BoundedFrequencyRunner struct {
	// contains filtered or unexported fields

BoundedFrequencyRunner manages runs of a user-provided function. See NewBoundedFrequencyRunner for examples.

func NewBoundedFrequencyRunner

func NewBoundedFrequencyRunner(name string, fn func(), minInterval, maxInterval time.Duration, burstRuns int) *BoundedFrequencyRunner

NewBoundedFrequencyRunner creates a new BoundedFrequencyRunner instance, which will manage runs of the specified function.

All runs will be async to the caller of BoundedFrequencyRunner.Run, but multiple runs are serialized. If the function needs to hold locks, it must take them internally.

Runs of the function will have at least minInterval between them (from completion to next start), except that up to bursts may be allowed. Burst runs are "accumulated" over time, one per minInterval up to burstRuns total. This can be used, for example, to mitigate the impact of expensive operations being called in response to user-initiated operations. Run requests that would violate the minInterval are coallesced and run at the next opportunity.

The function will be run at least once per maxInterval. For example, this can force periodic refreshes of state in the absence of anyone calling Run.


NewBoundedFrequencyRunner("name", fn, time.Second, 5*time.Second, 1) - fn will have at least 1 second between runs - fn will have no more than 5 seconds between runs

NewBoundedFrequencyRunner("name", fn, 3*time.Second, 10*time.Second, 3) - fn will have at least 3 seconds between runs, with up to 3 burst runs - fn will have no more than 10 seconds between runs

The maxInterval must be greater than or equal to the minInterval, If the caller passes a maxInterval less than minInterval, this function will panic.

func (*BoundedFrequencyRunner) Loop

func (bfr *BoundedFrequencyRunner) Loop(stop <-chan struct{})

Loop handles the periodic timer and run requests. This is expected to be called as a goroutine.

func (*BoundedFrequencyRunner) RetryAfter

func (bfr *BoundedFrequencyRunner) RetryAfter(interval time.Duration)

RetryAfter ensures that the function will run again after no later than interval. This can be called from inside a run of the BoundedFrequencyRunner's function, or asynchronously.

func (*BoundedFrequencyRunner) Run

func (bfr *BoundedFrequencyRunner) Run()

Run the function as soon as possible. If this is called while Loop is not running, the call may be deferred indefinitely. If there is already a queued request to call the underlying function, it may be dropped - it is just guaranteed that we will try calling the underlying function as soon as possible starting from now.

type Runner

type Runner struct {
	// contains filtered or unexported fields

Runner is an abstraction to make it easy to start and stop groups of things that can be described by a single function which waits on a channel close to exit.

func NewRunner

func NewRunner(f ...func(stop chan struct{})) *Runner

NewRunner makes a runner for the given function(s). The function(s) should loop until the channel is closed.

func (*Runner) Start

func (r *Runner) Start()

Start begins running.

func (*Runner) Stop

func (r *Runner) Stop()

Stop stops running.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier