Documentation
¶
Index ¶
- func DefaultThresholds() (maxStreak int, maxRate float64)
- func InitTracker(maxErrorStreak int, maxErrorRate float64)
- type ChannelKeyStats
- type KeyHealth
- type KeyHealthStatus
- type KeyHealthTracker
- func (t *KeyHealthTracker) GetAllKeyHealth() []*KeyHealth
- func (t *KeyHealthTracker) GetBadKeys() []*KeyHealth
- func (t *KeyHealthTracker) GetChannelStats(channelID int) *ChannelKeyStats
- func (t *KeyHealthTracker) GetKeyHealth(channelID, keyIndex int) *KeyHealth
- func (t *KeyHealthTracker) IsKeyBad(channelID, keyIndex int) bool
- func (t *KeyHealthTracker) RecordError(channelID, channelType int, channelName string, keyIndex int, statusCode int, ...) bool
- func (t *KeyHealthTracker) RecordSuccess(channelID, channelType int, channelName string, keyIndex int)
- func (t *KeyHealthTracker) ResetKey(channelID, keyIndex int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultThresholds ¶
DefaultThresholds returns sensible auto-ban thresholds.
func InitTracker ¶
InitTracker initializes the global key health tracker.
Types ¶
type ChannelKeyStats ¶
type ChannelKeyStats struct {
ChannelID int `json:"channel_id"`
ChannelName string `json:"channel_name"`
TotalKeys int `json:"total_keys"`
OKKeys int `json:"ok_keys"`
BadKeys int `json:"bad_keys"`
DegradedKeys int `json:"degraded_keys"`
RateLimitKeys int `json:"rate_limit_keys"`
}
GetChannelStats returns aggregated health stats for all keys in a channel.
type KeyHealth ¶
type KeyHealth struct {
ChannelID int `json:"channel_id"`
ChannelName string `json:"channel_name"`
ChannelType int `json:"channel_type"`
KeyIndex int `json:"key_index"`
Status KeyHealthStatus `json:"status"`
TotalRequests int64 `json:"total_requests"`
TotalErrors int64 `json:"total_errors"`
LastErrorAt *time.Time `json:"last_error_at,omitempty"`
LastErrorMsg string `json:"last_error_msg,omitempty"`
LastSuccessAt *time.Time `json:"last_success_at,omitempty"`
ErrorStreak int `json:"error_streak"` // consecutive errors
SuccessStreak int `json:"success_streak"` // consecutive successes
}
KeyHealth tracks the health of a single API key.
type KeyHealthStatus ¶
type KeyHealthStatus int
KeyHealthStatus represents the health state of an individual API key.
const ( KeyHealthUnknown KeyHealthStatus = 0 // Never used KeyHealthOK KeyHealthStatus = 1 // Working normally KeyHealthDegraded KeyHealthStatus = 2 // Some errors, still usable KeyHealthBad KeyHealthStatus = 3 // Auth failure, should not be used KeyHealthRateLimited KeyHealthStatus = 4 // Being rate-limited )
type KeyHealthTracker ¶
type KeyHealthTracker struct {
// contains filtered or unexported fields
}
KeyHealthTracker monitors the health of API keys across all channels. It provides real-time key health data for dashboards and auto-ban decisions.
func GetTracker ¶
func GetTracker() *KeyHealthTracker
GetTracker returns the global key health tracker.
func (*KeyHealthTracker) GetAllKeyHealth ¶
func (t *KeyHealthTracker) GetAllKeyHealth() []*KeyHealth
GetAllKeyHealth returns health status for all tracked keys.
func (*KeyHealthTracker) GetBadKeys ¶
func (t *KeyHealthTracker) GetBadKeys() []*KeyHealth
GetBadKeys returns all keys marked as Bad.
func (*KeyHealthTracker) GetChannelStats ¶
func (t *KeyHealthTracker) GetChannelStats(channelID int) *ChannelKeyStats
GetChannelStats returns aggregated key health stats for a channel.
func (*KeyHealthTracker) GetKeyHealth ¶
func (t *KeyHealthTracker) GetKeyHealth(channelID, keyIndex int) *KeyHealth
GetKeyHealth returns health info for a specific key.
func (*KeyHealthTracker) IsKeyBad ¶
func (t *KeyHealthTracker) IsKeyBad(channelID, keyIndex int) bool
IsKeyBad returns true if the key is marked as Bad and should be skipped.
func (*KeyHealthTracker) RecordError ¶
func (t *KeyHealthTracker) RecordError(channelID, channelType int, channelName string, keyIndex int, statusCode int, errMsg string) bool
RecordError records a failed API call for a key and updates health status. Returns true if the key should be auto-banned.
func (*KeyHealthTracker) RecordSuccess ¶
func (t *KeyHealthTracker) RecordSuccess(channelID, channelType int, channelName string, keyIndex int)
RecordSuccess records a successful API call for a key.
func (*KeyHealthTracker) ResetKey ¶
func (t *KeyHealthTracker) ResetKey(channelID, keyIndex int)
ResetKey resets health state for a specific key (e.g., after manual fix).