timers

package
v0.0.0-...-dc38f7b Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package timers provides a Clock abstraction useful for simulating timeouts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock[TimeoutType comparable] interface {
	// Zero returns a reset Clock. TimeoutAt channels will use the point
	// at which Zero was called as their reference point.
	Zero() Clock[TimeoutType]

	// Since returns the time spent between the last time the clock was zeroed out and the current
	// wall clock time.
	Since() time.Duration

	// TimeoutAt returns a channel that fires delta time after Zero was called.
	// timeoutType is specifies the reason for this timeout. If there are two
	// timeouts of the same type at the same time, then only one of them fires.
	// If delta has already passed, it returns a closed channel.
	//
	// TimeoutAt must be called after Zero; otherwise, the channel's behavior is
	// undefined.
	TimeoutAt(delta time.Duration, timeoutType TimeoutType) <-chan time.Time

	// Encode serializes the Clock into a byte slice.
	Encode() []byte

	// Decode deserializes the Clock from a byte slice.
	// A Clock which has been Decoded from an Encoded Clock should produce
	// the same timeouts as the original Clock.
	Decode([]byte) (Clock[TimeoutType], error)
}

Clock provides timeout events which fire at some point after a point in time.

func MakeFrozenClock

func MakeFrozenClock[TimeoutType comparable]() Clock[TimeoutType]

MakeFrozenClock creates a new frozen clock.

func MakeMonotonicClock

func MakeMonotonicClock[TimeoutType comparable](zero time.Time) Clock[TimeoutType]

MakeMonotonicClock creates a new monotonic clock with a given zero point.

type Frozen

type Frozen[TimeoutType comparable] struct {
	// contains filtered or unexported fields
}

Frozen is a dummy frozen clock that never fires.

func (*Frozen[TimeoutType]) Decode

func (m *Frozen[TimeoutType]) Decode([]byte) (Clock[TimeoutType], error)

Decode implements Clock.Decode.

func (*Frozen[TimeoutType]) Encode

func (m *Frozen[TimeoutType]) Encode() []byte

Encode implements Clock.Encode.

func (*Frozen[TimeoutType]) Since

func (m *Frozen[TimeoutType]) Since() time.Duration

Since implements the Clock interface.

func (*Frozen[TimeoutType]) String

func (m *Frozen[TimeoutType]) String() string

func (*Frozen[TimeoutType]) TimeoutAt

func (m *Frozen[TimeoutType]) TimeoutAt(delta time.Duration, timeoutType TimeoutType) <-chan time.Time

TimeoutAt returns a channel that will signal when the duration has elapsed.

func (*Frozen[TimeoutType]) Zero

func (m *Frozen[TimeoutType]) Zero() Clock[TimeoutType]

Zero returns a new Clock reset to the current time.

type Monotonic

type Monotonic[TimeoutType comparable] struct {
	// contains filtered or unexported fields
}

Monotonic uses the system's monotonic clock to emit timeouts.

func (*Monotonic[TimeoutType]) Decode

func (m *Monotonic[TimeoutType]) Decode(data []byte) (Clock[TimeoutType], error)

Decode implements Clock.Decode.

func (*Monotonic[TimeoutType]) Encode

func (m *Monotonic[TimeoutType]) Encode() []byte

Encode implements Clock.Encode.

func (*Monotonic[TimeoutType]) Since

func (m *Monotonic[TimeoutType]) Since() time.Duration

Since returns the time that has passed between the time the clock was last zeroed out and now

func (*Monotonic[TimeoutType]) String

func (m *Monotonic[TimeoutType]) String() string

func (*Monotonic[TimeoutType]) TimeoutAt

func (m *Monotonic[TimeoutType]) TimeoutAt(delta time.Duration, timeoutType TimeoutType) <-chan time.Time

TimeoutAt returns a channel that will signal when the duration has elapsed.

func (*Monotonic[TimeoutType]) Zero

func (m *Monotonic[TimeoutType]) Zero() Clock[TimeoutType]

Zero returns a new Clock reset to the current time.

Jump to

Keyboard shortcuts

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