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 ¶
const ( DefaultThreshold = 3 DefaultWindow = 60 * time.Second DefaultCooldown = 5 * time.Minute )
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 ¶
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
}
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 ¶
Expire puts back every member whose cooldown has ended, and calls apply when any came back or an earlier apply failed.
func (*Cooler) Filter ¶
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 ¶
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.
type Cooling ¶
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
}
Event is one failed upstream attempt. Target is in user names (provider and model as the user wrote them).