Documentation
¶
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 token-bucket rate limiter with separate refill rate and burst capacity.
func NewBucket ¶
NewBucket creates a token bucket that refills at ratePerSec tokens/second and allows bursts up to burstMax tokens.
func (*Bucket) LastCheck ¶
LastCheck returns the last time the bucket was accessed. Used for staleness checks.
func (*Bucket) RetryAfterSecs ¶
RetryAfterSecs returns the estimated seconds until a token becomes available. Always returns at least 1.
func (*Bucket) TryConsume ¶
TryConsume attempts to consume one token. Returns true if the token was available, false if the bucket is empty.
type BucketMap ¶
type BucketMap[K comparable] struct { // contains filtered or unexported fields }
BucketMap is a sharded, concurrent map of token buckets keyed by K. Each shard has an independent lock, eliminating global contention.
func NewBucketMap ¶
func NewBucketMap[K comparable](maxPerShard int) *BucketMap[K]
NewBucketMap creates a sharded bucket map. maxPerShard caps entries per shard to prevent unbounded growth (0 means no cap).
func (*BucketMap[K]) Cleanup ¶
Cleanup removes entries that haven't been accessed for staleAfter duration.
func (*BucketMap[K]) GetOrCreate ¶
GetOrCreate returns the bucket for key, creating one if it doesn't exist.
func (*BucketMap[K]) StartCleanupLoop ¶
StartCleanupLoop runs periodic cleanup in a background goroutine. Stops when ctx is cancelled or Stop is called.