cooling

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 14 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 within a window cool it too. A cooling member is left out when aliases are compiled, and put back when its cooldown ends. The state lives in memory only.

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

This section is empty.

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) Expire

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

Expire puts back every member whose cooldown has ended, and calls apply when any came back or an earlier apply failed.

func (*Cooler) Filter

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

Filter returns chain without its cooling members. When every member is cooling it returns chain unchanged, so an alias never loses its route.

func (*Cooler) Observe

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

Observe records one failed attempt. A 402 cools its member at once; a 429 cools it once Threshold of them fall within Window. Other status codes are ignored. When the set of cooling members changes, Observe calls apply and returns its error; the member stays cooling either way.

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 front door's log rows until ctx is done or rows is closed. For each error row it fetches the row's details and observes the event; 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
}

Cooling is one member that is out of its chains until Until. Reason is the status code that cooled it, "402" or "429".

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 error whose upstream status is 402 or 429. The target is in user names: a row of the OAuth engine's internal provider carries the "<name>/<model>" the user wrote.

Jump to

Keyboard shortcuts

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