-
Notifications
You must be signed in to change notification settings - Fork 1
/
congestion.go
179 lines (138 loc) · 2.76 KB
/
congestion.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
package congestion
import (
"context"
"errors"
"sync"
)
// Dropped is the error that will be returned if this token is dropped
var Dropped = errors.New("dropped")
type Config struct {
Capacity int
MaxLimit int
}
type Limiter struct {
mu sync.Mutex
waiters priorityQueue
stage stage
acksLeft int
outstanding int
limit int
maxLimit int
}
func New(cfg Config) Limiter {
return Limiter{
stage: slowStart,
limit: 1,
acksLeft: 1,
maxLimit: cfg.MaxLimit,
waiters: newQueue(cfg.Capacity),
}
}
// Acquire a Lock with FIFO ordering, respecting the context. Returns an error it fails to acquire.
func (l *Limiter) Acquire(ctx context.Context, priority int) error {
l.mu.Lock()
// Fast path if we are unblocked.
if l.outstanding < l.limit && l.waiters.Len() == 0 {
l.outstanding++
l.mu.Unlock()
return nil
}
r := rendezvouz{
priority: priority,
errChan: make(chan error),
}
pushed := l.waiters.Push(&r)
l.mu.Unlock()
if !pushed {
return Dropped
}
select {
case err := <-r.errChan:
return err
case <-ctx.Done():
err := ctx.Err()
l.mu.Lock()
select {
case err = <-r.errChan:
default:
l.waiters.Remove(&r)
}
l.mu.Unlock()
return err
}
}
func (l *Limiter) ack() {
// If we are waiting on acks, decrement and move on
if l.stage == recovering {
l.acksLeft = l.limit
// Implement a waiting period of our limit before scaling again
l.stage = waiting
return
}
if l.acksLeft > 1 {
l.acksLeft--
return
}
switch l.stage {
case waiting:
if l.outstanding == l.limit {
l.stage = increasing
}
// If we're in slow start, double our limit
case slowStart:
l.limit = l.limit * 2
// If we're increasing increment
case increasing:
l.limit++
}
if l.limit > l.maxLimit {
l.limit = l.maxLimit
}
// reset acks left for next stage transition
l.acksLeft = l.limit
}
// Release a previously acquired lock.
func (l *Limiter) Release() {
l.mu.Lock()
l.ack()
l.outstanding--
if l.outstanding < 0 {
l.mu.Unlock()
panic("lock: bad release")
}
for !l.waiters.Empty() && l.outstanding < l.limit {
rendezvouz := l.waiters.Pop()
l.outstanding++
rendezvouz.Signal()
}
l.mu.Unlock()
}
func (l *Limiter) decrease() {
l.limit = (l.limit * 3) / 4
if l.limit < 1 {
l.limit = 1
}
l.acksLeft = l.limit
}
// Signal that we need to backoff, and decrease our limit.
func (l *Limiter) Backoff() {
l.mu.Lock()
switch l.stage {
// Decrease limit if we were not recovering
case slowStart:
l.decrease()
case waiting:
l.decrease()
case increasing:
l.decrease()
// If we are recovering for more than the ack period, we decrease the limit again
case recovering:
if l.acksLeft > 1 {
l.acksLeft--
} else {
l.decrease()
}
}
l.stage = recovering
l.mu.Unlock()
}