Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Breaker ¶
type Breaker struct {
Down int32
// contains filtered or unexported fields
}
Breaker helps manage back-off in case of a resource checked by prober is unavailable.
func (*Breaker) FlagDown ¶
func (b *Breaker) FlagDown()
FlagDown is used to indicate the resource is down.
CompareAndSwap atomically transitions the Down flag exactly once per up->down edge, so backoff state (resetTime, resetDuration) is updated once per trip even under concurrent FlagDown calls. The atomic write is also synchronized with IsUp's atomic.LoadInt32.
func (*Breaker) FlagUp ¶
func (b *Breaker) FlagUp()
FlagUp is used to reset the backoff.
Uses CompareAndSwap so the resetDuration reset only fires on an actual down->up transition (not on idempotent FlagUp calls), and so the write to b.Down is atomic with respect to IsUp's atomic.LoadInt32. The resetDuration write is performed under the mutex so it cannot race with FlagDown's resetDuration *= 2 (lost-update bug).
type LatencyBreaker ¶ added in v0.20.0
type LatencyBreaker struct {
// Configuration. Set at construction; not mutated after.
LatestThreshold time.Duration
RollingThreshold time.Duration
RollingWindow time.Duration
KConsecutive int
PassThroughFraction float64
// contains filtered or unexported fields
}
LatencyBreaker is a state machine that sheds traffic when observed request latencies exceed configured thresholds. It is independent of (and parallel to) the connection-failure-based Breaker.
Detection:
- latest: the most recent observation. Compared against LatestThreshold.
- rolling: average over a sliding window. Compared against RollingThreshold.
State transitions:
- OFF -> ON on every observation where latest > LatestThreshold OR rolling > RollingThreshold (zero-valued thresholds are skipped, so a single threshold can be used by leaving the other zero).
- ON -> OFF after K consecutive observations satisfy latest < LatestThreshold AND rolling < RollingThreshold.
While ON, IsUp() returns true with probability PassThroughFraction and false otherwise -- letting a small fraction of traffic through to drive recovery sensing without committing real load.
Concurrency:
State (Down) is read with atomic.LoadInt32 in IsUp() and updated via atomic.CompareAndSwapInt32 from Observe(). Compound state (latest / rolling buckets / consecutiveOK) is protected by a mutex; only one Observe can mutate at a time. IsUp does not block.
Random number source for pass-through is math/rand/v2 top-level, which is concurrent-safe and lock-free in Go 1.22+. A test seam (randFloat) allows deterministic tests.
func NewLatencyBreaker ¶ added in v0.20.0
func NewLatencyBreaker( latestThreshold, rollingThreshold, rollingWindow time.Duration, kConsecutive int, passThroughFraction float64, ) *LatencyBreaker
NewLatencyBreaker constructs a LatencyBreaker. Zero-valued thresholds disable that branch of the trip predicate. If both thresholds are zero, Observe is a no-op and IsUp always returns true (effectively disabled).
func (*LatencyBreaker) IsUp ¶ added in v0.20.0
func (lb *LatencyBreaker) IsUp() bool
IsUp returns true if the breaker is OFF (allowing all traffic), or true with probability PassThroughFraction if ON (allowing a small fraction through for recovery sensing).
func (*LatencyBreaker) Observe ¶ added in v0.20.0
func (lb *LatencyBreaker) Observe(latency time.Duration)
Observe records the latency of a completed request and advances the state machine. Called from the bidder client after each httpPost attempt completes (success or failure -- timeouts and errors count as observations and the elapsed time captured by the caller).
func (*LatencyBreaker) State ¶ added in v0.20.0
func (lb *LatencyBreaker) State() int32
State returns 0 (OFF / up) or 1 (ON / shedding). Primarily for tests.