-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_limiter.go
More file actions
357 lines (314 loc) · 16 KB
/
load_limiter.go
File metadata and controls
357 lines (314 loc) · 16 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package goll
import "time"
// LoadLimiter is the parent interface for all kinds
// of load limiters.
//
// You are encouraged to use this type when storing references
// to your limiters in order to allow for easier implementations switch.
type LoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(tenantKey string, load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(tenantKey string, load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(tenantKey string, load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(tenantKey string, load uint64, timeout time.Duration) SubmitUntilResult
// IsComposite returns true if the limiter is a CompositeLoadLimiter.
IsComposite() bool
}
// StandaloneLoadLimiter is the specialized interface for the standard
// load limiters created with goll.New(...).
//
// Note that all types implementing StandaloneLoadLimiter also implements LoadLimiter:
// You are encouraged to use this type when storing references
// to your limiters in order to allow for easier implementations switch.
type StandaloneLoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(tenantKey string, load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(tenantKey string, load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(tenantKey string, load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(tenantKey string, load uint64, timeout time.Duration) SubmitUntilResult
// IsComposite is "inherited" from LoadLimiter
// and always returns false for this type.
IsComposite() bool
// Stats returns runtime statistics useful to evaluate system status,
// performance and overhead.
Stats(tenantKey string) (RuntimeStatistics, error)
// ForTenant returns a semplified proxy that applies the load limiting
// for the specified tenant, dropping the tenantKey input parameter.
//
// It's useful to simplify the code when you are acting on a single tenant.
//
// Please note that this does not create a new limiter instance,
// it just proxies the calls to the current limiter adding a fixed tenantKey.
ForTenant(tenantKey string) SingleTenantStandaloneLoadLimiter
// ForTenant returns a semplified proxy drops the tenantKey input parameter.
//
// It's useful to simplify the code when you don't need multitenancy.
//
// Please note that this does not create a new limiter instance,
// it just proxies the calls to the current limiter adding a fixed tenantKey.
AsSingleTenant() SingleTenantStandaloneLoadLimiter
}
// CompositeLoadLimiter is the specialized interface for the composite
// load limiters created with goll.NewComposite(...).
//
// Note that all types implementing CompositeLoadLimiter also implements LoadLimiter:
// You are encouraged to use this type when storing references
// to your limiters in order to allow for easier implementations switch.
type CompositeLoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(tenantKey string, load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(tenantKey string, load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(tenantKey string, load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(tenantKey string, load uint64, timeout time.Duration) SubmitUntilResult
// IsComposite is "inherited" from LoadLimiter
// and always returns true for this type.
IsComposite() bool
// Stats returns runtime statistics useful to evaluate system status,
// performance and overhead.
//
// In the case of a composite limiter, both statistics about the
// composite limiter itself and statistics for all the single composed
// limiters will be returned.
Stats(tenantKey string) (CompositeRuntimeStatistics, error)
// ForTenant returns a semplified proxy that applies the load limiting
// for the specified tenant, dropping the tenantKey input parameter.
//
// It's useful to simplify the code when you are acting on a single tenant.
//
// Please note that this does not create a new limiter instance,
// it just proxies the calls to the current limiter adding a fixed tenantKey.
ForTenant(tenantKey string) SingleTenantCompositeLoadLimiter
// ForTenant returns a semplified proxy drops the tenantKey input parameter.
//
// It's useful to simplify the code when you don't need multitenancy.
//
// Please note that this does not create a new limiter instance,
// it just proxies the calls to the current limiter adding a fixed tenantKey.
AsSingleTenant() SingleTenantCompositeLoadLimiter
}
// SingleTenantLoadLimiter is the specialized interface
// for load limiters that do not need to handle multitenancy.
//
// It works exactly like the standard limiter but drops the tenantKey
type SingleTenantLoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(load uint64, timeout time.Duration) SubmitUntilResult
// IsComposite returns true if the limiter is a CompositeLoadLimiter.
IsComposite() bool
}
// SingleTenantStandaloneLoadLimiter is the specialized interface
// for standalone (non composite) load limiters that do not need to handle multitenancy.
//
// It works exactly like the standard standalone limiter but drops the tenantKey
type SingleTenantStandaloneLoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(load uint64, timeout time.Duration) SubmitUntilResult
// Stats returns runtime statistics useful to evaluate system status,
// performance and overhead.
Stats() (RuntimeStatistics, error)
// IsComposite always returns false for this type.
IsComposite() bool
}
// SingleTenantCompositeLoadLimiter is the specialized interface
// for composite load limiters that do not need to handle multitenancy.
//
// It works exactly like the standard composite limiter but drops the tenantKey
type SingleTenantCompositeLoadLimiter interface {
// Probe checks if the given load would be allowed right now.
// it is a readonly method that does not modify the current window data.
Probe(load uint64) (bool, error)
// Submit asks for the given load to be accepted.
// The result object contains an Accepted property
// together with RetryIn information when available.
Submit(load uint64) (SubmitResult, error)
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil value is returned.
// In case of timeout or other errors a non-nil error is returned.
//
// You can check the returned error with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntil(load uint64, timeout time.Duration) error
// SubmitUntil asks for the given load to be accepted and,
// in case of rejection, automatically handles retries and delays.
// In case of acceptance a nil Error field is returned in the output object.
// In case of timeout or other errors a non-nil Error field is returned in the output object.
//
// Unlike SubmitUntil, more information about the request is returned with the output object,
// like the amount of time waited and the amount of submissions attempt.
//
// You can check the returned Error field with errors.Is against
// the sentinels goll.ErrLoadRequestTimeout or goll.ErrLoadRequestRejected,
// or you can cast them to the
// goll.LoadRequestSubmissionTimeout / goll.LoadRequestRejected
// types if you need additional info.
SubmitUntilWithDetails(load uint64, timeout time.Duration) SubmitUntilResult
// Stats returns runtime statistics useful to evaluate system status,
// performance and overhead.
//
// In the case of a composite limiter, both statistics about the
// composite limiter itself and statistics for all the single composed
// limiters will be returned.
Stats() (CompositeRuntimeStatistics, error)
// IsComposite always returns true for this type.
IsComposite() bool
}
// RuntimeStatistics holds runtime statistics
// for a single load limiter.
type RuntimeStatistics struct {
// WindowTotal holds the current active load in absolute units.
WindowTotal uint64
// WindowSegments is a slice holding the amount of absolute load
// allocated to each segment of the window.
WindowSegments []uint64
}
// RuntimeStatistics holds runtime statistics
// for a composite load limiter.
type CompositeRuntimeStatistics struct {
// LimitersStats holds the statistics for each composed limiter
LimitersStats []RuntimeStatistics
}