bucket

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package bucket implements the token bucket algorithm backed by Redis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConsumeAll

func ConsumeAll(ctx context.Context, client redis.Cmdable, buckets []*TokenBucket) (int, error)

ConsumeAll atomically checks and consumes one token from each bucket. All buckets are checked first; tokens are only consumed if ALL buckets have sufficient tokens.

Returns the index of the first bucket that rejected (0-based), or -1 if all allowed.

Types

type TokenBucket

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

TokenBucket represents a single rate limit bucket backed by Redis.

func New

func New(namespace, serverName, suffix string, maxTokens int32, refillPeriod time.Duration) *TokenBucket

New creates a TokenBucket. The Redis key is derived from namespace, server name, and suffix (e.g., "global" or "global:tool:search").

func (*TokenBucket) RetryAfter

func (b *TokenBucket) RetryAfter() time.Duration

RetryAfter returns the minimum wait time for one token to become available.

Jump to

Keyboard shortcuts

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