interval

package
v0.0.0-...-f32f910 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 3 Imported by: 0

README

Interval

Better interval functionality that wraps time.Timer and provides an interface for time based event dispatchers.

Documentation

Overview

Package interval provides an interface for time based event dispatchers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedInterval

type FixedInterval struct {
	events.Dispatcher
	// contains filtered or unexported fields
}

FixedInterval dispatches it's internal event type on a routine period. It does that by wrapping a time.Timer object, adding the additional Interval functionality as well as the event dispatcher functionality.

func NewFixedInterval

func NewFixedInterval(delay time.Duration, etype events.Type, echan chan<- error) *FixedInterval

NewFixedInterval creates and initializes a new fixed interval.

func (*FixedInterval) GetDelay

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

GetDelay returns the fixed interval duration.

func (*FixedInterval) Init

func (t *FixedInterval) Init(delay time.Duration, etype events.Type, echan chan<- error)

Init the Fixed Interval with the specified delay

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) Register

func (t *FixedInterval) Register(callback events.Callback)

Register the specific event type with the callback

func (*FixedInterval) Running

func (t *FixedInterval) Running() bool

Running returns true if the timer exists and false otherwise (thred safe).

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
	GetDelay() time.Duration           // the duration of the current interval period
	Register(callback events.Callback) // register a handler for the interval event
}

Interval is an interface that specifies the behavior of time based event dispatchers. A single interval object dispatches a single event type, to which callbacks from any go routine can be registered. The event is dispatched on schedule - the interval can either be fixed or stochastic. Fixed intervals resechedule themselves for a fixed delay after all callbacks are called. Stochastic intervals select a random delay in a configured range to schedule the next event after all callbacks.

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.

type RandomInterval

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

RandomInterval dispatches its internal interval on a random period between the minimum and maximum delay values. Every event has a different delay.

func NewRandomInterval

func NewRandomInterval(minDelay, maxDelay time.Duration, etype events.Type, echan chan<- error) *RandomInterval

NewRandomInterval creates and initializes a new random interval.

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) Init

func (t *RandomInterval) Init(minDelay, maxDelay time.Duration, etype events.Type, echan chan<- error)

Init the random interval

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) 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.

Jump to

Keyboard shortcuts

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