tokenbucket

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PriorityTokenBucket

type PriorityTokenBucket interface {
	// GetToken attempts to take count tokens from the
	// bucket with that priority. Priority 0 is highest.
	// Returns true on success, false
	// otherwise along with the duration for the next refill
	GetToken(priority, count int) (bool, time.Duration)
}

PriorityTokenBucket is the interface for rate limiter with priority

func NewFullPriorityTokenBucket

func NewFullPriorityTokenBucket(numOfPriority, rps int, timeSource clock.TimeSource) PriorityTokenBucket

NewFullPriorityTokenBucket creates and returns a new priority token bucket with all bucket init with full tokens. With all buckets full, get tokens from low priority buckets won't be missed initially, but may caused bursts.

func NewPriorityTokenBucket

func NewPriorityTokenBucket(numOfPriority, rps int, timeSource clock.TimeSource) PriorityTokenBucket

NewPriorityTokenBucket creates and returns a new token bucket rate limiter support priority. There are n buckets for n priorities. It replenishes the top priority bucket every 100 milliseconds, unused tokens flows to next bucket. The idea comes from Dual Token Bucket Algorithms. Thread safe.

@param numOfPriority

Number of priorities

@param rps

Desired rate per second

type TokenBucket

type TokenBucket interface {
	// TryConsume attempts to take count tokens from the
	// bucket. Returns true on success, false
	// otherwise along with the duration for the next refill
	TryConsume(count int) (bool, time.Duration)
	// Consume waits up to timeout duration to take count
	// tokens from the bucket. Returns true if count
	// tokens were acquired before timeout, false
	// otherwise
	Consume(count int, timeout time.Duration) bool
}

TokenBucket is the interface for any implementation of a token bucket rate limiter

func New

func New(rps int, timeSource clock.TimeSource) TokenBucket

New creates and returns a new token bucket rate limiter that replenishes the bucket every 100 milliseconds. Thread safe.

@param rps

Desired rate per second

Golang.org has an alternative implementation of the rate limiter. On benchmarking, golang's implementation was order of magnitude slower. In addition, it does a lot more than what we need. These are the benchmarks under different scenarios

BenchmarkTokenBucketParallel 50000000 40.7 ns/op BenchmarkGolangRateParallel 10000000 150 ns/op BenchmarkTokenBucketParallel-8 20000000 124 ns/op BenchmarkGolangRateParallel-8 10000000 208 ns/op BenchmarkTokenBucketParallel 50000000 37.8 ns/op BenchmarkGolangRateParallel 10000000 153 ns/op BenchmarkTokenBucketParallel-8 10000000 129 ns/op BenchmarkGolangRateParallel-8 10000000 208 ns/op

func NewDynamicTokenBucket added in v0.5.9

func NewDynamicTokenBucket(rps dynamicconfig.IntPropertyFn, timeSource clock.TimeSource) TokenBucket

NewDynamicTokenBucket creates and returns a token bucket rate limiter that supports dynamic change of RPS. Thread safe. @param rps

Dynamic config function for rate per second

Jump to

Keyboard shortcuts

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