-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paththrottlers.go
708 lines (590 loc) · 15.4 KB
/
throttlers.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package gohalt
import (
"container/heap"
"context"
"errors"
"fmt"
"math"
"math/rand"
"sync"
"sync/atomic"
"time"
)
type Throttler interface {
accept(context.Context, tvisitor)
Acquire(context.Context) error
Release(context.Context) error
}
type tvisitor interface {
tvisitEcho(context.Context, *techo)
tvisitWait(context.Context, *twait)
tvisitPanic(context.Context, *tpanic)
tvisitEach(context.Context, *teach)
tvisitBefore(context.Context, *tbefore)
tvisitChance(context.Context, *tchance)
tvisitAfter(context.Context, *tafter)
tvisitRunning(context.Context, *trunning)
tvisitBuffered(context.Context, *tbuffered)
tvisitPriority(context.Context, *tpriority)
tvisitTimed(context.Context, *ttimed)
tvisitMonitor(context.Context, *tmonitor)
tvisitMetric(context.Context, *tmetric)
tvisitLatency(context.Context, *tlatency)
tvisitPercentile(context.Context, *tpercentile)
tvisitAdaptive(context.Context, *tadaptive)
tvisitContext(context.Context, *tcontext)
tvisitEnqueue(context.Context, *tenqueue)
tvisitKeyed(context.Context, *tkeyed)
tvisitAll(context.Context, *tall)
tvisitAny(context.Context, *tany)
tvisitNot(context.Context, *tnot)
tvisitSuppress(context.Context, *tsuppress)
}
type techo struct {
err error
}
func NewThrottlerEcho(err error) techo {
return techo{err: err}
}
func (thr techo) accept(ctx context.Context, v tvisitor) {
v.tvisitEcho(ctx, &thr)
}
func (thr techo) Acquire(context.Context) error {
return thr.err
}
func (thr techo) Release(context.Context) error {
return nil
}
type twait struct {
duration time.Duration
}
func NewThrottlerWait(duration time.Duration) twait {
return twait{duration: duration}
}
func (thr twait) accept(ctx context.Context, v tvisitor) {
v.tvisitWait(ctx, &thr)
}
func (thr twait) Acquire(context.Context) error {
time.Sleep(thr.duration)
return nil
}
func (thr twait) Release(context.Context) error {
return nil
}
type tpanic struct{}
func NewThrottlerPanic() tpanic {
return tpanic{}
}
func (thr tpanic) accept(ctx context.Context, v tvisitor) {
v.tvisitPanic(ctx, &thr)
}
func (thr tpanic) Acquire(context.Context) error {
panic("throttler has reached panic")
}
func (thr tpanic) Release(context.Context) error {
return nil
}
type teach struct {
current uint64
threshold uint64
}
func NewThrottlerEach(threshold uint64) *teach {
return &teach{threshold: threshold}
}
func (thr *teach) accept(ctx context.Context, v tvisitor) {
v.tvisitEach(ctx, thr)
}
func (thr *teach) Acquire(context.Context) error {
if current := atomic.AddUint64(&thr.current, 1); current%thr.threshold == 0 {
return errors.New("throttler has reached periodic threshold")
}
return nil
}
func (thr *teach) Release(context.Context) error {
return nil
}
type tbefore struct {
current uint64
threshold uint64
}
func NewThrottlerBefore(threshold uint64) *tbefore {
return &tbefore{threshold: threshold}
}
func (thr *tbefore) accept(ctx context.Context, v tvisitor) {
v.tvisitBefore(ctx, thr)
}
func (thr *tbefore) Acquire(context.Context) error {
if current := atomic.AddUint64(&thr.current, 1); current <= thr.threshold {
return errors.New("throttler has not reached threshold yet")
}
return nil
}
func (thr *tbefore) Release(context.Context) error {
return nil
}
type tchance struct {
threshold float64
}
func NewThrottlerChance(threshold float64) tchance {
threshold = math.Abs(threshold)
if threshold > 1.0 {
threshold = 1.0
}
return tchance{threshold: threshold}
}
func (thr tchance) accept(ctx context.Context, v tvisitor) {
v.tvisitChance(ctx, &thr)
}
func (thr tchance) Acquire(context.Context) error {
if thr.threshold > 1.0-rand.Float64() {
return errors.New("throttler has reached chance threshold")
}
return nil
}
func (thr tchance) Release(context.Context) error {
return nil
}
type tafter struct {
current uint64
threshold uint64
}
func NewThrottlerAfter(threshold uint64) *tafter {
return &tafter{threshold: threshold}
}
func (thr *tafter) accept(ctx context.Context, v tvisitor) {
v.tvisitAfter(ctx, thr)
}
func (thr *tafter) Acquire(context.Context) error {
if current := atomic.AddUint64(&thr.current, 1); current > thr.threshold {
return errors.New("throttler has exceed threshold")
}
return nil
}
func (thr *tafter) Release(context.Context) error {
return nil
}
type trunning struct {
running uint64
threshold uint64
}
func NewThrottlerRunning(threshold uint64) *trunning {
return &trunning{threshold: threshold}
}
func (thr *trunning) accept(ctx context.Context, v tvisitor) {
v.tvisitRunning(ctx, thr)
}
func (thr *trunning) Acquire(context.Context) error {
if running := atomic.AddUint64(&thr.running, 1); running > thr.threshold {
return errors.New("throttler has exceed running threshold")
}
return nil
}
func (thr *trunning) Release(context.Context) error {
if running := atomic.AddUint64(&thr.running, ^uint64(0)); int64(running) < 0 {
// fix running discrepancies
atomic.AddUint64(&thr.running, 1)
}
return nil
}
type tbuffered struct {
running chan struct{}
}
func NewThrottlerBuffered(threshold uint64) *tbuffered {
return &tbuffered{running: make(chan struct{}, threshold)}
}
func (thr *tbuffered) accept(ctx context.Context, v tvisitor) {
v.tvisitBuffered(ctx, thr)
}
func (thr *tbuffered) Acquire(context.Context) error {
thr.running <- struct{}{}
return nil
}
func (thr *tbuffered) Release(ctx context.Context) error {
select {
case <-thr.running:
return nil
default:
return nil
}
}
type tpriority struct {
running *sync.Map
threshold uint64
levels uint8
}
func NewThrottlerPriority(threshold uint64, levels uint8) tpriority {
if levels == 0 {
levels = 1
}
running := &sync.Map{}
koef := float64(threshold) / (float64(levels) / 2 * float64((2 + (levels - 1))))
for i := uint8(1); i <= levels; i++ {
slots := uint64(math.Round(float64(i) * koef))
running.Store(i, make(chan struct{}, slots))
}
thr := tpriority{threshold: threshold, levels: levels}
thr.running = running
return thr
}
func (thr tpriority) accept(ctx context.Context, v tvisitor) {
v.tvisitPriority(ctx, &thr)
}
func (thr tpriority) Acquire(ctx context.Context) error {
priority := ctxPriority(ctx, thr.levels)
val, _ := thr.running.Load(priority)
running := val.(chan struct{})
running <- struct{}{}
return nil
}
func (thr tpriority) Release(ctx context.Context) error {
priority := ctxPriority(ctx, thr.levels)
val, _ := thr.running.Load(priority)
running := val.(chan struct{})
select {
case <-running:
return nil
default:
return nil
}
}
type ttimed struct {
*tafter
interval time.Duration
slide time.Duration
}
func NewThrottlerTimed(ctx context.Context, threshold uint64, interval time.Duration, slide time.Duration) ttimed {
thr := NewThrottlerAfter(threshold)
delta, window := threshold, interval
if slide > 0 && interval > slide {
delta = uint64(math.Ceil(float64(delta) / float64(slide)))
window /= slide
}
exec(ctx, loop(window, func(ctx context.Context) error {
atomic.AddUint64(&thr.current, ^(delta - 1))
if current := atomic.LoadUint64(&thr.current); current >= ^uint64(0) {
atomic.StoreUint64(&thr.current, 0)
}
return ctx.Err()
}))
return ttimed{tafter: thr, interval: interval, slide: slide}
}
func (thr ttimed) accept(ctx context.Context, v tvisitor) {
v.tvisitTimed(ctx, &thr)
}
func (thr ttimed) Acquire(ctx context.Context) error {
return thr.tafter.Acquire(ctx)
}
func (thr ttimed) Release(ctx context.Context) error {
return thr.tafter.Release(ctx)
}
type tmonitor struct {
mnt Monitor
threshold Stats
}
func NewThrottlerMonitor(mnt Monitor, threshold Stats) tmonitor {
return tmonitor{mnt: mnt, threshold: threshold}
}
func (thr tmonitor) accept(ctx context.Context, v tvisitor) {
v.tvisitMonitor(ctx, &thr)
}
func (thr tmonitor) Acquire(ctx context.Context) error {
stats, err := thr.mnt.Stats(ctx)
if err != nil {
return fmt.Errorf("throttler hasn't found any stats %w", err)
}
if stats.MEMAlloc >= thr.threshold.MEMAlloc || stats.MEMSystem >= thr.threshold.MEMSystem ||
stats.CPUPause >= thr.threshold.CPUPause || stats.CPUUsage >= thr.threshold.CPUUsage {
return errors.New("throttler has exceed stats threshold")
}
return nil
}
func (thr tmonitor) Release(context.Context) error {
return nil
}
type tmetric struct {
mtc Metric
}
func NewThrottlerMetric(mtc Metric) tmetric {
return tmetric{mtc: mtc}
}
func (thr tmetric) accept(ctx context.Context, v tvisitor) {
v.tvisitMetric(ctx, &thr)
}
func (thr tmetric) Acquire(ctx context.Context) error {
val, err := thr.mtc.Query(ctx)
if err != nil {
return fmt.Errorf("throttler hasn't found any metric %w", err)
}
if val {
return errors.New("throttler has reached metric threshold")
}
return nil
}
func (thr tmetric) Release(context.Context) error {
return nil
}
type tlatency struct {
latency uint64
limit time.Duration
retention time.Duration
}
func NewThrottlerLatency(limit time.Duration, retention time.Duration) *tlatency {
return &tlatency{limit: limit, retention: retention}
}
func (thr *tlatency) accept(ctx context.Context, v tvisitor) {
v.tvisitLatency(ctx, thr)
}
func (thr tlatency) Acquire(context.Context) error {
if latency := time.Duration(atomic.LoadUint64(&thr.latency)); latency > thr.limit {
return errors.New("throttler has exceed latency threshold")
}
return nil
}
func (thr *tlatency) Release(ctx context.Context) error {
if latency := atomic.LoadUint64(&thr.latency); latency < uint64(thr.limit) {
latency := uint64(ctxTimestamp(ctx) - time.Now().UTC().UnixNano())
atomic.StoreUint64(&thr.latency, latency)
exec(ctx, once(thr.retention, func(context.Context) error {
atomic.StoreUint64(&thr.latency, 0)
return nil
}))
}
return nil
}
type tpercentile struct {
latencies *blatheap
limit time.Duration
percentile float64
retention time.Duration
}
func NewThrottlerPercentile(limit time.Duration, percentile float64, retention time.Duration) *tpercentile {
percentile = math.Abs(percentile)
if percentile > 1.0 {
percentile = 1.0
}
return &tpercentile{
latencies: &blatheap{},
limit: limit,
percentile: percentile,
retention: retention,
}
}
func (thr *tpercentile) accept(ctx context.Context, v tvisitor) {
v.tvisitPercentile(ctx, thr)
}
func (thr *tpercentile) Acquire(ctx context.Context) error {
at := int(math.Round(float64(thr.latencies.Len()) * thr.percentile))
if latency := time.Duration(thr.latencies.At(at)); latency > thr.limit {
exec(ctx, once(thr.retention, func(context.Context) error {
thr.latencies.Prune()
return nil
}))
return errors.New("throttler has exceed latency threshold")
}
return nil
}
func (thr *tpercentile) Release(ctx context.Context) error {
latency := ctxTimestamp(ctx) - time.Now().UTC().UnixNano()
heap.Push(thr.latencies, latency)
return nil
}
type tadaptive struct {
ttimed
step uint64
thr Throttler
}
func NewThrottlerAdaptive(
ctx context.Context,
limit uint64,
interval time.Duration,
slide time.Duration,
step uint64,
thr Throttler,
) *tadaptive {
return &tadaptive{
ttimed: NewThrottlerTimed(ctx, limit, interval, slide),
step: step,
thr: thr,
}
}
func (thr *tadaptive) accept(ctx context.Context, v tvisitor) {
v.tvisitAdaptive(ctx, thr)
}
func (thr *tadaptive) Acquire(ctx context.Context) error {
err := thr.thr.Acquire(ctx)
if err != nil {
atomic.AddUint64(&thr.ttimed.threshold, ^(thr.step*thr.step - 1))
} else {
atomic.AddUint64(&thr.ttimed.threshold, thr.step)
}
return thr.ttimed.Acquire(ctx)
}
func (thr tadaptive) Release(ctx context.Context) error {
return thr.ttimed.Release(ctx)
}
type tcontext struct{}
func NewThrottlerContext() tcontext {
return tcontext{}
}
func (thr tcontext) accept(ctx context.Context, v tvisitor) {
v.tvisitContext(ctx, &thr)
}
func (thr tcontext) Acquire(ctx context.Context) error {
select {
case <-ctx.Done():
return fmt.Errorf("throttler has received context error %w", ctx.Err())
default:
return nil
}
}
func (thr tcontext) Release(ctx context.Context) error {
return nil
}
type tenqueue struct {
enq Enqueuer
}
func NewThrottlerEnqueue(enq Enqueuer) tenqueue {
return tenqueue{enq: enq}
}
func (thr tenqueue) accept(ctx context.Context, v tvisitor) {
v.tvisitEnqueue(ctx, &thr)
}
func (thr tenqueue) Acquire(ctx context.Context) error {
marshaler, data := ctxMarshaler(ctx), ctxData(ctx)
if marshaler == nil || data == nil {
return errors.New("throttler hasn't found any message")
}
message, err := marshaler(data)
if err != nil {
return fmt.Errorf("throttler hasn't sent any message %w", err)
}
if err := thr.enq.Enqueue(ctx, message); err != nil {
return fmt.Errorf("throttler hasn't sent any message %w", err)
}
return nil
}
func (thr tenqueue) Release(ctx context.Context) error {
return nil
}
type tkeyed struct {
keys *sync.Map
gen Generator
}
func NewThrottlerKeyed(gen Generator) tkeyed {
return tkeyed{keys: &sync.Map{}, gen: gen}
}
func (thr tkeyed) accept(ctx context.Context, v tvisitor) {
v.tvisitKeyed(ctx, &thr)
}
func (thr tkeyed) Acquire(ctx context.Context) error {
if key := ctxKey(ctx); key != nil {
r, _ := thr.keys.LoadOrStore(key, thr.gen.Generate(ctx, key))
return r.(Throttler).Acquire(ctx)
}
return errors.New("throttler hasn't found any key")
}
func (thr tkeyed) Release(ctx context.Context) error {
if key := ctxKey(ctx); key != nil {
if r, ok := thr.keys.Load(key); ok {
return r.(Throttler).Release(ctx)
}
}
return nil
}
type tall []Throttler
func NewThrottlerAll(thrs ...Throttler) tall {
return tall(thrs)
}
func (thrs tall) accept(ctx context.Context, v tvisitor) {
v.tvisitAll(ctx, &thrs)
}
func (thrs tall) Acquire(ctx context.Context) error {
for _, thr := range thrs {
if err := thr.Acquire(ctx); err == nil {
return nil
}
}
return errors.New("throttler has received one derived error")
}
func (thrs tall) Release(ctx context.Context) error {
for _, thr := range thrs {
if err := thr.Release(ctx); err == nil {
return nil
}
}
return nil
}
type tany []Throttler
func NewThrottlerAny(thrs ...Throttler) tany {
return tany(thrs)
}
func (thrs tany) accept(ctx context.Context, v tvisitor) {
v.tvisitAny(ctx, &thrs)
}
func (thrs tany) Acquire(ctx context.Context) error {
errch := make(chan error, len(thrs))
var wg sync.WaitGroup
for _, thr := range thrs {
wg.Add(1)
go func(thr Throttler) {
if err := thr.Acquire(ctx); err != nil {
errch <- errors.New("throttler has received multiple derived errors")
}
wg.Done()
}(thr)
}
wg.Wait()
close(errch)
for err := range errch {
return err
}
return nil
}
func (thrs tany) Release(ctx context.Context) error {
var wg sync.WaitGroup
for _, thr := range thrs {
wg.Add(1)
go func(thr Throttler) {
_ = thr.Release(ctx)
wg.Done()
}(thr)
}
wg.Wait()
return nil
}
type tnot struct {
thr Throttler
}
func NewThrottlerNot(thr Throttler) tnot {
return tnot{thr: thr}
}
func (thr tnot) accept(ctx context.Context, v tvisitor) {
v.tvisitNot(ctx, &thr)
}
func (thr tnot) Acquire(ctx context.Context) error {
if err := thr.thr.Acquire(ctx); err != nil {
return nil
}
return errors.New("throttler hasn't received any derived errors")
}
func (thr tnot) Release(ctx context.Context) error {
_ = thr.thr.Release(ctx)
return nil
}
type tsuppress struct {
thr Throttler
}
func NewThrottlerSuppress(thr Throttler) tsuppress {
return tsuppress{thr: thr}
}
func (thr tsuppress) accept(ctx context.Context, v tvisitor) {
v.tvisitSuppress(ctx, &thr)
}
func (thr tsuppress) Acquire(ctx context.Context) error {
_ = thr.thr.Acquire(ctx)
return nil
}
func (thr tsuppress) Release(ctx context.Context) error {
_ = thr.thr.Release(ctx)
return nil
}