Documentation
¶
Overview ¶
Package ratelimit provides adaptive concurrency control (AIMD) and a circuit breaker. The engine uses these to bound DB load and fail fast when the database is unhealthy. Implementations use only the standard library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCircuitOpen = errors.New("ratelimit: circuit open")
ErrCircuitOpen is returned when the breaker is open.
var ErrRateLimited = errors.New("ratelimit: concurrency limit reached")
ErrRateLimited is returned when concurrency exceeds the current adaptive limit.
Functions ¶
This section is empty.
Types ¶
type Adaptive ¶
type Adaptive struct {
// contains filtered or unexported fields
}
Adaptive implements AIMD concurrency control: additive increase on success, multiplicative decrease on error or slow response. The limit moves between min and max in response to observed DB health.
func NewAdaptive ¶
NewAdaptive returns an Adaptive limiter. initial is the starting concurrency limit; it moves within [min, max]. rttThreshold is the latency above which a response is considered "slow" and triggers a decrease.
func (*Adaptive) Acquire ¶
Acquire reserves a concurrency slot. It returns ErrRateLimited if the current concurrency is at the limit.
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is a simple circuit breaker: after threshold consecutive failures it opens for cooldown, then allows a half-open probe.
func NewBreaker ¶
NewBreaker returns a Breaker that opens after threshold consecutive failures, staying open for cooldown.
func (*Breaker) Allow ¶
Allow returns ErrCircuitOpen if the breaker is open and the cooldown has not elapsed.
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket is a bounded in-process request-rate limiter.
func NewTokenBucket ¶
func NewTokenBucket(rate float64) *TokenBucket
NewTokenBucket returns nil when rate is non-positive.
func (*TokenBucket) Allow ¶
func (b *TokenBucket) Allow() error
Allow consumes one token or returns ErrRateLimited.