token_bucket

package
v0.0.0-...-77fbaad Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit int64

type RateLimiter

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

RateLimiter controls how frequently events are allowed to happen.

RateLimiter has two main method: Allow and Wait

If the count of token in bucket is greater or equal to the 1 in a period time, Allow returns false

If the count of token in bucket is greater or equal to the limit in a period time, Wait blocks until the count of token in bucket is less than the 1 or its associated context.Context is canceled.

func NewRateLimiter

func NewRateLimiter(client *redis.Client, limit Limit, actionKey string, period time.Duration) *RateLimiter

NewRateLimiter returns a new RateLimiter that add a token into the bucket every period and the size of its bucket is limit

func (*RateLimiter) Allow

func (limiter *RateLimiter) Allow(ctx context.Context) bool

Allow is shorthand for AllowN(ctx, 1).

func (*RateLimiter) AllowN

func (limiter *RateLimiter) AllowN(ctx context.Context, n int64) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip action that exceed the rate limit. Otherwise use Wait.

func (*RateLimiter) Wait

func (limiter *RateLimiter) Wait(ctx context.Context) bool

Wait is shorthand for WaitN(ctx, 1).

func (*RateLimiter) WaitN

func (limiter *RateLimiter) WaitN(ctx context.Context, n int64) bool

WaitN blocks until lim permits n events to happen.

Jump to

Keyboard shortcuts

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