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. A decrease answers the results of requests sent before it as well: those come back after it without decreasing again, so one overload halves the limit once.
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 circuit breaker: after threshold consecutive failures it opens for cooldown, then lets one probe through at a time (half-open) until a probe's result closes it, or reopens it.
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, or half-open with a probe already through. A request it lets through must end with Record, or with Release when its outcome says nothing of health; probe is nonzero for the half-open probe.
func (*Breaker) Record ¶
Record counts a request's outcome. A success closes the breaker; a failure reopens a half-open one, and opens a closed one at threshold consecutive failures.
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.