-
Notifications
You must be signed in to change notification settings - Fork 20
/
crawler.go
145 lines (124 loc) · 3.32 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
// This program demonstrates how to write a web crawler with a single worker and multiple concurrent workers
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
)
type Response struct {
Body string
Error error
}
func fetch(url string) *Response {
resp, _ := http.Get(url)
body, err := ioutil.ReadAll(resp.Body)
return &Response{
Body: string(body),
Error: err,
}
}
func main() {
urls := []string{
"http://www.mastergoco.com/index1.html",
"http://www.mastergoco.com/index2.html",
"http://www.mastergoco.com/index3.html",
"http://www.mastergoco.com/index4.html",
"http://www.mastergoco.com/index5.html",
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Crawl pages using only one worker
start0 := time.Now()
for o := range singleWorker(ctx, urls...) {
res := o.(*Response)
if res.Error != nil {
log.Println("error at single worker:", res.Error)
continue
}
log.Println("single worker done", len(res.Body))
}
log.Println("single worker completed in", time.Since(start0))
log.Println("")
// Crawl pages using multiple workers
numWorkers := 4
start1 := time.Now()
for o := range multiWorkers(ctx, numWorkers, toStream(ctx, urls...)) {
res := o.(*Response)
if res.Error != nil {
log.Println("error at multi worker:", res.Error)
continue
}
log.Println("multi worker done", len(res.Body))
}
log.Println("multi workers completed in", time.Since(start1))
}
func singleWorker(ctx context.Context, urls ...string) <-chan interface{} {
outStream := make(chan interface{})
go func() {
defer close(outStream)
for _, url := range urls {
outStream <- fetch(url)
}
}()
return outStream
}
func multiWorkers(ctx context.Context, numWorkers int, inStream <-chan interface{}) <-chan interface{} {
outStream := make(chan interface{})
var wg sync.WaitGroup
wg.Add(numWorkers)
multiplex := func(index int, in <-chan interface{}) {
for o := range in {
select {
case <-ctx.Done():
return
case outStream <- fetch(o.(string)):
log.Println("processed by worker", index)
}
}
wg.Done()
}
for i := 0; i < numWorkers; i++ {
go multiplex(i, inStream)
}
go func() {
wg.Wait()
close(outStream)
}()
return outStream
}
func toStream(ctx context.Context, urls ...string) <-chan interface{} {
outStream := make(chan interface{})
go func() {
for _, v := range urls {
select {
case <-ctx.Done():
return
case outStream <- v:
}
}
close(outStream)
}()
return outStream
}
// 2018/03/29 11:47:50 single worker done 5943
// 2018/03/29 11:47:50 single worker done 5943
// 2018/03/29 11:47:51 single worker done 5943
// 2018/03/29 11:47:51 single worker done 5943
// 2018/03/29 11:47:52 single worker done 5943
// 2018/03/29 11:47:52 single worker completed in 3.005570534s
// 2018/03/29 11:47:52
// 2018/03/29 11:47:53 processed by worker 3
// 2018/03/29 11:47:53 multi worker done 5943
// 2018/03/29 11:47:53 processed by worker 1
// 2018/03/29 11:47:53 multi worker done 5943
// 2018/03/29 11:47:53 processed by worker 0
// 2018/03/29 11:47:53 multi worker done 5943
// 2018/03/29 11:47:53 processed by worker 2
// 2018/03/29 11:47:53 multi worker done 5943
// 2018/03/29 11:47:53 processed by worker 3
// 2018/03/29 11:47:53 multi worker done 5943
// 2018/03/29 11:47:53 multi workers completed in 1.140311726s