tokenbucket

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTokenBucketRateLimiter

func NewTokenBucketRateLimiter(ctx context.Context, client redis.Cmdable, key string, duration time.Duration,
	throughput int, maxCapacity int,
	batchSize int, opts ...Option) (ratelimit.Limiter, error)

Types

type Option

type Option func(*TokenBucketLimiter)

func WithAntiDDos

func WithAntiDDos(antiDDoS bool) Option

func WithEnablePreFetch

func WithEnablePreFetch(preFetch bool) Option

func WithPreFetchCount

func WithPreFetchCount(preFetchCount int64) Option

type TokenBucketLimiter

type TokenBucketLimiter struct {
	ratelimit.BaseRateLimiter

	N int64

	/*
		If the traffic is too large, the limiter will request Redis frequently.
		To avoid this situation, the frequency of accessing Redis will be limited.
	*/
	AntiDDoS bool

	/*
		Get the token in advance before actually needing to use it
	*/
	EnablePreFetch bool
	PreFetchCount  int64
	// contains filtered or unexported fields
}

func (*TokenBucketLimiter) PreFetch

func (r *TokenBucketLimiter) PreFetch()

func (*TokenBucketLimiter) Take

func (r *TokenBucketLimiter) Take(ctx context.Context) (bool, error)

func (*TokenBucketLimiter) Wait

func (r *TokenBucketLimiter) Wait(ctx context.Context) (err error)

wait until take a token or timeout

Jump to

Keyboard shortcuts

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