3 | 3 |
"errors"
|
4 | 4 |
"time"
|
5 | 5 |
|
6 | |
juju "github.com/juju/ratelimit"
|
|
6 |
"github.com/juju/ratelimit"
|
7 | 7 |
"golang.org/x/net/context"
|
8 | 8 |
|
9 | 9 |
"github.com/go-kit/kit/endpoint"
|
|
16 | 16 |
// NewTokenBucketLimiter returns an endpoint.Middleware that acts as a rate
|
17 | 17 |
// limiter based on a token-bucket algorithm. Requests that would exceed the
|
18 | 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,
|
24 | |
}
|
25 | |
for _, option := range options {
|
26 | |
option(&limiter)
|
27 | |
}
|
28 | |
tb := juju.NewBucketWithRate(limiter.rate, limiter.capacity)
|
|
19 |
func NewTokenBucketLimiter(tb *ratelimit.Bucket) endpoint.Middleware {
|
29 | 20 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
30 | 21 |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
31 | |
if tb.TakeAvailable(limiter.take) == 0 {
|
|
22 |
if tb.TakeAvailable(1) == 0 {
|
32 | 23 |
return nil, ErrLimited
|
33 | 24 |
}
|
34 | 25 |
return next(ctx, request)
|
|
36 | 27 |
}
|
37 | 28 |
}
|
38 | 29 |
|
39 | |
type tokenBucketLimiter struct {
|
40 | |
rate float64
|
41 | |
capacity int64
|
42 | |
take int64
|
43 | |
}
|
44 | |
|
45 | |
// TokenBucketLimiterOption sets a parameter on the TokenBucketLimiter.
|
46 | |
type TokenBucketLimiterOption func(*tokenBucketLimiter)
|
47 | |
|
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 }
|
53 | |
}
|
54 | |
|
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 | |
}
|
61 | |
|
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 | |
}
|
67 | |
|
68 | 30 |
// NewTokenBucketThrottler returns an endpoint.Middleware that acts as a
|
69 | 31 |
// 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)
|
|
32 |
// exceed the maximum request rate are delayed via the parameterized sleep
|
|
33 |
// function. By default you may pass time.Sleep.
|
|
34 |
func NewTokenBucketThrottler(tb *ratelimit.Bucket, sleep func(time.Duration)) endpoint.Middleware {
|
85 | 35 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
86 | 36 |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
87 | |
throttler.sleep(tb.Take(throttler.take))
|
|
37 |
sleep(tb.Take(1))
|
88 | 38 |
return next(ctx, request)
|
89 | 39 |
}
|
90 | 40 |
}
|
91 | 41 |
}
|
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 | |
}
|