Documentation
¶
Overview ¶
Package pool implements a concurrency-safe rotating pool of tokens with per-token health, cooldown, and JSON state persistence.
Index ¶
- type Lease
- type Options
- type Pool
- func (p *Pool) Entries() []Lease
- func (p *Pool) LoadState() error
- func (p *Pool) MarkChecked(id string, live bool)
- func (p *Pool) Penalize(id string, d time.Duration)
- func (p *Pool) Quarantine(id string, d time.Duration)
- func (p *Pool) RecordUse(id string, status int)
- func (p *Pool) Release(id string, ok bool)
- func (p *Pool) Save() error
- func (p *Pool) SetTokens(lines []string)
- func (p *Pool) SetValue(id, value string)
- func (p *Pool) Status() Snapshot
- func (p *Pool) Take() (Lease, bool)
- type Snapshot
- type Status
- type Token
- type TokenView
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
Rotation string // lru | round_robin | random (default lru)
Cooldown time.Duration // rest period applied on Take and on failed Release
StateFile string // JSON state file; empty disables persistence
Now func() time.Time
Rand *rand.Rand
}
Options configures a Pool. Now and Rand are injectable for tests.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a thread-safe token pool.
func (*Pool) LoadState ¶
LoadState applies persisted state onto the current tokens, matched by id. Call after SetTokens so values come from the tokens file.
func (*Pool) MarkChecked ¶
MarkChecked records a health-check outcome. A live result revives a token, including one in quarantine.
func (*Pool) Penalize ¶
Penalize keeps a token alive but rests it for d (on top of any current cooldown). Use for soft failures such as 429 or 5xx, where the key is fine but should back off briefly.
func (*Pool) Quarantine ¶
Quarantine pulls a token out of rotation for d after a hard failure (e.g. a 401/403 auth rejection). A subsequent passing health check revives it.
func (*Pool) RecordUse ¶
RecordUse accounts a request made with a token: it bumps the request count, records the last status, and counts anything >=400 (or 0 for a transport error) as an error.
func (*Pool) SetTokens ¶
SetTokens reconciles the pool with the given lines. Each non-empty, non-comment line is "id,value" or "value" (id derived from a value hash). Existing tokens keep their state; missing ones are dropped.
type Snapshot ¶
type Snapshot struct {
Total int `json:"total"`
Live int `json:"live"`
Dead int `json:"dead"`
Unknown int `json:"unknown"`
Quarantined int `json:"quarantined"`
Cooling int `json:"cooling"`
Tokens []TokenView `json:"tokens"`
}
Snapshot is an aggregate + per-token view of the pool.
type Token ¶
type Token struct {
ID string `json:"id"`
Value string `json:"-"`
Weight int `json:"weight"`
Status Status `json:"status"`
LastCheck time.Time `json:"last_check"`
LastUsed time.Time `json:"last_used"`
CoolingUntil time.Time `json:"cooling_until"`
Fails int `json:"fails"`
Requests int64 `json:"requests"`
Errors int64 `json:"errors"`
LastStatus int `json:"last_status"`
}
Token holds a value and its runtime state. Value is never persisted.
type TokenView ¶
type TokenView struct {
ID string `json:"id"`
Weight int `json:"weight"`
Status Status `json:"status"`
LastCheck time.Time `json:"last_check"`
LastUsed time.Time `json:"last_used"`
Cooling bool `json:"cooling"`
Fails int `json:"fails"`
Requests int64 `json:"requests"`
Errors int64 `json:"errors"`
LastStatus int `json:"last_status"`
}
TokenView is a persistence/reporting projection of a Token.