cooling

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cooling takes alias members that keep failing out of their chains for a while. A 402 cools a member at once; repeated 429s or transient failures also cool it. A cooling member is left out of its compiled chains. A member that tripped the circuit breaker returns only after a successful probe.

Index

Constants

View Source
const (
	DefaultThreshold = 3
	DefaultWindow    = 60 * time.Second
	DefaultCooldown  = 5 * time.Minute
	// DefaultSubscriptionCooldown is shorter: a subscription member stands for a pool of accounts that the OAuth engine
	// rotates and cools itself, so the member as a whole is back within seconds.
	DefaultSubscriptionCooldown = 30 * time.Second
)

Defaults for the zero fields of Config.

View Source
const DefaultTick = 5 * time.Second

DefaultTick is how often Run puts back members whose cooldown ended, when Run is given no tick.

Variables

View Source
var ErrFeedClosed = errors.New("cooling: log feed closed")

ErrFeedClosed means the log feed Run reads was closed, for example because the feed dropped a subscriber that fell behind. The caller subscribes again and calls Run again.

Functions

func RetryAfter added in v0.7.0

func RetryAfter(value string, now time.Time) time.Duration

RetryAfter parses a probe response's Retry-After, as delta-seconds or an HTTP date.

Types

type Config

type Config struct {
	Threshold int           // 429s within Window that cool a member
	Window    time.Duration // how close together those 429s must be
	Default   time.Duration // cooldown when the upstream sent no retry hint
	// SubscriptionDefault replaces Default for a subscription member's failure without a retry hint.
	SubscriptionDefault time.Duration
}

Config tunes when a member cools and for how long. Zero fields take the defaults above.

type Cooler

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

Cooler holds which members are cooling. apply recompiles the aliases (it reads Filter), and runs whenever the set of cooling members changes.

func New

func New(cfg Config, apply func(context.Context) error, now func() time.Time, log *slog.Logger) *Cooler

New returns a Cooler. A nil now uses time.Now; a nil log discards.

func (*Cooler) Admit added in v0.7.0

func (c *Cooler) Admit(chain []alias.Target) (probe alias.Target, allowed bool)

Admit reserves one expired breaker for a probe. With no probe, the request can use the normal chain only if it has a member outside the breaker; credit and rate-limit cooldowns retain their existing fallback behavior.

func (*Cooler) CompleteProbe added in v0.7.0

func (c *Cooler) CompleteProbe(ctx context.Context, t alias.Target, code int, retry time.Duration) error

CompleteProbe closes the breaker on success, or reopens it with backoff on an upstream failure. A canceled request or client-side error is inconclusive and lets the next request probe.

func (*Cooler) Expire

func (c *Cooler) Expire(ctx context.Context) error

Expire restores members cooling on credit or rate limits and retries pending changes. An expired breaker still needs a successful probe.

func (*Cooler) FailProbe added in v0.7.0

func (c *Cooler) FailProbe(ctx context.Context, t alias.Target, code int, retry time.Duration) error

FailProbe reopens the breaker for an explicit upstream failure in an otherwise successful response.

func (*Cooler) Filter

func (c *Cooler) Filter(chain []alias.Target) []alias.Target

Filter returns chain without its cooling members. When no live member remains, prefer the legacy cooldowns to open breakers. If every member is behind a breaker, keep the rule installed; Admit gates the request.

func (*Cooler) HasBreakers added in v0.7.0

func (c *Cooler) HasBreakers() bool

HasBreakers lets the proxy skip compiling a request's full alias chain on the normal path.

func (*Cooler) Observe

func (c *Cooler) Observe(ctx context.Context, e Event) error

Observe records an upstream failure and recompiles the aliases if it changes which members are cooling.

func (*Cooler) Restore added in v0.7.0

func (c *Cooler) Restore(ctx context.Context, storage Storage) error

Restore loads persisted cooldowns and tripped breakers before the first reconcile and before serving requests.

func (*Cooler) Run

func (c *Cooler) Run(ctx context.Context, rows <-chan bifrost.Log, detail func(context.Context, string) (bifrost.Log, error), tick time.Duration) error

Run follows the shared log feed until ctx is done or rows is closed. It ignores non-errors and fetches details only for failures; a failed fetch is logged and the row skipped. Every tick (DefaultTick when tick ≤ 0) it puts back members whose cooldown ended.

Rows that started before Run did are skipped: the feed replays its recent past when it starts, and a failure from before a restart already had its cooldown, so it must not start a new full one. Call Run before /v1 opens.

func (*Cooler) Snapshot

func (c *Cooler) Snapshot() []Cooling

Snapshot lists the cooling members, soonest back first.

type Cooling

type Cooling struct {
	Target  alias.Target
	Until   time.Time
	Reason  string
	Backoff time.Duration // nonzero for a tripped breaker; the base delay before its next probe
}

Cooling is one member out of its chains. Until is when it can return or be probed; Reason is the status code.

type Event

type Event struct {
	ID         string
	At         time.Time // when the request started
	Target     alias.Target
	StatusCode int
	RetryAfter time.Duration // 0 when the upstream gave no hint
	// Subscription marks a failure of a subscription member, served through the OAuth engine.
	Subscription bool
}

Event is one failed upstream attempt. Target is in user names (provider and model as the user wrote them).

func EventFrom

func EventFrom(l bifrost.Log) (Event, bool)

EventFrom turns a front-door log row, with its error details, into an Event. It reports false unless the row is an upstream error: 402, 429, or 5xx (including request_timed_out/504 and provider_connection_failed/502). The front door records Retry-After separately from the error body, including for timed 503s and OAuth pool 429s.

type Storage added in v0.7.0

type Storage interface {
	LoadCooling(context.Context, time.Time) ([]Cooling, error)
	SaveCooling(context.Context, []Cooling) error
}

Storage persists the active cooldowns, not the recent failure counts or in-flight probes.

Jump to

Keyboard shortcuts

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