-
Notifications
You must be signed in to change notification settings - Fork 5
/
make.go
71 lines (64 loc) · 1.38 KB
/
make.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
package crawler
import (
"net/http"
"strings"
)
type maker struct {
workerConn
In <-chan *Context
ErrOut chan<- *Context
CancelOut chan<- *Context
Out chan *Request
cw *Crawler
}
func (cw *Crawler) newRequestMaker() *maker {
nworker := cw.opt.NWorker.Maker
this := &maker{
Out: make(chan *Request, nworker),
cw: cw,
}
cw.initWorker("maker", this, nworker)
return this
}
func (m *maker) newRequest(ctx *Context) (req *Request, err error) {
req = &Request{ctx: ctx}
if req.Request, err = http.NewRequest("GET", ctx.url.String(), nil); err != nil {
return nil, err
}
m.cw.ctrl.Prepare(req)
if err = req.ctx.err; err != nil {
return nil, err
}
req.Method = strings.ToUpper(req.Method)
if req.Client == nil {
req.Client = DefaultClient
}
return
}
func (m *maker) cleanup() { close(m.Out) }
func (m *maker) work() {
for ctx := range m.In {
var (
logger = m.logger.New("url", ctx.url)
out = m.Out
errOut chan<- *Context
cancelOut chan<- *Context
req *Request
err error
)
if req, err = m.newRequest(ctx); err != nil {
out, errOut = nil, m.ErrOut
logger.Error("make request", "err", err)
} else if req.cancel {
out, cancelOut = nil, m.CancelOut
logger.Info("request canceled")
}
select {
case out <- req:
case errOut <- ctx:
case cancelOut <- ctx:
case <-m.quit:
return
}
}
}