ratelimit

package
v6.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ratelimit implements a rate limiter to avoid calling APIs too often.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

type RateLimiter interface {
	RateLimit(ctx gocontext.Context, name string, maxCalls uint64, per time.Duration) (bool, error)
}

RateLimiter checks if a call can be let through and returns true if it can.

The name should be the same for all calls that should be affected by the same rate limit. The maxCalls and per arguments must be the same for all calls that use the same name, otherwise the behaviour is undefined.

The rate limiter lets through maxCalls calls in a window of time specified by the "per" argument. Note that the window is not sliding, so if you say 10 calls per minute, and 10 calls happen in the first second, no further calls will be let through for another 59 seconds.

The actual call should only be made if (true, nil) is returned. If (false, nil) is returned, it means that the number of requests in the time window is met, and you should sleep for a bit and try again.

In case an error happens, (false, err) is returned.

func NewNullRateLimiter

func NewNullRateLimiter() RateLimiter

NewNullRateLimiter creates a valid RateLimiter that always lets all requests through immediately.

func NewRateLimiter

func NewRateLimiter(redisURL string, prefix string, dynamicConfig bool, dynamicConfigCacheTTL time.Duration) RateLimiter

NewRateLimiter creates a RateLimiter that's backed by Redis. The prefix can be used to allow multiple rate limiters with the same name on the same Redis server.

Notes

Bugs

  • The Redis rate limiter is known to let through too many requests when there are many clients talking to the same Redis. The reason for this is unknown, but it's probably wise to limit the number of clients to 5 or 6 for the time being.

Jump to

Keyboard shortcuts

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