tickers

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 2 Imported by: 0

README

Tickers

CI Go Report Card GoDoc codecov Go Version License

An implementation of an exponential Ticker - tickers.Exponential with a similar interface to time.Ticker but providing an exponentially increasing delay with optional Jitter.

This may be useful in itself but its main purpose is to demonstrate asynchronous testing using the testing/synctest package.

About synctest

Introduced in Go 1.25 the testing/synctest package allows test code to use the standard time package in a deterministic way, without waiting for real time to pass and without flakiness while background goroutines do their work.

The clock is mocked by wrapping the test function with synctest.Test. Calling synctest.Wait ensures that goroutines started inside the test complete their work.

Documentation

Overview

Package tickers provides specialized ticker implementations with configurable timing behaviors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exponential

type Exponential struct {
	// C is the channel on which the ticks are delivered.
	C chan time.Time
	// contains filtered or unexported fields
}

Exponential is a ticker which provides a channel sending time values with an exponentially increasing interval.

func NewExponential

func NewExponential(initialDuration time.Duration, factor float64, opts ...ExponentialOption) *Exponential

NewExponential creates and starts a new exponential ticker with the given initial duration and exponential factor. The ticker will send the current time on its channel at exponentially increasing intervals. The first tick occurs after initialDuration, the second after initialDuration * factor, the third after initialDuration * factor^2, and so on. Optional ExponentialOption functions can be provided to configure the ticker (e.g., WithJitter).

func (*Exponential) Stop

func (e *Exponential) Stop()

Stop stops the exponential ticker and releases associated resources. After calling Stop, no more ticks will be sent on the ticker's channel.

type ExponentialOption

type ExponentialOption func(*Exponential)

ExponentialOption is a function which can be provided to the NewExponential function to configure the exponential ticker.

func WithJitter

func WithJitter(jitter time.Duration) ExponentialOption

WithJitter returns an ExponentialOption that adds random jitter to each interval. The jitter is a random duration between 0 and the provided jitter value that is added to each interval.

Jump to

Keyboard shortcuts

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