timer

package
v1.14.0-fuji Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: BSD-3-Clause Imports: 11 Imported by: 117

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateETA deprecated added in v1.7.4

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

EstimateETA calculates ETA from start time and current progress.

Deprecated: use EtaTracker instead

func ProgressFromHash added in v1.10.4

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 added in v1.11.11

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 added in v0.8.0

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 added in v0.8.0

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 added in v1.7.11

func NewAdaptiveTimeoutManager(
	config *AdaptiveTimeoutConfig,
	reg prometheus.Registerer,
) (AdaptiveTimeoutManager, error)

type EtaTracker

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

EtaTracker tracks the ETA of a job

func NewEtaTracker

func NewEtaTracker(maxSamples uint8, slowdownFactor float64) *EtaTracker

NewEtaTracker creates a new EtaTracker with the given maximum number of samples and a slowdown factor. The slowdown factor is a multiplier that is added to the ETA based on the percentage completed.

The adjustment works as follows:

  • At 0% progress: ETA is multiplied by slowdownFactor
  • At 100% progress: ETA is the raw estimate (no adjustment)
  • Between 0% and 100%: Adjustment decreases linearly with progress

Example: With slowdownFactor = 2.0:

  • At 0% progress: ETA = raw_estimate * 2.0
  • At 50% progress: ETA = raw_estimate * 1.5
  • At 100% progress: ETA = raw_estimate * 1.0

If maxSamples is less than 1, it will default to 5

func (*EtaTracker) AddSample

func (t *EtaTracker) AddSample(completed uint64, target uint64, timestamp time.Time) (*time.Duration, float64)

AddSample adds a sample to the EtaTracker It returns the remaining time to complete the target and the percent complete The returned values are rounded to the nearest second and 2 decimal places respectively This function can return a nil time.Duration indicating that there are not yet enough samples to calculate an accurate ETA.

The first sample should be at 0% progress to establish a baseline

type Meter

type Meter interface {
	// Notify this meter of a new event for it to rate
	Tick()
	// Return the number of events this meter is currently tracking
	Ticks() int
}

Meter tracks the number of occurrences of a specified 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 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