rate

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: Apache-2.0 Imports: 5 Imported by: 5

Documentation

Overview

Package rate provides a rate limiter to rate limit requests that can be burstable but they should only allowed N per a period defined. This package differs from the "golang.org/x/time/rate" package as it does not implement the token bucket algorithm.

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
}

Limiter is used to limit the number of operations done.

func NewLimiter

func NewLimiter(interval time.Duration, b int64) *Limiter

NewLimiter returns a new Limiter that allows events up to b tokens during the given interval. This Limiter has a different implementation from the 'x/time/rate's Limiter implementation. 'x/time/rate.Limiter' sends a constant stream of updates (at a rate of few dozen events per second) over the period of a N minutes which is the behavior of the token bucket algorithm. It is designed to flatten bursts in a signal to a fixed output rate. This rate.Limiter does the opposite of 'x/time/rate.Limiter'. It takes a somewhat fixed-rate stream of updates and turns it into a stream of controlled small bursts every N minutes.

func (*Limiter) Allow

func (lim *Limiter) Allow() bool

Allow is shorthand for AllowN(1).

func (*Limiter) AllowN

func (lim *Limiter) AllowN(n int64) bool

AllowN returns true if it's possible to allow n tokens.

func (*Limiter) Stop

func (lim *Limiter) Stop()

Stop stops the internal components used for the rate limiter logic.

func (*Limiter) Wait

func (lim *Limiter) Wait(ctx context.Context) error

Wait is shorthand for WaitN(ctx, 1).

func (*Limiter) WaitN

func (lim *Limiter) WaitN(ctx context.Context, n int64) error

WaitN acquires n tokens, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the limiter unchanged.

If ctx is already done, WaitN may still succeed without blocking.

Jump to

Keyboard shortcuts

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