-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.go
55 lines (42 loc) · 1.09 KB
/
worker.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
package main
import (
"bytes"
"net/url"
)
type WorkRequest struct {
URL url.URL
Execute func(workerID, numberOfWorkers int) WorkResult
}
func executeWork(workerID, numberOfWorkers int, targetURL crawlerUrl, userAgent string, newURLs chan crawlerUrl) WorkResult {
// read the URL
response, err := readURL(targetURL.url, userAgent)
if err != nil {
return WorkResult{err: err}
}
if response.IsHTML() {
// get dependent links
links, err := getDependentRequests(targetURL.url, bytes.NewReader(response.Body()))
if err != nil {
return WorkResult{err: err}
}
for _, link := range links {
go func(url crawlerUrl) {
newURLs <- url
}(link)
}
}
workResult := WorkResult{
parentURL: targetURL.parent,
url: targetURL.url,
workerID: workerID,
numberOfWorkers: numberOfWorkers,
responseSize: response.Size(),
body: response.Body(),
header: response.Header(),
statusCode: response.StatusCode(),
startTime: response.StartTime(),
endTime: response.EndTime(),
contentType: response.ContentType(),
}
return workResult
}