3 | 3 |
"errors"
|
4 | 4 |
"time"
|
5 | 5 |
|
6 | |
"github.com/tsenart/tb"
|
|
6 |
juju "github.com/juju/ratelimit"
|
7 | 7 |
"golang.org/x/net/context"
|
8 | 8 |
|
9 | 9 |
"github.com/go-kit/kit/endpoint"
|
10 | 10 |
)
|
11 | 11 |
|
12 | |
// ErrThrottled is returned in the request path when the rate limiter is
|
|
12 |
// ErrLimited is returned in the request path when the rate limiter is
|
13 | 13 |
// triggered and the request is rejected.
|
14 | |
var ErrThrottled = errors.New("throttled")
|
|
14 |
var ErrLimited = errors.New("rate limit exceeded")
|
15 | 15 |
|
16 | |
// NewTokenBucketThrottler returns an endpoint.Middleware that acts as a rate
|
17 | |
// limiter based on a "token-bucket" algorithm. Requests that would exceed the
|
18 | |
// maximum request rate are rejected with an error.
|
19 | |
func NewTokenBucketThrottler(options ...TokenBucketOption) endpoint.Middleware {
|
20 | |
t := tokenBucketThrottler{
|
21 | |
freq: 100 * time.Millisecond,
|
22 | |
key: "",
|
23 | |
rate: 100,
|
24 | |
take: 1,
|
|
16 |
// NewTokenBucketLimiter returns an endpoint.Middleware that acts as a rate
|
|
17 |
// limiter based on a token-bucket algorithm. Requests that would exceed the
|
|
18 |
// maximum request rate are simply rejected with an error.
|
|
19 |
func NewTokenBucketLimiter(options ...TokenBucketLimiterOption) endpoint.Middleware {
|
|
20 |
limiter := tokenBucketLimiter{
|
|
21 |
rate: 100,
|
|
22 |
capacity: 100,
|
|
23 |
take: 1,
|
25 | 24 |
}
|
26 | 25 |
for _, option := range options {
|
27 | |
option(&t)
|
|
26 |
option(&limiter)
|
28 | 27 |
}
|
29 | |
throttler := tb.NewThrottler(t.freq)
|
|
28 |
tb := juju.NewBucketWithRate(limiter.rate, limiter.capacity)
|
30 | 29 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
31 | 30 |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
32 | |
if throttler.Halt(t.key, t.take, t.rate) {
|
33 | |
return nil, ErrThrottled
|
|
31 |
if tb.TakeAvailable(limiter.take) == 0 {
|
|
32 |
return nil, ErrLimited
|
34 | 33 |
}
|
35 | 34 |
return next(ctx, request)
|
36 | 35 |
}
|
37 | 36 |
}
|
38 | 37 |
}
|
39 | 38 |
|
40 | |
type tokenBucketThrottler struct {
|
41 | |
freq time.Duration
|
42 | |
key string
|
43 | |
rate int64
|
44 | |
take int64
|
|
39 |
type tokenBucketLimiter struct {
|
|
40 |
rate float64
|
|
41 |
capacity int64
|
|
42 |
take int64
|
45 | 43 |
}
|
46 | 44 |
|
47 | |
// TokenBucketOption sets an option on the token bucket throttler.
|
48 | |
type TokenBucketOption func(*tokenBucketThrottler)
|
|
45 |
// TokenBucketLimiterOption sets a parameter on the TokenBucketLimiter.
|
|
46 |
type TokenBucketLimiterOption func(*tokenBucketLimiter)
|
49 | 47 |
|
50 | |
// TokenBucketFillFrequency sets the interval at which tokens are replenished
|
51 | |
// into the bucket. By default, it's 100 milliseconds.
|
52 | |
func TokenBucketFillFrequency(freq time.Duration) TokenBucketOption {
|
53 | |
return func(t *tokenBucketThrottler) { t.freq = freq }
|
|
48 |
// TokenBucketLimiterRate sets the rate (per second) at which tokens are
|
|
49 |
// replenished into the bucket. For most use cases, this should be the same as
|
|
50 |
// the capacity. By default, the rate is 100.
|
|
51 |
func TokenBucketLimiterRate(rate float64) TokenBucketLimiterOption {
|
|
52 |
return func(tb *tokenBucketLimiter) { tb.rate = rate }
|
54 | 53 |
}
|
55 | 54 |
|
56 | |
// TokenBucketMaxRate sets the maximum allowed request rate.
|
57 | |
// By default, it's 100.
|
58 | |
func TokenBucketMaxRate(rate int64) TokenBucketOption {
|
59 | |
return func(t *tokenBucketThrottler) { t.rate = rate }
|
|
55 |
// TokenBucketLimiterCapacity sets the maximum number of tokens that the
|
|
56 |
// bucket will hold. For most use cases, this should be the same as the rate.
|
|
57 |
// By default, the capacity is 100.
|
|
58 |
func TokenBucketLimiterCapacity(capacity int64) TokenBucketLimiterOption {
|
|
59 |
return func(tb *tokenBucketLimiter) { tb.capacity = capacity }
|
60 | 60 |
}
|
61 | 61 |
|
62 | |
// TokenBucketTake sets the number of tokens taken with each request.
|
63 | |
// By default, it's 1.
|
64 | |
func TokenBucketTake(take int64) TokenBucketOption {
|
65 | |
return func(t *tokenBucketThrottler) { t.take = take }
|
|
62 |
// TokenBucketLimiterTake sets the number of tokens that will be taken from
|
|
63 |
// the bucket with each request. By default, this is 1.
|
|
64 |
func TokenBucketLimiterTake(take int64) TokenBucketLimiterOption {
|
|
65 |
return func(tb *tokenBucketLimiter) { tb.take = take }
|
66 | 66 |
}
|
|
67 |
|
|
68 |
// NewTokenBucketThrottler returns an endpoint.Middleware that acts as a
|
|
69 |
// request throttler based on a token-bucket algorithm. Requests that would
|
|
70 |
// exceed the maximum request rate are delayed via a parameterized sleep
|
|
71 |
// function.
|
|
72 |
func NewTokenBucketThrottler(options ...TokenBucketThrottlerOption) endpoint.Middleware {
|
|
73 |
throttler := tokenBucketThrottler{
|
|
74 |
tokenBucketLimiter: tokenBucketLimiter{
|
|
75 |
rate: 100,
|
|
76 |
capacity: 100,
|
|
77 |
take: 1,
|
|
78 |
},
|
|
79 |
sleep: time.Sleep,
|
|
80 |
}
|
|
81 |
for _, option := range options {
|
|
82 |
option(&throttler)
|
|
83 |
}
|
|
84 |
tb := juju.NewBucketWithRate(throttler.rate, throttler.capacity)
|
|
85 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
|
86 |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
87 |
throttler.sleep(tb.Take(throttler.take))
|
|
88 |
return next(ctx, request)
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
type tokenBucketThrottler struct {
|
|
94 |
tokenBucketLimiter
|
|
95 |
sleep func(time.Duration)
|
|
96 |
}
|
|
97 |
|
|
98 |
// TokenBucketThrottlerOption sets a parameter on the TokenBucketThrottler.
|
|
99 |
type TokenBucketThrottlerOption func(*tokenBucketThrottler)
|
|
100 |
|
|
101 |
// TokenBucketThrottlerRate sets the rate (per second) at which tokens are
|
|
102 |
// replenished into the bucket. For most use cases, this should be the same as
|
|
103 |
// the capacity. By default, the rate is 100.
|
|
104 |
func TokenBucketThrottlerRate(rate float64) TokenBucketThrottlerOption {
|
|
105 |
return func(tb *tokenBucketThrottler) { tb.rate = rate }
|
|
106 |
}
|
|
107 |
|
|
108 |
// TokenBucketThrottlerCapacity sets the maximum number of tokens that the
|
|
109 |
// bucket will hold. For most use cases, this should be the same as the rate.
|
|
110 |
// By default, the capacity is 100.
|
|
111 |
func TokenBucketThrottlerCapacity(capacity int64) TokenBucketThrottlerOption {
|
|
112 |
return func(tb *tokenBucketThrottler) { tb.capacity = capacity }
|
|
113 |
}
|
|
114 |
|
|
115 |
// TokenBucketThrottlerTake sets the number of tokens that will be taken from
|
|
116 |
// the bucket with each request. By default, this is 1.
|
|
117 |
func TokenBucketThrottlerTake(take int64) TokenBucketThrottlerOption {
|
|
118 |
return func(tb *tokenBucketThrottler) { tb.take = take }
|
|
119 |
}
|
|
120 |
|
|
121 |
// TokenBucketThrottlerSleep sets the sleep function that's invoked to
|
|
122 |
// throttle requests. By default, it's time.Sleep.
|
|
123 |
func TokenBucketThrottlerSleep(sleep func(time.Duration)) TokenBucketThrottlerOption {
|
|
124 |
return func(tb *tokenBucketThrottler) { tb.sleep = sleep }
|
|
125 |
}
|