-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
340 lines (285 loc) · 8.97 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/cactus/go-statsd-client/v5/statsd"
"github.com/valyala/fasthttp"
"golang.org/x/net/html"
)
type Crawler struct {
TTL time.Duration
UserAgent string
Stack []*Task
TaskChan chan *Task
Wg sync.WaitGroup
Q Queue
V Visited
P LinkParser
DoSaveContent bool
StorePath string
statsd statsd.Statter
Pool sync.Pool
}
func (c *Crawler) EnqeueMany(t *Task, ch <-chan *Link, wg *sync.WaitGroup) (int, int) {
start := time.Now()
defer c.statsd.TimingDuration("queue.enqueuemany", time.Since(start), 1.0, statsd.Tag{"domain", *Domain})
added := 0
total := 0
for link := range ch {
total++
_, err := t.Site.NewTask(link.Href, t.Depth+1)
if err != nil {
wg.Done()
continue
}
go func() {
defer wg.Done()
// c.Enqueue(nt)
}()
added++
}
return added, total
}
func (c *Crawler) Crawl(WorkerNo int) {
c.statsd.Inc("crawl.req.total", 1, 1.0, statsd.Tag{"domain", *Domain})
Log.Info("crawl.req.total", "qsize", c.Q.Size(context.Background()))
start := time.Now()
task, err := c.Q.Take(context.Background())
c.statsd.TimingDuration("queue.take", time.Since(start), 1.0, statsd.Tag{"domain", *Domain})
if err != nil {
time.Sleep(1 * time.Second)
Log.Error("error", "w", WorkerNo, "err", err, "qsize", c.Q.Size(context.Background()))
c.statsd.Inc("err", 1, 1.0, statsd.Tag{"domain", *Domain}, statsd.Tag{"type", "queue-take"})
return
}
startCheck := time.Now()
if c.V.Exists(context.Background(), task.Uri); err != nil {
Log.Info("already visited", "uri", task.Uri)
c.statsd.Inc("crawl.req.alreadyvisited", 1, 1.0, statsd.Tag{"domain", *Domain})
return
}
c.statsd.TimingDuration("cache.check", time.Since(startCheck), 1.0, statsd.Tag{"domain", *Domain})
Log.Info("crawl task", "w", WorkerNo, "task.uri", task.Uri, "task.depth", task.Depth, "qsize", c.Q.Size(context.Background()))
reader, err := c.Get(task.GetUrl())
if err != nil {
c.Q.TaskDone(context.Background())
Log.Error("error", "w", WorkerNo, "err", err)
c.statsd.Inc("err", 1, 1.0, statsd.Tag{"domain", *Domain}, statsd.Tag{"type", "http-request"})
return
}
go func() {
startPut := time.Now()
c.V.Add(context.Background(), task.Uri)
c.statsd.TimingDuration("cache.put", time.Since(startPut), 1.0, statsd.Tag{"domain", *Domain})
}()
c.P.ParseLinks(reader, task)
saveStart := time.Now()
if c.DoSaveContent {
go func() {
dirPath := fmt.Sprintf("%s/%s", c.StorePath, task.GetSubTree())
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
Log.Error("error", "w", WorkerNo, "err", err)
}
f, err := os.Create(fmt.Sprintf("%s/%s", dirPath, task.GetFilename()))
if err != nil {
panic(err)
}
defer f.Close()
reader.Seek(0, 0)
bytesWrote, err := reader.WriteTo(f)
if err != nil {
panic(err)
}
Log.Info("save content", "w", WorkerNo, "path", c.StorePath, "bytes", bytesWrote)
c.statsd.Inc("crawl.req.saved", 1, 1.0, statsd.Tag{"domain", *Domain})
}()
}
c.statsd.TimingDuration("save", time.Since(saveStart), 1.0, statsd.Tag{"domain", *Domain})
c.Q.TaskDone(context.Background())
c.statsd.TimingDuration("crawl", time.Since(start), 1.0, statsd.Tag{"domain", *Domain})
c.statsd.Inc("crawl.req.ok", 1, 1.0, statsd.Tag{"domain", *Domain})
Log.Info("task done", "w", WorkerNo, "exec-time", time.Since(start))
}
func (c *Crawler) Get(Url string) (*bytes.Reader, error) {
c.statsd.Inc("crawl.client.total", 1, 1.0, statsd.Tag{"domain", *Domain})
start := time.Now()
req := fasthttp.AcquireRequest()
req.SetRequestURI(Url)
req.Header.SetMethod(fasthttp.MethodGet)
req.Header.SetUserAgent(c.UserAgent)
resp := fasthttp.AcquireResponse()
client := c.Pool.Get().(*fasthttp.Client)
err := client.DoRedirects(req, resp, 5)
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
if err != nil {
c.statsd.Inc("crawl.client.err", 1, 1.0, statsd.Tag{"domain", *Domain})
return nil, err
}
c.statsd.Inc("crawl.client.code", 1, 1.0, statsd.Tag{"domain", *Domain}, statsd.Tag{"http_code", strconv.Itoa(resp.StatusCode())})
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("url=%s code=%d", Url, resp.StatusCode())
}
c.statsd.TimingDuration("crawl.client", time.Since(start), 1.0, statsd.Tag{"domain", *Domain})
c.statsd.Inc("crawl.client.ok", 1, 1.0, statsd.Tag{"domain", *Domain})
return bytes.NewReader(resp.Body()), nil
}
func getHref(t html.Token) (ok bool, href string) {
for _, a := range t.Attr {
if a.Key == "href" {
href = a.Val
ok = true
}
}
return
}
func (c *Crawler) Parse(reader *bytes.Reader, ch chan<- *Link, wg *sync.WaitGroup) []*Link {
start := time.Now()
defer c.statsd.Timing("crawl.parser", int64(time.Since(start)), 1.0, statsd.Tag{"domain", *Domain})
tokenizer := html.NewTokenizer(reader)
links := make([]*Link, 0)
var l *Link
for {
tt := tokenizer.Next()
switch {
case tt == html.ErrorToken:
return links
case tt == html.StartTagToken:
t := tokenizer.Token()
// Check if the token is an <a> tag
isAnchor := t.Data == "a"
if !isAnchor {
continue
}
l = NewLink()
// Extract the href value, if there is one
ok, url := getHref(t)
if !ok {
continue
}
l.Href = url
case tt == html.TextToken:
t := tokenizer.Token()
if l != nil && t.Data != "" {
l.Anchor = append(l.Anchor, t.Data)
}
case tt == html.EndTagToken:
t := tokenizer.Token()
isAnchor := t.Data == "a"
if !isAnchor {
continue
}
if l != nil {
// ch <- l
// wg.Add(1)
links = append(links, l)
l = nil
}
}
}
}
func (c *Crawler) Run(MaxGoroutine int) {
Log.Info("start crawler.run", "max", MaxGoroutine)
guard := make(chan struct{}, MaxGoroutine)
i := 0
for c.Q.Size(context.Background()) > 0 {
guard <- struct{}{}
go func(no int) {
c.Crawl(no)
<-guard
}(i)
i++
if i >= *Limit {
break
}
}
Log.Info("stop crawler.run", "max", MaxGoroutine)
}
func (c *Crawler) Enqueue(t *Task) error {
start := time.Now()
defer c.statsd.TimingDuration("queue.enqueue", time.Since(start), 1.0, statsd.Tag{"domain", *Domain})
startCheck := time.Now()
if c.V.Exists(context.Background(), t.Uri) {
return fmt.Errorf("uri=%s already visited or enqueued", t.Uri)
}
c.statsd.TimingDuration("cache.check", time.Since(startCheck), 1.0, statsd.Tag{"domain", *Domain})
startPut := time.Now()
c.V.Add(context.Background(), t.Uri)
c.statsd.TimingDuration("cache.put", time.Since(startPut), 1.0, statsd.Tag{"domain", *Domain})
go func() {
startPut = time.Now()
c.Q.Put(context.Background(), t)
c.statsd.TimingDuration("queue.put", time.Since(startPut), 1.0, statsd.Tag{"domain", *Domain})
c.statsd.Gauge("crawl.queue.size", int64(c.Q.Size(context.Background())), 1.0, statsd.Tag{"domain", *Domain})
}()
return nil
}
func NewCrawler(Q Queue, V Visited, StorePath string, statsd statsd.Statter) *Crawler {
ttl, _ := time.ParseDuration("30m")
readTimeout, _ := time.ParseDuration("6000ms")
writeTimeout, _ := time.ParseDuration("6000ms")
maxIdleConnDuration, _ := time.ParseDuration("1h")
maxConnWaitTimeout, _ := time.ParseDuration("6000ms")
cwlr := &Crawler{
TTL: ttl,
UserAgent: GetUserAgent(*UseGooglebot),
// Clients: make([]*fasthttp.Client, len(proxies)),
Stack: make([]*Task, 0),
TaskChan: make(chan *Task),
V: V,
Q: Q,
P: &HtmlLinkParser{q: Q, stats: statsd},
DoSaveContent: !*DoNotStore,
StorePath: StorePath,
Pool: sync.Pool{
New: func() interface{} {
// proxy := proxies[LastProxy%len(proxies)]
LastProxy++
// dial := fasthttpproxy.FasthttpHTTPDialerTimeout(proxy, writeTimeout)
// if *DoNotUseProxy {
dial := (&fasthttp.TCPDialer{
Concurrency: 4096,
DNSCacheDuration: time.Hour,
}).Dial
// }
return &fasthttp.Client{
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxIdleConnDuration: maxIdleConnDuration,
MaxConnWaitTimeout: maxConnWaitTimeout,
NoDefaultUserAgentHeader: true,
DisableHeaderNamesNormalizing: true,
DisablePathNormalizing: true,
Dial: dial,
}
},
},
}
// for pos, proxyStr := range proxies {
// dial := fasthttpproxy.FasthttpHTTPDialerTimeout(proxyStr, writeTimeout)
// if *DoNotUseProxy {
// dial = (&fasthttp.TCPDialer{
// Concurrency: 4096,
// DNSCacheDuration: time.Hour,
// }).Dial
// }
// cwlr.Clients[pos] = &fasthttp.Client{
// ReadTimeout: readTimeout,
// WriteTimeout: writeTimeout,
// MaxIdleConnDuration: maxIdleConnDuration,
// MaxConnWaitTimeout: maxConnWaitTimeout,
// NoDefaultUserAgentHeader: true,
// DisableHeaderNamesNormalizing: true,
// DisablePathNormalizing: true,
// Dial: dial,
// }
// }
return cwlr
}