timer

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: BSD-3-Clause Imports: 10 Imported by: 8

Documentation

Overview

Package timer provides time utilities including mockable clocks

Index

Constants

This section is empty.

Variables

View Source
var MaxTime = time.Unix(1<<63-62135596801, 0)

MaxTime is the maximum representable time

Functions

func EstimateETA

func EstimateETA(startTime time.Time, progress, end uint64) time.Duration

EstimateETA estimates the remaining time to complete a task given the startTime and its current progress.

func ProgressFromHash

func ProgressFromHash(b []byte) uint64

ProgressFromHash returns the progress out of MaxUint64 assuming [b] is a key in a uniformly distributed sequence that is being iterated lexicographically.

func StoppedTimer

func StoppedTimer() *time.Timer

StoppedTimer returns a stopped timer so that there is no entry on the C channel (and there isn't one scheduled to be added).

This means that after calling Reset there will be no events on the channel until the timer fires (at which point there will be exactly one event sent to the channel).

It enables re-using the timer across loop iterations without needing to have the first loop iteration perform any == nil checks to initialize the first invocation.

Types

type AdaptiveTimeoutConfig

type AdaptiveTimeoutConfig struct {
	InitialTimeout time.Duration `json:"initialTimeout"`
	MinimumTimeout time.Duration `json:"minimumTimeout"`
	MaximumTimeout time.Duration `json:"maximumTimeout"`
	// Timeout is [timeoutCoefficient] * average response time
	// [timeoutCoefficient] must be > 1
	TimeoutCoefficient float64 `json:"timeoutCoefficient"`
	// Larger halflife --> less volatile timeout
	// [timeoutHalfLife] must be positive
	TimeoutHalflife time.Duration `json:"timeoutHalflife"`
}

AdaptiveTimeoutConfig contains the parameters provided to the adaptive timeout manager.

type AdaptiveTimeoutManager

type AdaptiveTimeoutManager interface {
	// Start the timeout manager.
	// Must be called before any other method.
	// Must only be called once.
	Dispatch()
	// Stop the timeout manager.
	// Must only be called once.
	Stop()
	// Returns the current network timeout duration.
	TimeoutDuration() time.Duration
	// Registers a timeout for the item with the given [id].
	// If the timeout occurs before the item is Removed, [timeoutHandler] is called.
	Put(id ids.RequestID, measureLatency bool, timeoutHandler func())
	// Remove the timeout associated with [id].
	// Its timeout handler will not be called.
	Remove(id ids.RequestID)
	// ObserveLatency manually registers a response latency.
	// We use this to pretend that it a query to a benched validator
	// timed out when actually, we never even sent them a request.
	ObserveLatency(latency time.Duration)
}

func NewAdaptiveTimeoutManager

func NewAdaptiveTimeoutManager(
	config *AdaptiveTimeoutConfig,
	registry metric.Registry,
) (AdaptiveTimeoutManager, error)

type Clock

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

Clock provides a mockable time source

func NewClock

func NewClock() *Clock

NewClock returns a new clock synced to real time

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set sets the clock to a fixed time (for testing)

func (*Clock) Sync

func (c *Clock) Sync()

Sync resets the clock to use real time

func (*Clock) Time

func (c *Clock) Time() time.Time

Time returns the current time

func (*Clock) Unix

func (c *Clock) Unix() uint64

Unix returns the current unix timestamp

func (*Clock) UnixTime

func (c *Clock) UnixTime() time.Time

UnixTime returns the current time truncated to seconds

type EtaTracker

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

EtaTracker provides exponentially weighted moving average ETA estimates

func NewEtaTracker

func NewEtaTracker(total uint64, alpha float64) *EtaTracker

NewEtaTracker creates a new ETA tracker with the given total and smoothing factor. Alpha should be between 0 and 1; higher values give more weight to recent observations.

func (*EtaTracker) Progress

func (e *EtaTracker) Progress() uint64

Progress returns the current progress.

func (*EtaTracker) Rate

func (e *EtaTracker) Rate() float64

Rate returns the current estimated rate per second.

func (*EtaTracker) Total

func (e *EtaTracker) Total() uint64

Total returns the total amount of work.

func (*EtaTracker) Update

func (e *EtaTracker) Update(progress uint64) time.Duration

Update records new progress and returns the estimated time remaining.

type Meter

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

Meter tracks event rates

func NewMeter

func NewMeter(halflife time.Duration) *Meter

NewMeter returns a new meter with the given halflife

func (*Meter) Count

func (m *Meter) Count() int64

Count returns the total count

func (*Meter) Rate

func (m *Meter) Rate() float64

Rate returns the current rate

func (*Meter) Tick

func (m *Meter) Tick()

Tick records an event

type Timer

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

Timer wraps a timer object. This allows a user to specify a handler. Once specifying the handler, the dispatch thread can be called. The dispatcher will only return after calling Stop. SetTimeoutIn will result in calling the handler in the specified amount of time.

func NewStagedTimer deprecated

func NewStagedTimer(f func() (time.Duration, bool)) *Timer

NewStagedTimer returns a timer that will execute [f] when a timeout occurs and execute an additional timeout after the returned duration if [f] returns true and some duration.

Deprecated: NewStagedTimer exists for historical compatibility and should not be used.

func NewTimer

func NewTimer(handler func()) *Timer

NewTimer creates a new timer object

func (*Timer) Cancel

func (t *Timer) Cancel()

Cancel the currently scheduled event

func (*Timer) Dispatch

func (t *Timer) Dispatch()

func (*Timer) SetTimeoutIn

func (t *Timer) SetTimeoutIn(duration time.Duration)

SetTimeoutIn will set the timer to fire the handler in [duration]

func (*Timer) Stop

func (t *Timer) Stop()

Stop this timer from executing any more.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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