cooldown

package
v0.0.0-...-30b4b00 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReasonRateLimit   = "rate_limit"   // 429 Too Many Requests
	ReasonServerError = "server_error" // 5xx errors
	ReasonAuthFailure = "auth_failure" // 401/403
	ReasonTimeout     = "timeout"      // Connection timeout
	ReasonManual      = "manual"       // Manual cooldown via API
)

Reason constants for cooldown triggers

Variables

This section is empty.

Functions

func Init

func Init(cfg *Config)

Init initializes the global cooldown manager.

Types

type Config

type Config struct {
	BaseCooldown      time.Duration // initial cooldown duration (default: 30s)
	MaxCooldown       time.Duration // maximum cooldown after exponential backoff (default: 10min)
	BackoffMultiplier float64       // how much to multiply on consecutive errors (default: 2.0)
	CleanupInterval   time.Duration // how often to clean expired entries (default: 1min)
}

Config holds the cooldown manager configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns sensible defaults.

type CooldownManager

type CooldownManager struct {
	// contains filtered or unexported fields
}

CooldownManager manages global cooldown periods for API providers and individual keys. When a provider hits rate limits (429), all requests to that provider are paused. Individual API keys can also be cooled down for auth failures.

func GetManager

func GetManager() *CooldownManager

GetManager returns the global cooldown manager.

func (*CooldownManager) ClearKeyCooldown

func (m *CooldownManager) ClearKeyCooldown(provider string, keyIndex int)

ClearKeyCooldown clears a specific key's cooldown.

func (*CooldownManager) ClearProviderCooldown

func (m *CooldownManager) ClearProviderCooldown(provider string)

ClearProviderCooldown clears a provider's cooldown.

func (*CooldownManager) GetAllCooldowns

func (m *CooldownManager) GetAllCooldowns() []CooldownState

GetAllCooldowns returns all active cooldown states.

func (*CooldownManager) GetProviderCooldownStatus

func (m *CooldownManager) GetProviderCooldownStatus(provider string) *CooldownState

GetProviderCooldownStatus returns cooldown info for a provider.

func (*CooldownManager) GetRemaining

func (m *CooldownManager) GetRemaining(provider string, keyIndex int) time.Duration

GetRemaining returns the remaining cooldown time for a provider/key combination. Returns 0 if not in cooldown.

func (*CooldownManager) HandleChannelError

func (m *CooldownManager) HandleChannelError(provider string, keyIndex int, statusCode int, isRateLimit bool) bool

HandleChannelError processes a channel error and determines if cooldown should be triggered. Returns true if cooldown was started.

func (*CooldownManager) HandleChannelSuccess

func (m *CooldownManager) HandleChannelSuccess(provider string)

HandleChannelSuccess resets cooldown error counts on success.

func (*CooldownManager) IsCooledDown

func (m *CooldownManager) IsCooledDown(provider string, keyIndex int) bool

IsCooledDown checks if a specific provider and optionally key is in cooldown. If any level (provider or key) is cooled down, returns true.

func (*CooldownManager) IsKeyCooledDown

func (m *CooldownManager) IsKeyCooledDown(provider string, keyIndex int) bool

IsKeyCooledDown checks if a specific API key is in cooldown.

func (*CooldownManager) IsProviderCooledDown

func (m *CooldownManager) IsProviderCooledDown(provider string) bool

IsProviderCooledDown checks if an entire provider is in cooldown. Returns true if the provider cannot accept any requests.

func (*CooldownManager) Shutdown

func (m *CooldownManager) Shutdown()

Shutdown stops the cleanup goroutine.

func (*CooldownManager) StartKeyCooldown

func (m *CooldownManager) StartKeyCooldown(provider string, keyIndex int, reason string)

StartKeyCooldown puts a specific API key into cooldown (e.g., for auth failures).

func (*CooldownManager) StartProviderCooldown

func (m *CooldownManager) StartProviderCooldown(provider string, reason string)

StartProviderCooldown puts an entire provider into cooldown. Uses exponential backoff based on previous error count.

type CooldownState

type CooldownState struct {
	Provider   string        `json:"provider"`
	KeyIndex   int           `json:"key_index,omitempty"` // -1 for provider-level
	Reason     string        `json:"reason"`
	StartedAt  time.Time     `json:"started_at"`
	ExpiresAt  time.Time     `json:"expires_at"`
	Duration   time.Duration `json:"duration"`
	ErrorCount int           `json:"error_count"` // consecutive errors since last success
}

CooldownState represents the current cooldown state for a provider or key.

Jump to

Keyboard shortcuts

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