Documentation ¶
Overview ¶
Package ratelimit provides rate limiting implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter interface { // Take takes the size of available resources(maybe one or more tokens for TokenBucketRateLimiter), // wait until resources available or ctx canceled. Take(ctx context.Context, size int) error // Close closes the RateLimiter, after that, Take will block until ctx canceled. Close() error }
RateLimiter is the abstraction for rate limiter.
func NewTokenBucketRateLimiter ¶
func NewTokenBucketRateLimiter(limit int) RateLimiter
NewTokenBucketRateLimiter creates a new token bucket RateLimiter.
NOTE: If the size you Take is too large or too small(compare to limit/500), it will not reach the max possible limit, except you only have few goroutines in your nonbusy system, if that matters, test it before using it.
QPS:
l := NewTokenBucketRateLimiter(1000) // 1000 queries per second defer l.Close() err := l.Take(ctx, 1) // take a token
BPS:
l := NewTokenBucketRateLimiter(200*(1<<20)) // 200MB per second defer l.Close() err := l.Take(ctx, 1<<20) // take 1MB
func NewUnlimiter ¶
func NewUnlimiter() RateLimiter
NewUnlimiter creates an unlimiter, use it with caution.
Click to show internal directories.
Click to hide internal directories.