-
Notifications
You must be signed in to change notification settings - Fork 769
/
rate_limiter_impl.go
142 lines (121 loc) · 4.03 KB
/
rate_limiter_impl.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package quotas
import (
"context"
"sync"
"time"
"golang.org/x/time/rate"
)
type (
// RateLimiterImpl is a wrapper around the golang rate limiter
RateLimiterImpl struct {
sync.RWMutex
rate float64
burst int
goRateLimiter *rate.Limiter
}
)
var _ RateLimiter = (*RateLimiterImpl)(nil)
// NewRateLimiter returns a new rate limiter that can handle dynamic
// configuration updates
func NewRateLimiter(newRate float64, newBurst int) *RateLimiterImpl {
rl := &RateLimiterImpl{
rate: newRate,
burst: newBurst,
goRateLimiter: rate.NewLimiter(rate.Limit(newRate), newBurst),
}
return rl
}
// SetRate set the rate of the rate limiter
func (rl *RateLimiterImpl) SetRate(rate float64) {
rl.refreshInternalRateLimiterImpl(&rate, nil)
}
// SetBurst set the burst of the rate limiter
func (rl *RateLimiterImpl) SetBurst(burst int) {
rl.refreshInternalRateLimiterImpl(nil, &burst)
}
// SetRateBurst set the rate & burst of the rate limiter
func (rl *RateLimiterImpl) SetRateBurst(rate float64, burst int) {
rl.refreshInternalRateLimiterImpl(&rate, &burst)
}
// Allow immediately returns with true or false indicating if a rate limit
// token is available or not
func (rl *RateLimiterImpl) Allow() bool {
return rl.goRateLimiter.Allow()
}
// AllowN immediately returns with true or false indicating if n rate limit
// token is available or not
func (rl *RateLimiterImpl) AllowN(now time.Time, numToken int) bool {
return rl.goRateLimiter.AllowN(now, numToken)
}
// Reserve reserves a rate limit token
func (rl *RateLimiterImpl) Reserve() Reservation {
return rl.goRateLimiter.Reserve()
}
// ReserveN reserves n rate limit token
func (rl *RateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation {
return rl.goRateLimiter.ReserveN(now, numToken)
}
// Wait waits up till deadline for a rate limit token
func (rl *RateLimiterImpl) Wait(ctx context.Context) error {
return rl.goRateLimiter.Wait(ctx)
}
// WaitN waits up till deadline for n rate limit token
func (rl *RateLimiterImpl) WaitN(ctx context.Context, numToken int) error {
return rl.goRateLimiter.WaitN(ctx, numToken)
}
// Rate returns the rate per second for this rate limiter
func (rl *RateLimiterImpl) Rate() float64 {
rl.Lock()
defer rl.Unlock()
return rl.rate
}
// Burst returns the burst for this rate limiter
func (rl *RateLimiterImpl) Burst() int {
rl.Lock()
defer rl.Unlock()
return rl.burst
}
func (rl *RateLimiterImpl) refreshInternalRateLimiterImpl(
newRate *float64,
newBurst *int,
) {
rl.Lock()
defer rl.Unlock()
refresh := false
if newRate != nil && rl.rate != *newRate {
rl.rate = *newRate
refresh = true
}
if newBurst != nil && rl.burst != *newBurst {
rl.burst = *newBurst
refresh = true
}
if refresh {
now := time.Now()
rl.goRateLimiter.SetLimitAt(now, rate.Limit(rl.rate))
rl.goRateLimiter.SetBurstAt(now, rl.burst)
}
}