-
Notifications
You must be signed in to change notification settings - Fork 6
/
regulator.go
116 lines (102 loc) · 2.43 KB
/
regulator.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
// Copyright 2022 The incite Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package incite
import (
"context"
"time"
)
// A regulator is an object that can regulate the rate at which events
// happen. It is a component of a worker and is used to limit the rate
// at which the worker manipulates chunks flowing into the worker. This
// helps the worker stay under CloudWatch Logs API RPS limits.
type regulator struct {
close <-chan struct{} // Short-circuits a wait when owning mgr is closed
minDelay time.Duration // Minimum delay enforced by wait between consecutive events
lastReq time.Time // Time of last event
timer *time.Timer // Timer for rate limiting
ding bool // Flag indicating whether timer channel has been read
rps adapter // RPS adapter whose current value is current RPS
}
const (
rpsMin = 1.0
rpsDownStep = 1.0
rpsUpPerS = 0.5
)
func makeRegulator(close <-chan struct{}, rps, defaultRPS int, adapt bool) regulator {
if rps <= 0 {
rps = defaultRPS
}
var rpsAdapter adapter
if adapt {
rpsAdapter = &standardAdapter{
val: float64(rps),
max: float64(rps),
upPerS: rpsUpPerS,
min: rpsMin,
downStep: rpsDownStep,
last: time.Now(),
}
} else {
rpsAdapter = disabledAdapter(rps)
}
r := regulator{
close: close,
timer: time.NewTimer(1<<63 - 1),
rps: rpsAdapter,
}
r.setMinDelay()
return r
}
func (r *regulator) wait(ctx context.Context) error {
if !r.setTimerRPS() {
return nil
}
select {
case <-r.close:
return errClosing
case <-ctx.Done():
return ctx.Err()
case <-r.timer.C:
r.ding = true
return nil
}
}
func (r *regulator) setTimer(d time.Duration) bool {
if !r.ding && !r.timer.Stop() {
<-r.timer.C
} else {
r.ding = false
}
if d > 0 {
r.timer.Reset(d)
return true
}
r.timer.Reset(1<<63 - 1)
return false
}
func (r *regulator) setTimerRPS() bool {
delaySoFar := time.Since(r.lastReq)
delayRem := r.minDelay - delaySoFar
if delayRem <= 0 {
return false
}
return r.setTimer(delayRem)
}
func (r *regulator) setMinDelay() {
r.minDelay = time.Second / time.Duration(r.rps.value())
}
func (r *regulator) throttled() bool {
if r.rps.decrease() {
r.setMinDelay()
return true
}
return false
}
func (r *regulator) notThrottled() bool {
if r.rps.increase() {
r.setMinDelay()
return true
}
return false
}