forked from nfx/slrp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshard.go
300 lines (279 loc) · 7.11 KB
/
shard.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
package pool
import (
"context"
"fmt"
"math/rand"
"net/http"
"sort"
"time"
"github.com/nfx/slrp/app"
"github.com/nfx/slrp/pmux"
"github.com/corpix/uarand"
)
type incoming struct {
ctx context.Context
Proxy pmux.Proxy
Speed time.Duration
}
type request struct {
in *http.Request
out chan *http.Response
start time.Time
attempt int
serial int
}
type reply struct {
r request
response *http.Response
start time.Time
e *entry
err error
}
type work struct {
r request
e *entry
reply chan reply
}
type removal struct {
proxy pmux.Proxy
reply chan bool
}
type shard struct {
Entries []*entry
incoming chan incoming
remove chan removal
requests chan request
snapshot chan chan []*entry
reanimate chan bool
reply chan reply
work chan work //todo channel in pool
minute *time.Ticker
evictions []pmux.Proxy
eviction chan chan []pmux.Proxy
config *monitorConfig
}
func (pool *shard) init(config *monitorConfig, work chan work) {
pool.work = work
pool.incoming = make(chan incoming)
pool.remove = make(chan removal)
pool.requests = make(chan request)
pool.reanimate = make(chan bool)
pool.snapshot = make(chan chan []*entry)
pool.reply = make(chan reply)
pool.eviction = make(chan chan []pmux.Proxy)
pool.minute = time.NewTicker(1 * time.Minute)
pool.config = config
}
func (pool *shard) main(ctx app.Context) {
for {
select {
case <-ctx.Done():
return
case res := <-pool.snapshot:
res <- pool.Entries
case <-pool.minute.C:
if pool.handleReanimate() {
ctx.Heartbeat()
}
if pool.checkEviction(ctx.Ctx()) {
ctx.Heartbeat()
}
case <-pool.reanimate:
pool.forceReanimate()
case v := <-pool.remove:
pool.removeProxy(v)
ctx.Heartbeat()
case v := <-pool.incoming:
pool.add(v)
ctx.Heartbeat()
case r := <-pool.requests:
pool.handleRequest(r)
case r := <-pool.reply:
pool.handleReply(r)
case r := <-pool.eviction:
r <- pool.evictions
pool.evictions = []pmux.Proxy{}
}
}
}
func (pool *shard) checkEviction(ctx context.Context) bool {
log := app.Log.From(ctx)
replace := []*entry{}
evict := []pmux.Proxy{}
thresholdTimeouts := pool.config.evictThresholdTimeouts
thresholdFailures := pool.config.evictThresholdFailures
thresholdReanimations := pool.config.evictThresholdReanimations
longerSleep := pool.config.longTimeoutSleep
for i := range pool.Entries {
e := pool.Entries[i]
if e.Eviction(thresholdTimeouts, thresholdFailures, thresholdReanimations, longerSleep) {
proxy := e.Proxy
log.Info().
Stringer("proxy", proxy).
Int("shard", proxy.Bucket(pool.config.shards)).
Msg("evicting from shard")
evict = append(evict, proxy)
} else {
replace = append(replace, e)
}
}
if len(evict) > 0 {
pool.Entries = replace
pool.evictions = append(pool.evictions, evict...)
return true
}
return false
}
func (pool *shard) handleReanimate() bool {
heartbeat := false
for i := range pool.Entries {
if pool.Entries[i].ReanimateIfNeeded() {
heartbeat = true
}
}
return heartbeat
}
func (pool *shard) forceReanimate() {
for i := range pool.Entries {
pool.Entries[i].ForceReanimate()
}
}
func (pool *shard) removeProxy(r removal) {
newEntries := []*entry{}
found := false
for _, v := range pool.Entries {
if v.Proxy == r.proxy {
found = true
continue
}
local := v
newEntries = append(newEntries, local)
}
pool.Entries = newEntries
if found {
log := app.Log.From(context.TODO())
log.Info().Stringer("proxy", r.proxy).Msg("removed")
}
r.reply <- found
}
func (pool *shard) add(v incoming) {
pool.Entries = append(pool.Entries, newEntry(v.Proxy, v.Speed, int16(pool.config.evictSpanMinutes)))
sort.Slice(pool.Entries, func(i, j int) bool {
return pool.Entries[i].Speed < pool.Entries[j].Speed
})
log := app.Log.From(v.ctx)
log.Info().Stringer("proxy", v.Proxy).Dur("speed", v.Speed).Msg("added")
}
func (pool *shard) firstAvailableProxy(r request) *entry {
size := len(pool.Entries)
if size == 0 {
return nil
}
if size == 1 {
return pool.Entries[0]
}
// offset := 0
// defaultSorting(pool.Entries)
offset := rand.Intn(size)
// offset := r.serial % len(pool.Entries)
// TODO: per-request offset selection strategy -
// scrapes are encouraged to refresh the old or
// the least offered proxies, but relays need fresher pool
available := pool.Entries[offset:size]
ctx := r.in.Context()
for idx := range available {
e := pool.Entries[offset+idx]
if e.ConsiderSkip(ctx, pool.config.offerLimit) {
continue
}
return e
}
return nil
}
func (pool *shard) handleRequest(r request) {
// log := app.Log.From(r.in.Context())
r.in.Header.Set("User-Agent", uarand.GetRandom())
entry := pool.firstAvailableProxy(r)
if entry == nil {
// this pool has no entries, try next one
headers := http.Header{}
headers.Add("X-Proxy-Serial", fmt.Sprintf("%d", r.serial))
r.out <- &http.Response{
StatusCode: 552,
Status: "Proxy Pool Exhausted",
Header: headers,
Request: r.in,
}
return
}
// log.Debug().
// Stringer("url", req.URL).
// Stringer("proxy", entry.Proxy).
// Dur("t", time.Since(start)).
// Msg("prepare request")
ctx := entry.Proxy.InContext(r.in.Context())
ctx = app.Log.WithStringer(ctx, "proxy", entry.Proxy)
ctx = app.Log.WithStr(ctx, "serial", fmt.Sprint(r.serial))
r.in = r.in.WithContext(ctx)
go func() {
// send work via goroutine, so we're not deadlocked by work trying to send to handleReply
// this leaks a goroutine to certain extent, but hopefully with throttling on serial channel
// we can overcome this difficulty
select {
case <-ctx.Done():
return
case pool.work <- work{
reply: pool.reply,
e: entry,
r: r,
}: // bada boom ts
}
}()
}
func (pool *shard) handleReply(r reply) {
request := r.r
res := r.response
log := app.Log.From(r.r.in.Context())
err := r.err
entry := r.e
if err == nil && res.StatusCode >= 400 {
err = fmt.Errorf(res.Status)
}
// TODO: special error codes for timeouts
if err == nil {
entry.MarkSuccess()
// TODO: Bytes1D & Bytes5M
res.Header.Set("X-Proxy-Through", entry.Proxy.String())
res.Header.Set("X-Proxy-Attempt", fmt.Sprint(request.attempt))
res.Header.Set("X-Proxy-Offered", fmt.Sprint(entry.Offered))
res.Header.Set("X-Proxy-Succeed", fmt.Sprint(entry.Succeed))
res.Header.Set("X-Proxy-Serial", fmt.Sprint(request.serial))
log.Debug().
Stringer("t", time.Since(request.start)).
Int("offered", entry.Offered).
Msg("forwarded")
r.r.out <- res
return
}
// TODO: if more than 10 failed offers in this hour, mark dead till beginning of next hour
entry.MarkFailure(err, pool.config.shortTimeoutSleep)
log.Debug().
Err(app.ShErr(err)).
Int("timeouts", entry.Timeouts).
Int("offered", entry.Offered).
Stringer("t", time.Since(request.start)).
Msg("forwarding failed")
if request.attempt >= 10 {
headers := http.Header{}
headers.Add("X-Proxy-Serial", fmt.Sprintf("%d", request.serial))
request.out <- &http.Response{
StatusCode: 429,
Status: err.Error(),
Header: headers,
Request: request.in,
}
return
}
// todo: proxies becoming dead (connections rejected, etc)
request.out <- nil
}