tokenbucket

package
v0.0.0-...-757fabd Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit struct {
	RefillPerSec  int
	WindowSeconds int
}

func LimitPerHour

func LimitPerHour(limit int) Limit

LimitPerHour creates a TokenLimit configuration for a rate limit that allows a specified number of tokens to be refilled per hour with a burst capacity equal to the refill rate. Example: LimitPerHour(18000) creates a TokenLimit allowing 18000 tokens per hour with a burst capacity of 18000.

func LimitPerMinute

func LimitPerMinute(limit int) Limit

LimitPerMinute creates a TokenLimit configuration for a rate limit that allows a specified number of tokens to be refilled per minute with a burst capacity equal to the refill rate. Example: LimitPerMinute(300) creates a TokenLimit allowing 300 tokens per minute with a burst capacity of 300.

func LimitPerSecond

func LimitPerSecond(limit int) Limit

LimitPerSecond creates a TokenLimit configuration for a rate limit that allows a specified number of tokens to be refilled per second with a burst capacity equal to the refill rate. Example: LimitPerSecond(5) creates a TokenLimit allowing 5 tokens per second with a burst capacity of 5.

type Limiter

type Limiter struct {
	Options Options
	// contains filtered or unexported fields
}

func NewLimiter

func NewLimiter(client pgxUniversalClient, options Options) *Limiter

func (*Limiter) ConsumeTokens

func (t *Limiter) ConsumeTokens(ctx context.Context, key string, limit Limit) (*Result, error)

type Options

type Options struct {
	// KeyPrefix is the prefix for keys managed by the limiter.
	// By default, it is set to "RATE_LIMIT", but it can be customized.
	KeyPrefix string
	// KeyCleanupInterval is the interval at which expired keys are cleaned up.
	// Default is 10 seconds.
	KeyCleanupInterval time.Duration
	// KeyCleanupBatchLimit is the maximum number of keys to delete per cleanup run.
	// Default is 10000.
	KeyCleanupBatchLimit int
}

Options holds the configuration settings for the LeakyBucketLimiter. It includes parameters for key prefix, cleanup intervals, and batch limits.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns an Options struct initialized with default values for limiter configuration settings. The default values are designed to provide a basic configuration suitable for many scenarios. Each field in the Options struct is set to a default value, as documented below:

Defaults:

  • KeyPrefix: The prefix used for keys managed by the limiter. Default is set to "RATE_LIMIT".
  • KeyCleanupInterval: The interval between cleanup operations for expired key cleanup. Default is 10 seconds.
  • KeyCleanupBatchLimit: The maximum number of keys to delete during each cleanup run. Default is 10000.

type Result

type Result struct {
	Limit  Limit
	Tokens int
}

Jump to

Keyboard shortcuts

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