golib: github.com/levenlabs/golib/ratelimiter Index | Files

package ratelimiter

import "github.com/levenlabs/golib/ratelimiter"

Package ratelimiter implements generic rate-limiting algorithms

Index

Package Files

ratelimiter.go

func TokenBucketTake Uses

func TokenBucketTake(rc util.Cmder, key string, interval time.Duration, maxTokens, toTake int64) (int64, error)

TokenBucketTake implements a token bucket rate-limiting system. Each bucket (identified by key) starts with some amount of tokens (maxTokens) in it. Whenever some action is performed tokens are removed from the bucket (toTake). If there are not enough tokens in the bucket then the action is rate-limited.

Tokens which were taken out of the bucket more than the given Duration previously are automatically returned to the bucket. If a bucket has never been used (i.e. key doesn't exist in redis) it is considered to be full.

Returns the number of tokens left in the bucket after toTake is removed, or a negative value if none are available to be taken. An error is only returned if communication with the datastore fails.

Package ratelimiter imports 2 packages (graph). Updated 2016-07-24. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).