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) 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) 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 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
}
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) Expire ¶
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 ¶
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) 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 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.
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 ¶
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.