-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalg.go
228 lines (205 loc) · 4.04 KB
/
alg.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
package throtto
import (
"errors"
"fmt"
"log"
"math"
"net/http"
"sync"
"time"
)
type lweight struct {
sw, fw float64
sync.Mutex
}
type lcap struct {
window, thres float64
sync.Mutex
}
type lcount struct {
count, pcount, scount, fcount, dcount int64
sync.Mutex
}
type lstate struct {
lwindow float64
isDrop bool
sync.Mutex
}
type lmem struct {
wdrop []float64
sync.Mutex
}
type ltask struct {
tasks []string
sync.Mutex
}
type cctrl struct {
sincr, fdcr, flux float64
}
type limiter struct {
lweight *lweight
lcap *lcap
lcount *lcount
lstate *lstate
lmem *lmem
ltask *ltask
cctrl *cctrl
rejectFunc http.HandlerFunc
finish chan bool
debug bool
}
func (l *limiter) ptick() {
tick := time.NewTicker(time.Second)
go func() {
for c := range tick.C {
log.Println("resetting counter", c)
l.rcounter()
}
}()
}
func (l *limiter) pschedule() {
for {
select {
case <-l.finish:
return
default:
l.ltask.Lock()
if len(l.ltask.tasks) > 0 {
stat, ntask := l.ltask.tasks[0], l.ltask.tasks[1:]
l.ltask.tasks = ntask
l.add(stat)
l.wupdate(stat)
l.balance(stat)
}
l.ltask.Unlock()
}
}
}
func (l *limiter) allow() bool {
l.lcount.Lock()
l.lcap.Lock()
defer l.lcount.Unlock()
defer l.lcap.Unlock()
return l.lcount.count < int64(math.Ceil(l.lcap.window))
}
func (l *limiter) next(code int) error {
l.ltask.Lock()
defer l.ltask.Unlock()
if len(l.ltask.tasks) > MaxTask {
return errors.New("task buffer exceeded")
}
l.ltask.tasks = append(l.ltask.tasks, getStatus(code))
return nil
}
func (l *limiter) add(status string) {
l.lcount.Lock()
defer l.lcount.Unlock()
if status == success {
l.lcount.scount++
} else if status == failure {
l.lcount.fcount++
} else {
l.lcount.dcount++
}
}
func (l *limiter) tcount(status string) int64 {
l.lcount.Lock()
defer l.lcount.Unlock()
if status == success {
return l.lcount.scount
} else if status == failure {
return l.lcount.fcount
} else {
return l.lcount.dcount
}
}
func (l *limiter) rcounter() {
l.lcount.Lock()
defer l.lcount.Unlock()
l.lcount.pcount = (l.lcount.pcount + l.lcount.count) / 2
l.lcount.count = 0
l.lcount.dcount = 0
l.lcount.scount = 0
l.lcount.fcount = 0
}
func (l *limiter) wupdate(status string) {
l.lcap.Lock()
defer l.lcap.Unlock()
l.lweight.Lock()
defer l.lweight.Unlock()
l.lstate.Lock()
defer l.lstate.Unlock()
var nw float64
var isDrop bool
switch status {
case failure:
nw = l.remedy()
isDrop = true
case success:
if l.lstate.isDrop {
l.lweight.sw = l.cctrl.flux
l.lweight.fw = 1 - l.cctrl.flux
l.lstate.isDrop = false
}
if l.lcap.window < l.lcap.thres {
nw = l.slow()
} else {
nw = l.congavd()
}
}
if l.lcap.window < 1.0 {
nw = 1.0
}
l.lstate.lwindow = l.lcap.window
l.lcap.window = nw
if isDrop {
if l.lcap.window < l.lstate.lwindow && !l.lstate.isDrop {
l.lmem.Lock()
defer l.lmem.Unlock()
l.lcount.Lock()
defer l.lcount.Unlock()
l.lmem.wdrop = append(l.lmem.wdrop, float64(l.lcount.pcount))
var total float64
for _, v := range l.lmem.wdrop {
total += v
}
l.lcap.thres = total / float64(len(l.lmem.wdrop))
l.debugln(fmt.Sprint("new threshold", l.lcap.thres))
l.lstate.isDrop = true
}
}
}
func (l *limiter) balance(nstat string) {
l.lweight.Lock()
defer l.lweight.Unlock()
if nstat == success {
if l.lweight.sw < l.cctrl.flux || l.lweight.sw+l.cctrl.flux > 1.0 {
return
}
l.lweight.sw += l.cctrl.flux
l.lweight.fw -= l.cctrl.flux
return
}
if nstat == failure {
if l.lweight.fw < l.cctrl.flux || l.lweight.fw+l.cctrl.flux > 1.0 {
return
}
l.lweight.fw += l.cctrl.flux
l.lweight.sw -= l.cctrl.flux
return
}
}
func (l *limiter) slow() float64 {
return l.lcap.window + (l.cctrl.sincr * l.lweight.sw)
}
func (l *limiter) congavd() float64 {
return l.lcap.window + (l.cctrl.sincr*l.lweight.sw)/l.lcap.window
}
func (l *limiter) remedy() float64 {
return (l.lcap.window * l.lweight.fw) / l.cctrl.fdcr
}
func (l *limiter) debugln(str string) {
if l.debug {
log.Println(str)
}
}