-
Notifications
You must be signed in to change notification settings - Fork 5
/
crawler.go
166 lines (144 loc) · 3.03 KB
/
crawler.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
// Package crawler provides a flexible web crawler.
package crawler
import (
"errors"
"net/url"
"sync"
"gopkg.in/inconshreveable/log15.v2"
"github.com/fanyang01/crawler/urlx"
)
// Crawler crawls web pages.
type Crawler struct {
ctrl Controller
store Store
opt *Option
logger log15.Logger
maker *maker
fetcher *fetcher
handler *handler
scheduler *scheduler
normalize func(*url.URL) error
quit chan struct{}
wg sync.WaitGroup
}
// New creates a new crawler.
func New(cfg *Config) *Crawler {
cfg = initConfig(cfg)
cw := &Crawler{
opt: cfg.Option,
store: storeWrapper{cfg.Store},
ctrl: cfg.Controller,
logger: cfg.Logger,
normalize: cfg.NormalizeURL,
quit: make(chan struct{}),
}
// connect each part
cw.maker = cw.newRequestMaker()
cw.fetcher = cw.newFetcher()
cw.handler = cw.newRespHandler()
cw.scheduler = cw.newScheduler(cfg.Queue)
// normal flow
cw.maker.In = cw.scheduler.Out
cw.fetcher.In = cw.maker.Out
cw.handler.In = cw.fetcher.Out
cw.scheduler.In = cw.handler.Out
// additional flow
cw.maker.ErrOut = cw.scheduler.ErrIn
cw.maker.CancelOut = cw.scheduler.CancelIn
cw.fetcher.ErrOut = cw.scheduler.ErrIn
cw.handler.ErrOut = cw.scheduler.ErrRespIn
return cw
}
// Crawl starts the crawler using several seeds.
func (cw *Crawler) Crawl(seeds ...string) (err error) {
cw.wg.Add(4)
start(cw.maker)
start(cw.fetcher)
start(cw.handler)
start(cw.scheduler)
nr, err := cw.recover()
if err != nil {
cw.logger.Error("failed to recover from persistent storage", "err", err)
cw.Stop()
return
}
ns, err := cw.addSeeds(seeds...)
if err != nil {
cw.logger.Error("add seeds", "err", err)
cw.Stop()
return
}
if nr+ns <= 0 {
cw.Stop()
}
return nil
}
func (cw *Crawler) recover() (n int, err error) {
ps, ok := cw.store.(PersistableStore)
if !ok {
return
}
var (
ch = make(chan *url.URL, 1024)
chErr = make(chan error, 1)
cnt int
)
go func() {
err := ps.Recover(ch)
close(ch)
chErr <- err
}()
for u := range ch {
cw.scheduler.RecoverIn <- u
cnt++
}
return cnt, <-chErr
}
func (cw *Crawler) Wait() error {
cw.wg.Wait()
return cw.store.Close()
}
func (cw *Crawler) addSeeds(seeds ...string) (n int, err error) {
if len(seeds) == 0 {
return 0, errors.New("crawler: no seed provided")
}
for _, seed := range seeds {
var u *url.URL
var ok bool
if u, err = urlx.Parse(seed, cw.normalize); err != nil {
return
}
if ok, err = cw.store.PutNX(&URL{
URL: *u,
}); err != nil {
return
} else if ok {
cw.scheduler.NewIn <- u
n++
}
}
return
}
// Enqueue adds urls to queue.
func (cw *Crawler) Enqueue(urls ...string) error {
for _, u := range urls {
uu, err := urlx.Parse(u, cw.normalize)
if err != nil {
return err
}
if ok, err := cw.store.PutNX(&URL{
URL: *uu,
}); err != nil {
return err
} else if ok {
cw.scheduler.NewIn <- uu
}
}
return nil
}
// Stop stops the crawler.
func (cw *Crawler) Stop() {
close(cw.quit)
cw.wg.Wait()
}
func (cw *Crawler) Logger() log15.Logger { return cw.logger }