limit

package
v0.0.0-...-8223eb1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TokenBucket

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

TokenBucket implements the token bucket rate-limiting algorithm. Once configured with its parameters, users can get the current number of tokens in the bucket or attempt to use some tokens (non-blocking).

func NewTokenBucket

func NewTokenBucket(
	initCount int64,
	maxCount int64,
	refillRate time.Duration,
) (*TokenBucket, error)

NewTokenBucket creates a new token bucket. The first parameters specify the inital and maximum token counts of this bucket. One token is added every `refillRate` intervals.

func (*TokenBucket) Count

func (b *TokenBucket) Count() int64

Count retrieves the number of tokens currently in the bucket.

func (*TokenBucket) TryUse

func (b *TokenBucket) TryUse(count int64) (int64, bool)

TryUse attempts to take the given number of tokens from the bucket in a non-blocking manner. The return value shows the current count of tokens and whether the operation succeeded (if it didn't succeed, the token count is unchanged).

Jump to

Keyboard shortcuts

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