clock

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package clock provides clock-related types and utilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoClockFunc = errors.New("no clock function provided")

ErrNoClockFunc is returned when creating a new Clock without a valid TimeFunc or NanotimeFunc.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// After waits for the duration to elapse and then sends the current time
	// on the returned channel. It is equivalent to NewTimer(d).C. The
	// underlying [Timer] is not recovered by the garbage collector until the
	// it fires. If efficiency is a concern, use [NewTimer] instead and call
	// [Timer.Stop] if the timer is no longer needed.
	After(d time.Duration) <-chan time.Time

	// AfterFunc waits for the duration to elapse and then calls fn in its own
	// goroutine. It returns a [Timer] that can be used to cancel the call using
	// its [Timer.Stop] method.
	AfterFunc(d time.Duration, fn func()) *Timer

	// Nanotime returns the current time in nanoseconds.
	Nanotime() int64

	// NewStopwatch returns a new [Stopwatch] that uses the [Clock] for
	// measuring time.
	NewStopwatch() *Stopwatch

	// NewTicker returns a new [Ticker] containing a channel that will send the
	// current time on the channel after each tick. The period of the ticks is
	// specified by the duration argument. The ticker will adjust the time
	// interval or drop ticks to make up for slow receivers. The duration d
	// must be greater than zero; if not, NewTicker will panic. Stop the ticker
	// to release associated resources.
	NewTicker(d time.Duration) *Ticker

	// NewTimer creates a new [Timer] that will send the current time on its
	// channel after at least d has elapsed.
	NewTimer(d time.Duration) *Timer

	// Now returns the current time. For wall clocks, this is the local time;
	// for monotonic clocks, this is the system's monotonic time. Other Clock
	// implementations may have different locale or clock time semantics.
	Now() time.Time

	// Since returns the time elapsed since t. It is shorthand for
	// Now().Sub(t).
	Since(t time.Time) time.Duration

	// Since returns the time elapsed since ns. It is shorthand for
	// Nanotime()-ns.
	SinceNanotime(ns int64) time.Duration

	// Sleep pauses the current goroutine for at least the duration d. A
	// negative or zero duration causes Sleep to return immediately.
	Sleep(d time.Duration)

	// Tick is a convenience wrapper for [NewTicker] providing access to the
	// ticking channel only. While Tick is useful for clients that have no need
	// to shut down the [Ticker], be aware that without a way to shut it down
	// the underlying Ticker cannot be recovered by the garbage collector; it
	// "leaks". Like [NewTicker], Tick will panic if d <= 0.
	Tick(time.Duration) <-chan time.Time
}

A Clock tells time.

func MustClock

func MustClock(clock Clock, err error) Clock

MustClock panics if the given error is not nil, otherwise it returns the given Clock.

func NewClock

func NewClock(opts ...Option) (Clock, error)

NewClock returns a new Clock based on the given options.

func NewMonotonicClock

func NewMonotonicClock() Clock

NewMonotonicClock returns a new monotonic Clock.

func NewWallClock

func NewWallClock() Clock

NewWallClock returns a new wall Clock.

type FakeClock

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

A FakeClock is a manually-adjusted clock useful for mocking the flow of time in tests. It does not keep time by itself: use FakeClock.Add, FakeClock.SetTime, and related functions to manage the clock's time.

func NewFakeClock

func NewFakeClock() *FakeClock

NewFakeClock creates a new FakeClock.

func (*FakeClock) Add

func (c *FakeClock) Add(d time.Duration)

Add adds d to the clock's internal time.

func (*FakeClock) After

func (c *FakeClock) After(d time.Duration) <-chan time.Time

After returns a channel that receives the current time after d has elapsed.

func (*FakeClock) AfterFunc

func (c *FakeClock) AfterFunc(d time.Duration, fn func()) *Timer

AfterFunc returns a timer that will invoke the given function after d has elapsed. The timer may be stopped and reset.

func (*FakeClock) Nanotime

func (c *FakeClock) Nanotime() int64

Nanotime returns the clock's internal time as integer nanoseconds.

func (*FakeClock) NewStopwatch added in v0.2.0

func (c *FakeClock) NewStopwatch() *Stopwatch

NewStopwatch returns a new Stopwatch that uses the current clock for measuring time. The clock's current time is used as the stopwatch's epoch.

func (*FakeClock) NewTicker

func (c *FakeClock) NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker that receives time ticks every d. If d is not greater than zero, [NewTicker] will panic.

func (*FakeClock) NewTimer

func (c *FakeClock) NewTimer(d time.Duration) *Timer

NewTimer returns a new Timer that receives a time tick after d.

func (*FakeClock) Now

func (c *FakeClock) Now() time.Time

Now returns the clock's internal time as a time.Time.

func (*FakeClock) SetNanotime

func (c *FakeClock) SetNanotime(ns int64)

SetNanotime sets the clock's time to ns.

func (*FakeClock) SetTime

func (c *FakeClock) SetTime(t time.Time)

SetTime sets the clock's time to t.

func (*FakeClock) Since

func (c *FakeClock) Since(t time.Time) time.Duration

Since returns the amount of time that elapsed between the clock's internal time and t.

func (*FakeClock) SinceNanotime

func (c *FakeClock) SinceNanotime(ns int64) time.Duration

SinceNanotime returns the amount of time that elapsed between the clock's internal time and ns.

func (*FakeClock) Sleep

func (c *FakeClock) Sleep(d time.Duration)

Sleep blocks for d.

Note that Sleep must be called from a different goroutine than the clock's time is being managed on, or the program will deadlock.

func (*FakeClock) Tick

func (c *FakeClock) Tick(d time.Duration) <-chan time.Time

Tick returns a new channel that receives time ticks every d. It is equivalent to writing c.NewTicker(d).C(). The given duration must be greater than 0.

type NanotimeFunc

type NanotimeFunc = func() int64

A NanotimeFunc is a function that returns time as integer nanoseconds.

func DefaultNanotimeFunc

func DefaultNanotimeFunc() NanotimeFunc

DefaultNanotimeFunc returns a new NanotimeFunc that uses chrono.Nanotime to tell time.

func DefaultWallNanotimeFunc added in v0.4.0

func DefaultWallNanotimeFunc() NanotimeFunc

DefaultWallNanotimeFunc returns a new, default NanotimeFunc that reports wall time as nanoseconds.

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Clock.

func WithNanotimeFunc

func WithNanotimeFunc(f NanotimeFunc) Option

WithNanotimeFunc returns an Option that configures a Clock to use f as its time function.

func WithTimeFunc

func WithTimeFunc(f TimeFunc) Option

WithTimeFunc returns an Option that configures a Clock to use f as its time function.

type Options

type Options struct {
	// TimeFunc configures the [TimeFunc] for a [Clock].
	// If both TimeFunc and NanotimeFunc are provided, NanotimeFunc is used.
	TimeFunc TimeFunc
	// NanotimeFunc configures the [NanotimeFunc] for a [Clock].
	// If both TimeFunc and NanotimeFunc are provided, NanotimeFunc is used.
	NanotimeFunc NanotimeFunc
}

Options configure a Clock.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns a new Options with sane defaults.

type Stopwatch

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

A Stopwatch measures elapsed time. A Stopwatch is created by calling [Clock.NewStopwatch].

func (*Stopwatch) Elapsed

func (s *Stopwatch) Elapsed() time.Duration

Elapsed returns the time elapsed since the last call to Stopwatch.Reset.

func (*Stopwatch) Reset

func (s *Stopwatch) Reset() time.Duration

Reset resets the stopwatch to zero, returning the elapsed time since the last call to Reset.

type ThrottledClock

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

ThrottledClock provides a simple interface to memoize repeated time syscalls within a given threshold.

func NewThrottledClock

func NewThrottledClock(
	nowfn NanotimeFunc,
	interval time.Duration,
) *ThrottledClock

NewThrottledClock creates a new ThrottledClock that uses the given NanoFunc to update its internal time at the given interval. A ThrottledClock should be stopped via ThrottledClock.Stop once it is no longer used.

Note that interval should be tuned to be greater than the actual frequency of calls to ThrottledClock.Nanos or ThrottledClock.Now (otherwise the clock will generate more time calls than it is saving).

func NewThrottledMonotonicClock

func NewThrottledMonotonicClock(interval time.Duration) *ThrottledClock

NewThrottledMonotonicClock creates a new ThrottledClock that uses NewMonotonicNanoFunc as its backing time function. See NewThrottledClock for more information.

func NewThrottledWallClock

func NewThrottledWallClock(interval time.Duration) *ThrottledClock

NewThrottledWallClock creates a new ThrottledClock that uses NewWallNanoFunc as its backing time function. See NewThrottledClock for more information.

func (*ThrottledClock) After

func (c *ThrottledClock) After(d time.Duration) <-chan time.Time

After returns a channel that receives the current time after d has elapsed. This method is not throttled and uses Go's runtime timers.

func (*ThrottledClock) AfterFunc

func (c *ThrottledClock) AfterFunc(d time.Duration, fn func()) *Timer

AfterFunc returns a timer that will invoke the given function after d has elapsed. The timer may be stopped and reset. This method is not throttled and uses Go's runtime timers.

func (*ThrottledClock) Interval

func (c *ThrottledClock) Interval() time.Duration

Interval returns the interval at which the clock updates its internal time.

func (*ThrottledClock) Nanotime

func (c *ThrottledClock) Nanotime() int64

Nanotime returns the current time as integer nanoseconds.

func (*ThrottledClock) NewStopwatch added in v0.2.0

func (c *ThrottledClock) NewStopwatch() *Stopwatch

NewStopwatch returns a new Stopwatch that uses the current clock for measuring time. The clock's current time is used as the stopwatch's epoch.

func (*ThrottledClock) NewTicker

func (c *ThrottledClock) NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker that receives time ticks every d. This method is not throttled and uses Go's runtime timers. If d is not greater than zero, NewTicker will panic.

func (*ThrottledClock) NewTimer

func (c *ThrottledClock) NewTimer(d time.Duration) *Timer

NewTimer returns a new Timer that receives a time tick after d. This method is not throttled and uses Go's runtime timers.

func (*ThrottledClock) Now

func (c *ThrottledClock) Now() time.Time

Now returns the current time as time.Time.

func (*ThrottledClock) Since

func (c *ThrottledClock) Since(t time.Time) time.Duration

Since returns the amount of time that elapsed between the clock's internal time and t.

func (*ThrottledClock) SinceNanotime

func (c *ThrottledClock) SinceNanotime(ns int64) time.Duration

SinceNanotime returns the amount of time that elapsed between the clock's internal time and ns.

func (*ThrottledClock) Sleep

func (c *ThrottledClock) Sleep(d time.Duration)

Sleep puts the current goroutine to sleep for d. This method is not throttled and uses Go's runtime timers.

func (*ThrottledClock) Stop

func (c *ThrottledClock) Stop()

Stop stops the clock. Note that this has no effect on currently-running timers.

func (*ThrottledClock) Tick

func (c *ThrottledClock) Tick(d time.Duration) <-chan time.Time

Tick returns a new channel that receives time ticks every d. It is equivalent to writing c.NewTicker(d).C().

type Ticker

type Ticker struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

A Ticker is functionally equivalent to a time.Ticker. A Ticker must be created by [Clock.NewTicker].

func (*Ticker) Reset

func (t *Ticker) Reset(d time.Duration)

Reset stops a ticker and resets its period to the specified duration. The next tick will arrive after the new period elapses. The duration d must be greater than zero; if not, Reset will panic.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".

type TimeFunc

type TimeFunc = func() time.Time

A TimeFunc is a function that returns time as a time.Time object.

func DefaultTimeFunc

func DefaultTimeFunc() TimeFunc

DefaultTimeFunc returns a new TimeFunc that uses time.Now to tell time.

type Timer

type Timer struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

A Timer is functionally equivalent to a time.Timer. A Timer must be created by [Clock.NewTimer].

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.

See Reset documentation on time.Timer for more information.

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.

See Stop documentation on time.Timer for more information.

Directories

Path Synopsis
Package clockmock is a generated GoMock package.
Package clockmock is a generated GoMock package.

Jump to

Keyboard shortcuts

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