interval

package
v0.12.8 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedInterval

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

FixedInterval sends a timestamp on its channel at a fixed interval specified by delay.

func NewFixed

func NewFixed(delay time.Duration) *FixedInterval

NewFixed creates and initializes a new fixed interval.

func (*FixedInterval) GetDelay

func (t *FixedInterval) GetDelay() time.Duration

GetDelay returns the fixed interval duration.

func (*FixedInterval) Initialized

func (t *FixedInterval) Initialized() bool

func (*FixedInterval) Interrupt

func (t *FixedInterval) Interrupt() bool

Interrupt the current interval, stopping and starting it again. Returns true if the interval was running and is successfully reset, false if the ticker was stopped or uninitialized.

func (*FixedInterval) Running

func (t *FixedInterval) Running() bool

Running returns true if the timer exists and false otherwise.

func (*FixedInterval) Start

func (t *FixedInterval) Start() bool

Start the interval to periodically issue events. Returns true if the ticker gets started, false if it's already started or uninitialized.

func (*FixedInterval) Stop

func (t *FixedInterval) Stop() bool

Stop the interval so that no more events are dispatched. Returns true if the call stops the interval, false if already expired or never started.

type Interval

type Interval interface {
	Start() bool     // start the interval to periodically call its function
	Stop() bool      // stop the interval, the function will not be called
	Interrupt() bool // interrupt the interval, setting it to the next period
	Running() bool   // whether or not the interval is running
}

Interval is an interface that specifies the behavior of time based signals. An interval dispatches a single signal to its channel, C at the delay specified by the schedule, which can be either fixed or stochastic. Fixed intervals resechedule themselves for a fixed delay after all the signal has been dispatched. Stochastic intervals select a random delay in a configured range to schedule the next event.

Interval objects can be started and stopped. On start, the interval schedules the next event after the delay returned by GetDelay(). On stop no events will be dispatched by the handler. Intervals can be interrupted which resets the timer to a new delay. Timer state (running or not running) can be determined by the Running() method.

Intervals are not thread safe and should only be used from within a single thread.

type JitterInterval

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

JitterInterval sends a timestamp on its channel at a normally distributed random interval with a mean and standard deviation delays. The mean and the stddev must be greater than 0. When sampling the distribution, the interval tries 7 times to get a non-zero delay, otherwise it defaults to the mean. It's important to choose a distribution that is unlikely to sample values less than or equal to zero.

func NewJitter

func NewJitter(meanDelay, stddevDelay time.Duration) *JitterInterval

NewJitter creates and initializes a new normal random interval with mean and stddev.

func (*JitterInterval) GetDelay

func (t *JitterInterval) GetDelay() time.Duration

GetDelay returns a random delay with a normal distribution of mean delay and a standard deviation of stddev delay. This method tries 7 times to return a non-zero, non-negative delay then defaults to returning the mean.

func (*JitterInterval) Initialized

func (t *JitterInterval) Initialized() bool

func (*JitterInterval) Interrupt

func (t *JitterInterval) Interrupt() bool

Interrupt the current interval, stopping and starting it again. Returns true if the interval was running and is successfully reset, false if the ticker was stopped or uninitialized.

func (*JitterInterval) Running

func (t *JitterInterval) Running() bool

Running returns true if the timer exists and false otherwise.

func (*JitterInterval) Start

func (t *JitterInterval) Start() bool

Start the interval to periodically issue events. Returns true if the ticker gets started, false if it's already started or uninitialized.

func (*JitterInterval) Stop

func (t *JitterInterval) Stop() bool

Stop the interval so that no more events are dispatched. Returns true if the call stops the interval, false if already expired or never started.

type RandomInterval

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

RandomInterval sends a timestamp on its channel at a uniform random interval between the minimum and maximum delays. The maximum delay must be greater than the minimum.

func NewRandom

func NewRandom(minDelay, maxDelay time.Duration) *RandomInterval

NewRandom creates and initializes a new uniform random interval inside of a delay range.

func (*RandomInterval) GetDelay

func (t *RandomInterval) GetDelay() time.Duration

GetDelay returns a random integer in the range (minDelay, maxDelay) on every request for the delay, causing jitter so that no timeout occurs at the same time.

func (*RandomInterval) Initialized

func (t *RandomInterval) Initialized() bool

func (*RandomInterval) Interrupt

func (t *RandomInterval) Interrupt() bool

Interrupt the current interval, stopping and starting it again. Returns true if the interval was running and is successfully reset, false if the ticker was stopped or uninitialized.

func (*RandomInterval) Running

func (t *RandomInterval) Running() bool

Running returns true if the timer exists and false otherwise.

func (*RandomInterval) Start

func (t *RandomInterval) Start() bool

Start the interval to periodically issue events. Returns true if the ticker gets started, false if it's already started or uninitialized.

func (*RandomInterval) Stop

func (t *RandomInterval) Stop() bool

Stop the interval so that no more events are dispatched. Returns true if the call stops the interval, false if already expired or never started.

type Timer

type Timer interface {
	Initialized() bool
	GetDelay() time.Duration
}

All structs that embed interval must implement the Timer interface.

Jump to

Keyboard shortcuts

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