ratelimiter

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_PRIORITY_MULTIPLIER = uint16(2)
	PUBLISH_PRIORITY_MULTIPLIER = uint16(4)
	DEFAULT_RATE_PER_MINUTE     = uint16(4000)
	DEFAULT_MAX_TOKENS          = uint16(20000)
	PUBLISH_RATE_PER_MINUTE     = uint16(600)
	PUBLISH_MAX_TOKENS          = uint16(3000)
	MAX_UINT_16                 = 65535

	DEFAULT LimitType = "DEF"
	PUBLISH LimitType = "PUB"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Buckets

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

func NewBuckets

func NewBuckets(log *zap.Logger, name string) *Buckets

type Entry

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

Entry represents a single wallet entry in the rate limiter

type Limit

type Limit struct {
	// Maximum number of tokens that can be accumulated
	MaxTokens uint16
	// Number of tokens to refill per minute
	RatePerMinute uint16
}

Limit controls token refilling for bucket entries

func (Limit) Refill

func (l Limit) Refill(entry *Entry, multiplier uint16)

type LimitType

type LimitType string

type RateLimiter

type RateLimiter interface {
	Spend(limitType LimitType, bucket string, cost uint16, isPriority bool) error
}

type TokenBucketRateLimiter

type TokenBucketRateLimiter struct {
	PriorityMultiplier        uint16
	PublishPriorityMultiplier uint16
	Limits                    map[LimitType]*Limit
	// contains filtered or unexported fields
}

TokenBucketRateLimiter implements the RateLimiter interface

func NewTokenBucketRateLimiter

func NewTokenBucketRateLimiter(ctx context.Context, log *zap.Logger) *TokenBucketRateLimiter

func (*TokenBucketRateLimiter) Janitor

func (rl *TokenBucketRateLimiter) Janitor(sweepInterval, expiresAfter time.Duration)

func (*TokenBucketRateLimiter) Spend

func (rl *TokenBucketRateLimiter) Spend(
	limitType LimitType,
	bucket string,
	cost uint16,
	isPriority bool,
) error

The Spend function takes a bucket and a boolean asserting whether to apply the PRIORITY or the REGULAR rate limits.

Jump to

Keyboard shortcuts

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