forked from nfx/slrp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentry.go
264 lines (235 loc) · 6.24 KB
/
entry.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
package pool
import (
"context"
"fmt"
"time"
"github.com/nfx/slrp/app"
"github.com/nfx/slrp/pmux"
"github.com/nfx/slrp/pool/counter"
)
// unexported unit test shim
var now = time.Now
type entry struct {
Proxy pmux.Proxy
FirstSeen int64
LastSeen int64
ReanimateAfter time.Time
Ok bool
Speed time.Duration
Timeouts int
Failures int
Offered int
Reanimated int
Succeed int
OfferShort counter.RollingCounter
SuccessShort counter.RollingCounter
TimeoutShort counter.RollingCounter
FailureShort counter.RollingCounter
Offer1D counter.RollingCounter
Success1D counter.RollingCounter
Timeout1D counter.RollingCounter
Failure1D counter.RollingCounter
HourOffered [24]int
HourSucceed [24]int
}
func newEntry(proxy pmux.Proxy, speed time.Duration, evictSpanMinutes int16) *entry {
now := now()
return &entry{
Ok: true,
Proxy: proxy,
Speed: speed,
FirstSeen: now.Unix(),
LastSeen: now.Unix(),
OfferShort: counter.NewRollingCounter(evictSpanMinutes, time.Minute),
SuccessShort: counter.NewRollingCounter(evictSpanMinutes, time.Minute),
TimeoutShort: counter.NewRollingCounter(evictSpanMinutes, time.Minute),
FailureShort: counter.NewRollingCounter(evictSpanMinutes, time.Minute),
Offer1D: counter.NewRollingCounter(24, time.Hour),
Success1D: counter.NewRollingCounter(24, time.Hour),
Timeout1D: counter.NewRollingCounter(24, time.Hour),
Failure1D: counter.NewRollingCounter(24, time.Hour),
}
}
func (e *entry) MarkSuccess() {
e.SuccessShort.Increment()
e.Success1D.Increment()
now := now()
hour := now.Hour()
if (e.Succeed + 1) < e.Offered {
e.HourSucceed[hour]++
e.Succeed++
} else {
// fix the potential mess
e.HourSucceed[hour] = e.HourOffered[hour]
e.Succeed = e.Offered
}
e.Ok = true
e.LastSeen = now.Unix()
// TODO: verify if prev request success will improve hitrate
e.ReanimateAfter = time.Time{}
}
func (e *entry) MarkFailure(err error, shortTimeoutSleep time.Duration) {
t, ok := err.(interface {
Timeout() bool
})
e.Ok = false
e.ReanimateAfter = now().Add(shortTimeoutSleep)
if ok && t.Timeout() {
e.TimeoutShort.Increment()
e.Timeout1D.Increment()
e.Timeouts++
} else {
e.FailureShort.Increment()
e.Failure1D.Increment()
e.Failures++
}
}
func (e *entry) RequestsComplete() bool {
offers := e.OfferShort.Sum()
if offers == 0 {
return false
}
return offers == e.RequestsShort()
}
func (e *entry) RequestsShort() int {
return e.SuccessShort.Sum() + e.TimeoutShort.Sum() + e.FailureShort.Sum()
}
func (e *entry) SuccessRateShort() float64 {
pass := float64(e.SuccessShort.Sum())
fail := float64(e.RequestsShort())
return pass / fail
}
func (e *entry) RequestsLong() int {
return e.Success1D.Sum() + e.Timeout1D.Sum() + e.Failure1D.Sum()
}
func (e *entry) SuccessRateLong() float64 {
pass := float64(e.Success1D.Sum())
fail := float64(e.RequestsLong())
return pass / fail
}
func (e *entry) Eviction(evictThresholdTimeouts, evictThresholdFailures, evictThresholdReanimations int, longTimeoutSleep time.Duration) bool {
if !e.RequestsComplete() {
return false
}
suceeded := e.SuccessShort.Sum()
timedOut := e.TimeoutShort.Sum()
failed := e.FailureShort.Sum()
if suceeded == 0 && timedOut > evictThresholdTimeouts {
e.Ok = false
e.ReanimateAfter = now().Add(longTimeoutSleep)
// don't evict just yet
return false
}
if suceeded == 0 && failed > evictThresholdFailures {
e.Ok = false
return true
}
if suceeded == 0 && e.Reanimated > evictThresholdReanimations && (timedOut > 0 || failed > 0) {
e.Ok = false
return true
}
return false
}
// TODO: bug in offering: plenty of 551 errors, even though they should have been limited.
// perhaps we can do LastOffered and check it to be more than 3s (checker.Timeout) ago.
func (e *entry) ConsiderSkip(ctx context.Context, offerLimit int) bool {
currentOffers := e.OfferShort.Sum()
if currentOffers > offerLimit {
// there were too many offers in the last 5 minutes
return true
}
if e.SuccessShort.Sum() == 0 && e.FailureShort.Sum() == 3 {
e.Ok = false
e.ReanimateAfter = now().Add(30 * time.Second)
return true
}
now := now()
log := app.Log.From(ctx).
With().
Stringer("proxy", e.Proxy).
Int("offered", e.Offered).
Int("succeed", e.Succeed).
Logger()
if e.ReanimateAfter.After(now) {
log.Trace().Str("until", e.DeadUntil()).Msg("dead")
return true
}
if !e.ReanimateAfter.IsZero() {
e.ReanimateAfter = time.Time{}
e.Reanimated++
log.Trace().Int("count", e.Reanimated).Msg("reanimated")
e.Ok = true
}
if !e.Ok {
// TODO: is this statement even reachable?
log.Trace().Str("until", e.DeadUntil()).Msg("not ok")
return true
}
e.HourOffered[now.Hour()]++
e.Offered += 1
e.OfferShort.Increment()
e.Offer1D.Increment()
log.Trace().Int("new_offered", e.Offered).Msg("offered")
return false
}
func (e *entry) ReanimateIfNeeded() bool {
if e.ReanimateAfter.IsZero() {
return false
}
now := now()
if e.ReanimateAfter.After(now) {
return false
}
e.ReanimateAfter = time.Time{}
e.Reanimated++
e.Ok = true
return true
}
func (e *entry) ForceReanimate() {
e.ReanimateAfter = time.Time{}
if !e.Ok {
e.Reanimated++
e.Ok = true
}
}
func (e *entry) HourSuccessRate() [24]float32 {
res := [24]float32{}
for i := 0; i < 24; i++ {
res[i] = 100 * float32(e.HourSucceed[i]) / float32(e.HourOffered[i])
}
return res
}
func (e *entry) SuccessRate() float32 {
return 100 * float32(e.Succeed) / float32(e.Offered)
}
func (e *entry) TimeoutRate() float32 {
return 100 * float32(e.Timeouts) / float32(e.Offered)
}
func (e *entry) SinceLastSeen() time.Duration {
ls := time.Unix(e.LastSeen, 0)
return time.Since(ls)
}
func (e *entry) LastSeenAgo() string {
return e.SinceLastSeen().Round(time.Second).String()
}
func (e *entry) DeadUntil() string {
if e.ReanimateAfter.IsZero() {
return "-"
}
now := now()
if now.After(e.ReanimateAfter) {
return "-"
}
in := e.ReanimateAfter.Sub(now) * -1
return in.Round(time.Second).String()
}
func (e *entry) String() string {
speed := e.Speed.Round(time.Millisecond)
ok := "+"
if !e.Ok {
ok = "-"
}
return fmt.Sprintf("%32s\t%s\t%s\t%3d%%\t%9s\t%5s\t%3d\t%3d\t%3d",
e.Proxy, speed, ok, int(e.SuccessRate()),
e.LastSeenAgo(), e.DeadUntil(), e.Offered, e.Succeed, e.Reanimated)
}