Documentation ¶
Index ¶
- Variables
- func AtLeast(n int64) func(int64) int64
- func Sha1Hash(s string) string
- func TimeGTE(from time.Time, target time.Time) bool
- type FixedTruncatedWindowArgs
- type FixedTruncatedWindowMemoryStorage
- type FixedTruncatedWindowRateLimiter
- type FixedWindowArgs
- type FixedWindowIncArgs
- type FixedWindowMemoryStorage
- type FixedWindowRateLimiter
- type FixedWindowRedisStorage
- type FixedWindowRedisStorageOpts
- type Rate
- type RealClock
- type Result
- type TestClock
- type TokenFixedWindowRateLimiter
Constants ¶
This section is empty.
Variables ¶
var ( ErrRateLimitExceeded = errors.New("rate limit exceeded") ErrTokensGreaterThanCapacity = errors.New("tokens are greater than capacity") ErrCannotLoadScript = errors.New("cannot load LUA script") )
var (
ScriptHash = Sha1Hash(script)
)
Functions ¶
Types ¶
type FixedTruncatedWindowMemoryStorage ¶ added in v0.1.0
type FixedTruncatedWindowMemoryStorage struct {
// contains filtered or unexported fields
}
FixedTruncatedWindowMemoryStorage is an in-memory storage for the rate limit state. Preferred option when testing and working with standalone instances of your program and do not care about it restarting and not being exactly compliant with the state of rate limits at the server
func NewFixedTruncatedWindowMemoryStorage ¶ added in v0.1.0
func NewFixedTruncatedWindowMemoryStorage() *FixedTruncatedWindowMemoryStorage
NewFixedTruncatedWindowMemoryStorage returns a new instance of FixedTruncatedWindowMemoryStorage
func (*FixedTruncatedWindowMemoryStorage) Inc ¶ added in v0.1.0
func (s *FixedTruncatedWindowMemoryStorage) Inc( ctx context.Context, args FixedWindowIncArgs, ) (int64, error)
type FixedTruncatedWindowRateLimiter ¶
type FixedTruncatedWindowRateLimiter struct {
// contains filtered or unexported fields
}
FixedTruncatedWindowRateLimiter limits how many requests check be make in a time window. This window is calculated by truncating the first request's time of to the limit rate in order to adjust to real time passing. E.g: First request time: 2022-02-05 10:23:23 Rate limit interval: new window every 10 seconds First request window: from 2022-02-05 10:23:20 to 2022-02-05 10:23:30
func NewFixedTruncatedWindowRateLimiter ¶
func NewFixedTruncatedWindowRateLimiter( args FixedTruncatedWindowArgs, ) *FixedTruncatedWindowRateLimiter
NewFixedTruncatedWindowRateLimiter returns a new instance of FixedTruncatedWindowRateLimiter from struct of args
func (*FixedTruncatedWindowRateLimiter) Check ¶
func (l *FixedTruncatedWindowRateLimiter) Check(ctx context.Context) (Result, error)
Check return how many free slots remain without increasing the token counter. This testMethod is typically used to assert there are available requests prior try an increase the counter
func (*FixedTruncatedWindowRateLimiter) Dump ¶ added in v0.7.0
func (l *FixedTruncatedWindowRateLimiter) Dump(ctx context.Context) (r Result, err error)
func (*FixedTruncatedWindowRateLimiter) Try ¶ added in v0.5.1
func (l *FixedTruncatedWindowRateLimiter) Try(ctx context.Context) (Result, error)
Try returns how much time to wait to perform the request and an error indicating whether the rate limit was exhausted or any kind or error happened when updating the backend. Typically, you would do
ttw, err := limiter.Try(ctx)
if errors.Is(ErrRateLimitExceeded) { <-time.After(ttw) // Wait, or enqueue your request }
type FixedWindowArgs ¶ added in v0.1.0
type FixedWindowIncArgs ¶ added in v0.6.0
type FixedWindowMemoryStorage ¶ added in v0.1.0
type FixedWindowMemoryStorage struct {
// contains filtered or unexported fields
}
FixedWindowMemoryStorage is an in-memory storage for the rate limit state. Preferred option when testing and working with standalone instances of your program and do not care about it restarting and not being exactly compliant with servers rate limits
func NewFixedWindowMemoryStorage ¶ added in v0.1.0
func NewFixedWindowMemoryStorage() *FixedWindowMemoryStorage
NewFixedWindowMemoryStorage returns a new instance of FixedWindowMemoryStorage
func (*FixedWindowMemoryStorage) Inc ¶ added in v0.1.0
func (s *FixedWindowMemoryStorage) Inc( ctx context.Context, args FixedWindowIncArgs, ) (int64, error)
type FixedWindowRateLimiter ¶ added in v0.1.0
type FixedWindowRateLimiter struct {
// contains filtered or unexported fields
}
FixedWindowRateLimiter limits how many requests check be make in a time window. This window is calculated by considering the start of the window the exact same moment the first request came. E.g: First request time: 2022-02-05 10:23:23 Rate limit interval: new window every 10 seconds First request window: from 2022-02-05 10:23:23 to 2022-02-05 10:23:33 FIXME: This rate limiter is not consistent across restarts, as there are no
func NewFixedWindowRateLimiter ¶ added in v0.1.0
func NewFixedWindowRateLimiter(args FixedWindowArgs) *FixedWindowRateLimiter
NewFixedWindowRateLimiter returns a new instance of FixedWindowRateLimiter from struct of args
func (*FixedWindowRateLimiter) Check ¶ added in v0.1.0
func (l *FixedWindowRateLimiter) Check(ctx context.Context) (Result, error)
type FixedWindowRedisStorage ¶ added in v0.3.0
type FixedWindowRedisStorage struct {
// contains filtered or unexported fields
}
func NewFixedWindowRedisStorage ¶ added in v0.3.0
func NewFixedWindowRedisStorage( cli *redis.Client, opts FixedWindowRedisStorageOpts, ) FixedWindowRedisStorage
func (FixedWindowRedisStorage) Inc ¶ added in v0.3.0
func (s FixedWindowRedisStorage) Inc( ctx context.Context, args FixedWindowIncArgs, ) (counter int64, err error)
Inc will increase, if there is room to, the rate limiting counter for the bucket specified by window argument.
type FixedWindowRedisStorageOpts ¶ added in v0.3.0
type FixedWindowRedisStorageOpts struct {
Prefix string
}
type Rate ¶
func (Rate) TruncateDuration ¶ added in v0.7.2
TruncateDuration returns, for windows smaller than a minute, the sole unit as they scape the sexagesimal counting mode. Otherwise, return the product of amount and unit to produce the full rate limit window.
type TestClock ¶
type TestClock struct {
// contains filtered or unexported fields
}
func NewMockClock ¶
type TokenFixedWindowRateLimiter ¶ added in v0.2.0
type TokenFixedWindowRateLimiter struct {
// contains filtered or unexported fields
}
TokenFixedWindowRateLimiter behaves the same as a fixed-window rate limiter. However, it allows requests to hold an arbitrary weight, consuming as much as weight from the capacity of the inner fixed-window rate limiter. When using this rate limiter, keep in mind that the `capacity` attribute of the inner rate limit means the total tokens usable for every window, and not the total amount of requests doable on that window.
func NewTokenFixedWindowRateLimiter ¶ added in v0.2.0
func NewTokenFixedWindowRateLimiter(inner fixedWindowRateLimiter) TokenFixedWindowRateLimiter
NewTokenFixedWindowRateLimiter returns a new instance of TokenFixedWindowRateLimiter by receiving an already created fixed-window rate limiter as argument.
func (*TokenFixedWindowRateLimiter) Check ¶ added in v0.2.0
Check returns whether further requests check be made by returning the number of free slots
func (*TokenFixedWindowRateLimiter) Dump ¶ added in v0.7.0
func (l *TokenFixedWindowRateLimiter) Dump(ctx context.Context) (Result, error)
Dump returns the actual rate limit state according to data stores