throttling

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 12 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

func New

func New(options ...Option) (*Limiter, error)

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, cost, rate, window int64, key string) (
	bool, func(context.Context) error, error,
)

Allow returns true if the limit is not exceeded, false otherwise.

func (*Limiter) AllowAfter added in v0.26.0

func (l *Limiter) AllowAfter(ctx context.Context, cost, rate, window int64, key string) (
	bool, time.Duration, func(context.Context) error, error,
)

AllowAfter returns true if the limit is not exceeded, false otherwise. Additionally, it returns the time.Duration until the next allowed request.

type Option

type Option func(*Limiter)

Option is a functional option for the limiter, see With* functions for reference

func WithInMemoryGCRA

func WithInMemoryGCRA(burst int64) Option

WithInMemoryGCRA allows to use the GCRA algorithm (in-memory) with the specified burst or with the burst as rate if the provided burst is <= 0

func WithRedisGCRA

func WithRedisGCRA(rc *redis.Client, burst int64) Option

WithRedisGCRA allows to use the GCRA algorithm (Redis version) with the specified burst or with the burst as rate if the provided burst is <= 0

func WithRedisSortedSet

func WithRedisSortedSet(rc *redis.Client) Option

WithRedisSortedSet allows to use the Redis SortedSet algorithm for rate limiting

func WithStatsCollector

func WithStatsCollector(sc statsCollector) Option

WithStatsCollector allows to setup a stats collector for the limiter

Jump to

Keyboard shortcuts

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