Documentation
¶
Index ¶
- Variables
- func NewKeyFailoverTransport(inner http.RoundTripper, config KeyFailoverConfig) http.RoundTripper
- func NewStateCollector(pools func() []*Pool) prometheus.Collector
- func ParseRetryAfter(resp *http.Response) time.Duration
- type Error
- type ErrorKind
- type Key
- type KeyFailoverConfig
- type KeyState
- type Pool
- type Walker
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoKeys is returned when the input is empty. ErrNoKeys = xerrors.New("no keys provided") // ErrDuplicateKey is returned when the input contains // duplicate key values. ErrDuplicateKey = xerrors.New("duplicate key") )
Configuration validation type errors. These surface when the pool is built from invalid input.
Functions ¶
func NewKeyFailoverTransport ¶
func NewKeyFailoverTransport(inner http.RoundTripper, config KeyFailoverConfig) http.RoundTripper
NewKeyFailoverTransport returns an http.RoundTripper backed by keyFailoverTransport. If config.Pool is nil, inner is returned unchanged.
func NewStateCollector ¶ added in v2.35.0
func NewStateCollector(pools func() []*Pool) prometheus.Collector
NewStateCollector returns a collector reporting the number of keys in each state, per provider.
func ParseRetryAfter ¶
ParseRetryAfter extracts the cooldown duration from response headers. It prefers the OpenAI-specific "retry-after-ms" header (milliseconds) over the standard "Retry-After" header (seconds). Returns zero if neither header is present or parseable. The HTTP-date form of "Retry-After" is not parsed.
Types ¶
type Error ¶
Error is returned when no key is available for the current attempt. RetryAfter is the soonest remaining cooldown across the pool.
type ErrorKind ¶
type ErrorKind int
ErrorKind classifies a runtime key-pool failure.
const ( // ErrorKindRateLimited means no key is currently available // but at least one key will recover after a cooldown. ErrorKindRateLimited ErrorKind = iota // ErrorKindPermanent means every key is permanently marked // and no key can satisfy the request. ErrorKindPermanent // cooldown triggered by an authentication failure. ErrorKindUnauthorized )
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key holds a key value and its runtime state.
func (*Key) Hint ¶
Hint returns a masked, identifiable fragment of the key, suitable for logs and persisted records.
func (*Key) MarkPermanent ¶
MarkPermanent marks the key as permanently unavailable. This is a terminal state. Returns true if this call transitions the key to permanent.
func (*Key) MarkTemporary ¶
MarkTemporary marks the key unavailable for the given cooldown. Returns true on the valid -> temporary transition.
type KeyFailoverConfig ¶
type KeyFailoverConfig struct {
// Pool is the key pool to walk. Nil disables key failover.
Pool *Pool
Logger slog.Logger
// IsBYOK returns true when the request already carries
// user-supplied auth. BYOK requests skip key failover.
IsBYOK func(*http.Request) bool
// InjectAuthKey writes the key value into the outbound headers
// in the format the provider expects.
InjectAuthKey func(*http.Header, string)
// BuildKeyPoolResponse renders the response sent to the client
// when the walker has no more keys to try.
BuildKeyPoolResponse func(*Error) *http.Response
}
KeyFailoverConfig is the per-provider configuration consumed by NewKeyFailoverTransport.
type KeyState ¶
type KeyState string
KeyState represents the current state of a key in the pool.
const ( // KeyStateValid means the key is available for use. KeyStateValid KeyState = "valid" // KeyStateTemporary means the key is temporarily unavailable // (e.g. rate-limited) and will recover after a cooldown. KeyStateTemporary KeyState = "temporary" // KeyStatePermanent means the key is permanently unavailable // (e.g. revoked or unauthorized) until process restart. KeyStatePermanent KeyState = "permanent" )
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool manages a set of keys with state tracking and cooldown expiry. It is safe for concurrent use.
func New ¶
New creates a pool from the given keys, labeled by providerName in its metrics and logs. All keys start in the valid state. Returns ErrNoKeys if keys is empty and ErrDuplicateKey if any key appears more than once.
func (*Pool) MarkKeyOnStatus ¶ added in v2.35.0
func (p *Pool) MarkKeyOnStatus( ctx context.Context, key *Key, resp *http.Response, logger slog.Logger, ) bool
MarkKeyOnStatus marks key based on a key-specific HTTP status code from resp (429 or 401 for temporary). Returns true if the status was a key-specific failover trigger so callers can retry with the next key.
func (*Pool) PoolState ¶
PoolState returns a snapshot of each key's state in the pool's original order, used by tests and other diagnostic callers. Use Walker for the failover iteration path.
func (*Pool) RecordAttempts ¶ added in v2.35.0
RecordAttempts records the total number of keys tried across an interception. Each upstream request uses its own walker, so the total sums the attempts across those per-request walkers. Call it once when the interception finishes.
type Walker ¶
type Walker struct {
// contains filtered or unexported fields
}
Walker traverses a Pool for a single request. Each request creates its own walker so that it can independently iterate through keys without interfering with other requests.