ratebucket

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package ratebucket is the canonical mutex-guarded token bucket shared by the rate-limiters that previously each carried a byte-identical private copy of this algorithm (securex.Bucket, mcpx's bucket, httpx's tokenBucket) — QG-046/QG-059/PERF-0044. Consumers that need different semantics keep their own type: httpx.PeerLimiter (per-IP map + TTL eviction), htpx.rateBucket (lock-free int64/atomic), and wsx.Limiter (sliding-window connection limiter) are deliberately NOT folded in — they are different algorithms, not copies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket is a classic token bucket: tokens refill at `rate` per second up to a ceiling of `burst`, and each admitted call consumes one token. Safe for concurrent use.

func New

func New(rate, burst float64) *Bucket

New creates a bucket pre-filled to burst with the given rate and burst.

func (*Bucket) Allow

func (b *Bucket) Allow() bool

Allow returns true if a token is available and consumes one.

func (*Bucket) AllowAndSnapshot

func (b *Bucket) AllowAndSnapshot(ctx context.Context) (allowed bool, snap Snap)

AllowAndSnapshot performs the admit decision and captures the snapshot under a single lock acquisition. A canceled context short-circuits to denied without taking the lock (the caller has already blown its deadline).

func (*Bucket) Snapshot

func (b *Bucket) Snapshot() (int, int, time.Time)

Snapshot returns an approximate (limit, remaining, resetTime) for headers without consuming a token.

type Snap

type Snap struct {
	Limit     int
	Remaining int
	Reset     time.Time
}

Snap is the value form of (limit, remaining, resetTime).

Jump to

Keyboard shortcuts

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