Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Factory ¶
type Factory interface {
CreateTokenBucket(rps int, timeSource clock.TimeSource) TokenBucket
}
Factory is an interface mainly used for injecting mock implementation of TokenBucket for unit testing
func NewFactory ¶
func NewFactory() Factory
NewFactory creates an instance of factory used for creating TokenBucket instances
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 // Reset resets the token bucket rps limit to the given value Reset(rps int) }
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