-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_limiter_impl.go
171 lines (131 loc) · 4.17 KB
/
load_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
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
package goll
import (
"sync"
"time"
"github.com/gammazero/deque"
)
// loadLimiterDefaultImpl holds all the required
// runtime data together with the parsed configuration.
type loadLimiterDefaultImpl struct {
Logger Logger
Config *loadLimiterEffectiveConfig
// Time functions can be overridden for testing.
TimeFunc func() time.Time
SleepFunc func(d time.Duration)
// a lock provides thread safety.
Lock sync.Mutex
// SyncAdapter is an implementation used to synchronize
// the limiter data in a clustered environment.
SyncAdapter SyncAdapter
// we keep all runtime data for tenants
// in a map indexed by tenant key
TenantData map[string]*loadLimiterDefaultImplTenantData
}
type loadLimiterDefaultImplTenantData struct {
// a deque implementation is used to represent the sliding window
// as we need to operate on both sides of the window.
WindowQueue *deque.Deque
// WasOver signals that a rejection was sent with the last request
WasOver bool
// WindowTotal stores the total active load aggregated from the window.
WindowTotal uint64
// Versioning data for persistence and synchronization
Version uint64
}
// loadLimiterEffectiveConfig holds the validated and parsed configuration
// that was obtained from the user-provided configuration.
type loadLimiterEffectiveConfig struct {
// max absolute load
MaxLoad uint64
// window composition
WindowSize uint64
WindowSegmentSize uint64
NumSegments uint64
// features control
SkipRetryInComputing bool
// overstep penalty
ApplyOverstepPenalty bool
AbsoluteOverstepPenalty uint64
OverstepPenaltySegmentSpan uint64
// request overhead penalty
ApplyRequestOverheadPenalty bool
RequestOverheadPenaltyFactor float64
RequestOverheadPenaltySegmentSpan uint64
// penalty capping
ApplyPenaltyCapping bool
AbsoluteMaxPenaltyCap uint64
}
// windowSegment represents a single segment the activeWindow is divided in
type windowSegment struct {
StartTime uint64
Value uint64
}
func (instance *loadLimiterDefaultImpl) getTenant(key string) *loadLimiterDefaultImplTenantData {
existing, exists := instance.TenantData[key]
if exists {
return existing
}
newTenantData := &loadLimiterDefaultImplTenantData{
WindowTotal: 0,
WasOver: false,
Version: 1,
}
// call setMinCapacity on queue
// to avoid dynamically resizing and improve performance.
windowQueue := instance.newWindowQueue()
newTenantData.WindowQueue = windowQueue
instance.TenantData[key] = newTenantData
return newTenantData
}
func (instance *loadLimiterDefaultImpl) newWindowQueue() *deque.Deque {
minQueueCapacity := int(instance.Config.NumSegments) * 3
return deque.New(minQueueCapacity, minQueueCapacity)
}
func (instance *loadLimiterDefaultImpl) currentTime() time.Time {
// hook time provider here to allow easier testing
return instance.TimeFunc()
}
func (instance *loadLimiterDefaultImpl) sleep(d time.Duration) {
// hook time provider here to allow easier testing
instance.SleepFunc(d)
}
// for future usage (persistence)
func (instance *loadLimiterDefaultImpl) markDirty(req *submitRequest) {
req.TenantData.Version++
}
func (instance *loadLimiterDefaultImpl) IsComposite() bool {
return false
}
// Stats returns runtime statistics useful to evaluate system status,
// performance and overhead.
func (instance *loadLimiterDefaultImpl) Stats(tenantKey string) (RuntimeStatistics, error) {
instance.Lock.Lock()
defer instance.Lock.Unlock()
var out RuntimeStatistics
var outErr error
err := instance.withSyncTransaction(func() {
out, outErr = instance.stats(tenantKey)
}, syncTxOptions{
TenantKey: tenantKey,
ReadOnly: true,
})
if err != nil {
return out, err
}
return out, outErr
}
func (instance *loadLimiterDefaultImpl) stats(tenantKey string) (RuntimeStatistics, error) {
tenant := instance.getTenant(tenantKey)
var out RuntimeStatistics
qLen := tenant.WindowQueue.Len()
segments := make([]uint64, qLen)
for i := 0; i < qLen; i++ {
segments[i] = tenant.WindowQueue.At(i).(*windowSegment).Value
}
out = RuntimeStatistics{
WindowTotal: tenant.WindowTotal,
WindowSegments: segments,
}
return out, nil
}
// core methods have been moved to the submit.go and window.go files