Documentation
¶
Overview ¶
Package jitter provides functionality for generating durations and tickers that deviate from true periodicity within specified bounds.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Ticker ¶
type Ticker struct {
C <-chan time.Time // The channel on which the ticks are delivered.
// contains filtered or unexported fields
}
A Ticker holds a channel that delivers `ticks' of a clock at intervals, deviating with constrained random jitter.
It adjusts the intervals or drops ticks to make up for slow receivers.
func NewTicker ¶
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument, but adjusted with random jitter based on the specified scaling factor.
The duration d must be greater than zero; and the scaling factor f must be within the range 0 < f <= 1.0, or NewTicker will panic.
Stop the ticker to release associated resources.
Example ¶
package main
import (
"fmt"
"time"
"github.com/mroth/jitter"
)
func main() {
// ticker with base duration of 10 milliseconds and 0.5 scaling factor
ticker := jitter.NewTicker(10*time.Millisecond, 0.5)
defer ticker.Stop()
prev := time.Now()
for i := 0; i < 5; i++ {
t := <-ticker.C // time elapsed is random in range (5ms, 15ms).
fmt.Println("Time elapsed since last tick: ", t.Sub(prev))
prev = t
}
}
func NewTickerWithContext ¶
NewTickerWithContext is identical to NewTicker but also takes a specified context. If this context is cancelled, the Ticker will automatically Stop.