ratelimit

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
var ErrCircuitOpen = errors.New("ratelimit: circuit open")

ErrCircuitOpen is returned when the breaker is open.

View Source
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

func NewAdaptive(initial, min, max int64, rttThreshold time.Duration) *Adaptive

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

func (a *Adaptive) Acquire() error

Acquire reserves a concurrency slot. It returns ErrRateLimited if the current concurrency is at the limit.

func (*Adaptive) Limit

func (a *Adaptive) Limit() int64

Limit returns the current concurrency limit (for introspection/testing).

func (*Adaptive) OnResult

func (a *Adaptive) OnResult(err error, d time.Duration)

OnResult adjusts the limit: multiplicative decrease on error or slow RTT, additive increase otherwise.

func (*Adaptive) Release

func (a *Adaptive) Release()

Release frees a concurrency slot.

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

func NewBreaker(threshold int64, cooldown time.Duration) *Breaker

NewBreaker returns a Breaker that opens after threshold consecutive failures, staying open for cooldown.

func (*Breaker) Allow

func (b *Breaker) Allow() error

Allow returns ErrCircuitOpen if the breaker is open and the cooldown has not elapsed.

func (*Breaker) IsOpen

func (b *Breaker) IsOpen() bool

IsOpen reports whether the breaker is currently open.

func (*Breaker) Record

func (b *Breaker) Record(success bool)

Record updates failure count. A success resets it; a failure that crosses the threshold opens the breaker for cooldown.

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.

Jump to

Keyboard shortcuts

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