-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter_token_fixed_window.go
More file actions
48 lines (40 loc) · 1.79 KB
/
limiter_token_fixed_window.go
File metadata and controls
48 lines (40 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package pacemaker
import (
"context"
)
type (
rateLimit interface {
Dump(ctx context.Context) (Result, error)
}
fixedWindowRateLimiter interface {
rateLimit
try(ctx context.Context, tokens int64) (Result, error)
check(ctx context.Context, tokens int64) (Result, error)
fixedWindow()
}
)
// 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.
type TokenFixedWindowRateLimiter struct {
inner fixedWindowRateLimiter
}
// Try returns the amount of time to wait when the rate limit has been exceeded. The total amount of tokens consumed
// by the requests check be given as argument
func (l *TokenFixedWindowRateLimiter) Try(ctx context.Context, tokens int64) (Result, error) {
return l.inner.try(ctx, tokens)
}
// Check returns whether further requests check be made by returning the number of free slots
func (l *TokenFixedWindowRateLimiter) Check(ctx context.Context, tokens int64) (Result, error) {
return l.inner.check(ctx, tokens)
}
// Dump returns the actual rate limit state according to data stores
func (l *TokenFixedWindowRateLimiter) Dump(ctx context.Context) (Result, error) {
return l.inner.Dump(ctx)
}
// NewTokenFixedWindowRateLimiter returns a new instance of TokenFixedWindowRateLimiter by receiving an already
// created fixed-window rate limiter as argument.
func NewTokenFixedWindowRateLimiter(inner fixedWindowRateLimiter) TokenFixedWindowRateLimiter {
return TokenFixedWindowRateLimiter{inner: inner}
}