hedge

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package hedge defines hedge triggers and latency tracking used by the retry executor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedDelayTrigger

type FixedDelayTrigger struct {
	Delay time.Duration
}

FixedDelayTrigger spawns a hedge after a fixed delay.

func (FixedDelayTrigger) ShouldSpawnHedge

func (t FixedDelayTrigger) ShouldSpawnHedge(state HedgeState) (bool, time.Duration)

type HedgeState

type HedgeState struct {
	// CallStart is when the overall operation (call) started.
	CallStart time.Time // Not strictly needed for fixed delay but good for future.
	// AttemptStart is when the current retry group (attempt 0 of this group) started.
	AttemptStart time.Time
	// AttemptsLaunched is the number of attempts already launched in this group.
	AttemptsLaunched int
	// MaxHedges is the maximum number of additional managed attempts (hedges) allowed.
	// Note: Total attempts in group = 1 (primary) + MaxHedges.
	MaxHedges int
	// Elapsed is the time elapsed since AttemptStart.
	Elapsed time.Duration
	// Snapshot contains the current latency statistics for the operation.
	Snapshot LatencySnapshot
	// HedgeDelay is the configured delay between hedges, if static.
	HedgeDelay time.Duration
}

HedgeState describes the current state of a retry group for hedging decisions.

type LatencySnapshot

type LatencySnapshot struct {
	P50 time.Duration
	P90 time.Duration
	P95 time.Duration
	P99 time.Duration
}

LatencySnapshot contains latency quantiles.

type LatencyTracker

type LatencyTracker interface {
	// Observe records a duration sample.
	Observe(d time.Duration)
	// Snapshot returns the current latency snapshot.
	Snapshot() LatencySnapshot
}

LatencyTracker tracks recent latency samples and calculates quantiles.

type LatencyTrigger

type LatencyTrigger struct {
	Percentile string // "p50", "p90", "p95", "p99"
}

LatencyTrigger spawns a hedge if the elapsed time exceeds a dynamic threshold.

func (LatencyTrigger) ShouldSpawnHedge

func (t LatencyTrigger) ShouldSpawnHedge(state HedgeState) (bool, time.Duration)

ShouldSpawnHedge checks if the hedge should be spawned based on latency stats.

type Registry

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

Registry manages named hedge triggers. It is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new, empty registry.

func (*Registry) Get

func (r *Registry) Get(name string) (Trigger, bool)

Get returns the trigger with the given name.

func (*Registry) Register

func (r *Registry) Register(name string, t Trigger)

Register adds a trigger to the registry. Panics if name is empty or trigger is nil.

type RingBufferTracker

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

RingBufferTracker implements LatencyTracker using a fixed-size ring buffer. It is safe for concurrent use.

func NewRingBufferTracker

func NewRingBufferTracker(size int) *RingBufferTracker

NewRingBufferTracker creates a new tracker with the specified size. Size must be greater than 0.

func (*RingBufferTracker) Observe

func (t *RingBufferTracker) Observe(d time.Duration)

Observe records a duration sample.

func (*RingBufferTracker) Snapshot

func (t *RingBufferTracker) Snapshot() LatencySnapshot

Snapshot returns the current latency snapshot.

type Trigger

type Trigger interface {
	// ShouldSpawnHedge returns true if a new hedge should be spawned.
	// nextCheckIn returns the duration to wait before checking again.
	// If nextCheckIn is 0, the executor uses a default enforcement interval.
	ShouldSpawnHedge(state HedgeState) (should bool, nextCheckIn time.Duration)
}

Trigger decides when to spawn a hedged attempt.

Jump to

Keyboard shortcuts

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