-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschedule.go
313 lines (281 loc) · 6.28 KB
/
schedule.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
package crawler
import (
"net/url"
"sync"
"time"
"golang.org/x/net/context"
"github.com/fanyang01/crawler/queue"
)
type scheduler struct {
workerConn
cw *Crawler
NewIn chan *url.URL
RecoverIn chan *url.URL
CancelIn chan *Context
ErrIn chan *Context
Out chan *Context
In chan *Response
ErrRespIn chan *Response
queue queue.WaitQueue
queueIn chan<- *queue.Item
queueOut <-chan *queue.Item
queueErr <-chan error
stop chan struct{}
once sync.Once // used for closing Out
}
func (cw *Crawler) newScheduler(wq queue.WaitQueue) *scheduler {
nworker := cw.opt.NWorker.Scheduler
queueIn, queueOut, queueErr := wq.Channel()
this := &scheduler{
cw: cw,
NewIn: make(chan *url.URL, nworker),
RecoverIn: make(chan *url.URL, nworker),
ErrIn: make(chan *Context, nworker),
CancelIn: make(chan *Context, nworker),
ErrRespIn: make(chan *Response, nworker),
Out: make(chan *Context, nworker),
queue: wq,
queueIn: queueIn,
queueOut: queueOut,
queueErr: queueErr,
stop: make(chan struct{}),
}
cw.initWorker("scheduler", this, nworker)
return this
}
func (sd *scheduler) work() {
var (
queueIn chan<- *queue.Item
waiting = make([]*queue.Item, 0, perPage)
next *queue.Item
out chan<- *Context
first *Context
outFIFO []*Context
)
for {
if len(outFIFO) != 0 {
out = sd.Out
first = outFIFO[0]
}
if len(waiting) > 0 {
queueIn = sd.queueIn
next = waiting[0]
}
var (
item *queue.Item
done, ok bool
err error
)
select {
// Input:
case u := <-sd.NewIn:
item = sd.sched(nil, u)
waiting = append(waiting, item)
continue
case u := <-sd.RecoverIn:
item = sd.sched(nil, u)
waiting = append(waiting, item)
continue
case ctx := <-sd.CancelIn:
if err = sd.cw.store.Complete(ctx.url); err != nil {
goto ERROR
}
sd.logger.Info(
"complete due to canceled request",
"url", ctx.url,
)
case ctx := <-sd.ErrIn:
switch ctx.err.(type) {
case FatalError, *FatalError:
err = ctx.err
goto ERROR
case RetryableError, *RetryableError:
if item, ok, err = sd.retry(ctx); err != nil {
goto ERROR
} else if ok {
waiting = append(waiting, item)
continue
}
default:
if err = sd.cw.store.Complete(ctx.url); err != nil {
goto ERROR
}
sd.logger.Error(
"complete due to unknown error",
"err", ctx.err, "url", ctx.url,
)
}
case resp, ok := <-sd.In:
if !ok {
sd.exit()
return // closed
}
sd.cw.store.IncVisitCount()
for _, url := range resp.links {
item = sd.sched(resp, url)
waiting = append(waiting, item)
}
item, done, err = sd.resched(resp)
if err != nil {
goto ERROR
} else if !done {
waiting = append(waiting, item)
continue
}
case resp := <-sd.ErrRespIn:
// NOTE: even if an error occured, links found in the response
// should still be enqueued, because the state of storage has
// been changed.
for _, url := range resp.links {
item = sd.sched(resp, url)
waiting = append(waiting, item)
}
switch resp.ctx.err.(type) {
case FatalError, *FatalError:
err = resp.ctx.err
goto ERROR
case RetryableError, *RetryableError:
if item, ok, err = sd.retry(resp.ctx); err != nil {
goto ERROR
} else if ok {
waiting = append(waiting, item)
continue
}
default:
sd.logger.Error(
"complete due to unknown error",
"err", resp.ctx.err, "url", resp.URL,
)
if err = sd.cw.store.Complete(resp.URL); err != nil {
goto ERROR
}
}
case item := <-sd.queueOut:
if item == nil { // queue has been closed
return
}
var ctx *Context
if ctx, err = sd.cw.newContext(item.URL, item.Ctx); err != nil {
goto ERROR
}
item.Free()
outFIFO = append(outFIFO, ctx)
// Output:
case queueIn <- next:
if waiting = waiting[1:]; len(waiting) == 0 {
queueIn = nil
}
case out <- first:
if outFIFO = outFIFO[1:]; len(outFIFO) == 0 {
out = nil
}
// Control:
case err = <-sd.queueErr:
if err != nil {
goto ERROR
}
return
case <-sd.stop:
return
case <-sd.quit:
return
}
if ok, err = sd.cw.store.IsFinished(); err != nil {
goto ERROR
} else if ok {
sd.exit()
return
}
continue
ERROR:
sd.logger.Crit("unrecoverable error, exiting...", "err", err)
sd.exit() // notify other scheduler goroutines to exit.
return
}
}
func (sd *scheduler) cleanup() {
close(sd.Out)
close(sd.queueIn)
if err := sd.queue.Close(); err != nil {
sd.logger.Error("close wait queue", "err", err)
}
select {
case <-sd.quit: // closed
default:
close(sd.quit)
}
}
func (sd *scheduler) exit() {
sd.once.Do(func() { close(sd.stop) })
}
func (sd *scheduler) sched(r *Response, u *url.URL) *queue.Item {
item := queue.NewItem()
item.URL = u
t := sd.cw.ctrl.Sched(r, u)
if t.Ctx == nil {
t.Ctx = context.Background()
}
item.Next, item.Score, item.Ctx = t.At, t.Score, t.Ctx
return item
}
func (sd *scheduler) resched(r *Response) (
item *queue.Item, done bool, err error,
) {
defer func() {
r.ctx.free()
r.free()
}()
var last time.Time
if err = sd.cw.store.UpdateFunc(r.URL, func(u *URL) {
u.NumVisit++
u.NumRetry = 0
last = u.Last
u.Last = r.Timestamp
}); err != nil {
return
}
var t Ticket
done, t = sd.cw.ctrl.Resched(r)
if done {
err = sd.cw.store.Complete(r.URL)
return
} else if t.Ctx == nil {
t.Ctx = context.Background()
}
item = queue.NewItem()
item.URL = r.URL
item.Next, item.Score, item.Ctx = t.At, t.Score, t.Ctx
min := last.Add(sd.cw.opt.MinDelay)
if item.Next.Before(min) {
item.Next = min
}
return
}
func (sd *scheduler) retry(ctx *Context) (*queue.Item, bool, error) {
defer ctx.free()
var cnt int
if err := sd.cw.store.UpdateFunc(ctx.url, func(uu *URL) {
uu.NumRetry++
cnt = uu.NumRetry
}); err != nil {
return nil, false, err
}
delay, max := sd.cw.ctrl.Retry(ctx)
if cnt >= max {
sd.logger.Error(
"exceed maximum number of retries",
"err", ctx.err, "url", ctx.url, "retries", cnt,
)
err := sd.cw.store.Complete(ctx.url)
return nil, false, err
}
sd.logger.Error(
"retry due to error",
"err", ctx.err, "url", ctx.url, "retries", cnt,
)
item := queue.NewItem()
item.URL = ctx.url
item.Ctx = ctx.C
item.Next = time.Now().Add(delay)
return item, true, nil
}