-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoother.go
282 lines (241 loc) · 6.08 KB
/
smoother.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package smoother
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
)
const (
// DefaultTimeout is the default timeout for requests to rate limiter.
DefaultTimeout = time.Second
// DefaultQueueSize is the default size of the request queue.
DefaultQueueSize = 100
)
// Option is a function that configures a RateSmoother.
type Option func(*RateSmoother)
// WithTimeout sets the timeout for requests to rate limiter.
func WithTimeout(timeout time.Duration) Option {
return func(r *RateSmoother) {
r.timeout = timeout
}
}
// WithQueueSize sets the size of the request queue.
func WithQueueSize(size int) Option {
return func(r *RateSmoother) {
r.queueSize = size
}
}
// RateSmoother implements a rate limiter that blocks to ensure that the time spent between multiple.
type RateSmoother struct {
tryer Tryer
timeout time.Duration
queueSize int
requestQueue chan *waitingRequest
wg sync.WaitGroup
quit chan struct{}
running atomic.Bool
}
// NewRateSmoother creates a new RateSmoother instance.
func NewRateSmoother(tryer Tryer, opts ...Option) (*RateSmoother, error) {
if tryer == nil {
return nil, fmt.Errorf("NewRateSmoother: nil tryer")
}
s := &RateSmoother{
tryer: tryer,
timeout: DefaultTimeout,
queueSize: DefaultQueueSize,
quit: make(chan struct{}),
}
for _, opt := range opts {
opt(s)
}
if err := s.validateOptions(); err != nil {
return nil, fmt.Errorf("NewRateSmoother: %w", err)
}
s.requestQueue = make(chan *waitingRequest, s.queueSize)
return s, nil
}
// Start starts the background processor.
func (r *RateSmoother) Start() {
if !r.running.CompareAndSwap(false, true) {
return
}
r.wg.Add(1)
go r.processQueue()
}
// Stop stops the background processor and cleans up resources.
func (r *RateSmoother) Stop() {
if !r.running.CompareAndSwap(true, false) {
return
}
close(r.quit)
r.wg.Wait()
}
// waitingRequest represents a request waiting to be processed.
type waitingRequest struct {
ctx context.Context
count int
arrivalTime time.Time
resultCh chan<- takeResult
abandoned atomic.Bool // Indicates if the request was abandoned
}
// takeResult represents the result of a Take operation.
type takeResult struct {
waitDuration time.Duration
err error
}
func (r *RateSmoother) validateOptions() error {
if r.timeout <= 0 {
return errors.New("timeout must be positive")
}
if r.queueSize <= 0 {
return errors.New("queue size must be positive")
}
return nil
}
// processQueue handles requests in FIFO order
func (r *RateSmoother) processQueue() {
defer r.wg.Done()
for {
select {
case <-r.quit:
// Drain any remaining requests with errors when shutting down
for {
select {
case req := <-r.requestQueue:
if !req.abandoned.Load() { // Don't send to abandoned requests
req.resultCh <- takeResult{
waitDuration: time.Since(req.arrivalTime),
err: fmt.Errorf("rate smoother shutting down"),
}
}
default:
// After draining, close the request queue channel
close(r.requestQueue)
return
}
}
case req := <-r.requestQueue:
// Process the request
r.handleRequest(req)
}
}
}
// handleRequest processes a single request
func (r *RateSmoother) handleRequest(req *waitingRequest) {
if req.ctx.Err() != nil {
// Handle already canceled context
return
}
var (
allowed bool
waitTime time.Duration
err error
)
for {
// Check if the request context is still valid
select {
case <-req.ctx.Done():
if !req.abandoned.Load() {
req.resultCh <- takeResult{
waitDuration: time.Since(req.arrivalTime),
err: req.ctx.Err(),
}
}
return
default:
}
// Create a context with timeout for the TryTake call
ctxRequest, cancel := context.WithTimeout(req.ctx, r.timeout)
allowed, waitTime, err = r.tryer.TryTake(ctxRequest, req.count)
cancel()
if err != nil {
if !req.abandoned.Load() {
req.resultCh <- takeResult{
waitDuration: time.Since(req.arrivalTime),
err: fmt.Errorf("tryer: %w", err),
}
}
return
}
if allowed {
// Success, send the result
if !req.abandoned.Load() {
req.resultCh <- takeResult{
waitDuration: time.Since(req.arrivalTime),
err: nil,
}
}
return
}
// Need to wait before trying again
timer := time.NewTimer(waitTime)
select {
case <-req.ctx.Done():
if !timer.Stop() {
<-timer.C
}
if !req.abandoned.Load() {
req.resultCh <- takeResult{
waitDuration: time.Since(req.arrivalTime),
err: req.ctx.Err(),
}
}
return
case <-timer.C:
// Timer expired, try again
continue
}
}
}
// Take blocks to ensure that the time spent between multiple Take calls is on average per/rate.
// The count parameter specifies how many tokens to take at once.
// It returns the time at which function waits for allowance.
func (r *RateSmoother) Take(ctx context.Context, count int) (time.Duration, error) {
if count <= 0 {
return 0, fmt.Errorf("invalid count %d", count)
}
if !r.running.Load() {
return 0, errors.New("smoother not running")
}
resultCh := make(chan takeResult, 1)
// Create and enqueue the request
req := &waitingRequest{
ctx: ctx,
count: count,
arrivalTime: time.Now(),
resultCh: resultCh,
}
// Enqueue the request
select {
case r.requestQueue <- req:
// Request enqueued successfully
case <-ctx.Done():
return 0, ctx.Err()
}
// Wait for the result
select {
case result := <-resultCh:
return result.waitDuration, result.err
case <-ctx.Done():
// Context expired while waiting for result
req.abandoned.Store(true) // Mark as abandoned
return time.Since(req.arrivalTime), ctx.Err()
}
}
// BurstFromRPSFunc is the function to calculate the burst from rps.
type BurstFromRPSFunc func(rps int) int
// DefaultBurstFromRPS calculates the empirical dependency of the burst,
// so that it does not freeze rps.
func DefaultBurstFromRPS(rps int) int {
burst := rps / 500 //nolint:mnd // ok
if burst < 1 {
burst = 1
}
if burst > 100 { //nolint:mnd // ok
burst = 100
}
return burst
}