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
- Variables
- func RetryAfter(value string, now time.Time) time.Duration
- type Config
- type Cooler
- func (c *Cooler) Admit(chain []alias.Target) (probe alias.Target, allowed bool)
- func (c *Cooler) CompleteProbe(ctx context.Context, t alias.Target, code int, retry time.Duration) error
- func (c *Cooler) EndKeyCooling(ctx context.Context, id string) error
- func (c *Cooler) Expire(ctx context.Context) error
- func (c *Cooler) FailProbe(ctx context.Context, t alias.Target, code int, retry time.Duration) error
- func (c *Cooler) Filter(chain []alias.Target) []alias.Target
- func (c *Cooler) HasBreakers() bool
- func (c *Cooler) KeyFailures(id string, now time.Time) (n int, last time.Time)
- func (c *Cooler) KeyLive(id string) bool
- func (c *Cooler) KeySnapshot() []KeyCooling
- func (c *Cooler) Lift(ctx context.Context, t alias.Target, observedAt time.Time) error
- func (c *Cooler) Observe(ctx context.Context, e Event) error
- func (c *Cooler) Restore(ctx context.Context, storage Storage) error
- func (c *Cooler) Run(ctx context.Context, rows <-chan bifrost.Log, ...) error
- func (c *Cooler) Snapshot() []Cooling
- type Cooling
- type Event
- type KeyBook
- type KeyCooling
- type Storage
Constants ¶
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.
const DefaultTick = 5 * time.Second
DefaultTick is how often Run puts back members whose cooldown ended, when Run is given no tick.
Variables ¶
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 ¶
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
// ProviderWide reports whether a 402 on provider cools every model of that provider (Target{provider, "*"}).
ProviderWide func(provider string) bool
// Keys enables per-key failure handling from attempt trails. Nil keeps member-only cooling.
Keys KeyBook
}
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
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) EndKeyCooling ¶ added in v0.12.0
EndKeyCooling ends any active cooldown on key id, records the reset time so stale in-flight failures from before the recovery/secret rotation cannot re-disable it, and reconciles when state changed.
func (*Cooler) Expire ¶
Expire restores members and provider keys 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 ¶
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
HasBreakers lets the proxy skip compiling a request's full alias chain on the normal path.
func (*Cooler) KeyFailures ¶ added in v0.12.0
KeyFailures returns the failed attempt count for key id in the last 24 hours and the timestamp of the newest one.
func (*Cooler) KeyLive ¶ added in v0.12.0
KeyLive reports whether provider key id is not currently cooling.
func (*Cooler) KeySnapshot ¶ added in v0.12.0
func (c *Cooler) KeySnapshot() []KeyCooling
KeySnapshot lists the cooling provider keys, sorted by key ID.
func (*Cooler) Lift ¶ added in v0.10.0
Lift removes a cooling entry for t when it started before observedAt (or observedAt is zero), persists the change, and recompiles aliases.
func (*Cooler) Observe ¶
Observe records an upstream failure and recompiles the aliases if it changes which members are cooling.
func (*Cooler) Restore ¶ added in v0.7.0
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 fetches details for error rows and for rotated rows (NumberOfRetries > 0 when key routing is enabled); a failed fetch is logged and the row skipped. Every tick (DefaultTick when tick ≤ 0) it puts back members and keys 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.
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
ErrorCode string // upstream error.code when present
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
Seen time.Time // when handle received the row from the tail; zero for probes
EndedAt time.Time // when the request finished (Timestamp + Latency); zero when unknown
DetailDur time.Duration // GetLog duration; zero when not fetched from the tail
}
Event is one failed upstream attempt. Target is in user names (provider and model as the user wrote them).
func EventFrom ¶
EventFrom turns a front-door log row, with its error details, into an Event. It reports false unless the row is an upstream error: 404 minimum_discount_not_met, 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 KeyBook ¶ added in v0.12.0
type KeyBook interface {
LiveProviderKeys(ctx context.Context, provider string) ([]string, error)
DisableProviderKey(ctx context.Context, id, reason string) error
}
KeyBook reads and disables provider keys for per-key failure handling. *store.Store satisfies it.
type KeyCooling ¶ added in v0.12.0
KeyCooling is one provider key temporarily kept out of rotation.