/
crawler.go
154 lines (130 loc) · 2.79 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
package crawler
import (
"net/http"
"fmt"
"log"
"runtime"
"time"
"io/ioutil"
"github.com/a2n/alu"
)
/*
* TODO
*
* - Make the concurrent requesting depends on bandwidth.
*
*/
type Config struct {
Email string
URL string
Concurrency int
}
type Crawler struct {
logger *log.Logger
config *Config
user_agent string
Response chan *Response
queue []*http.Request
// Clients
clients []*client
currentClient *client
}
func NewCrawler(config *Config) *Crawler {
runtime.GOMAXPROCS(runtime.NumCPU())
c := &Crawler {
user_agent: fmt.Sprintf("%s %s", config.Email, config.URL),
Response: make(chan *Response, config.Concurrency),
queue: make([]*http.Request, 0),
config: config,
logger: alu.NewLogger("crawler.log"),
}
/*
* TODO
* The transport of client supports MaxIdleConnsPerHost property.
* It controls the concurrent connections,use few transports instead of
* clients.
*
*/
// Init clients
c.clients = make([]*client, 0)
for i := 0; i < config.Concurrency; i++ {
client := NewClient(c.logger, uint8(i), c.Response)
c.clients = append(c.clients, client)
}
currentClient := c.clients[0]
for i := 0; i < len(c.clients); i++ {
if i != len(c.clients) - 1{
c.clients[i].Next = c.clients[i + 1]
} else {
c.clients[i].Next = c.clients[0]
}
}
c.currentClient = currentClient
return c
}
func (c *Crawler) Push(r *http.Request) {
r.Header.Add("User-Agent", c.user_agent)
c.currentClient.Push(r)
c.currentClient = c.currentClient.Next
}
type client struct {
logger *log.Logger
index uint8
client *http.Client
queue []*http.Request
Response chan *Response
Next *client
}
func NewClient(l *log.Logger, idx uint8, ch chan *Response) *client {
c := &client {
logger: l,
index: idx,
client: &http.Client{},
queue: make([]*http.Request, 0),
Response: ch,
}
go func() {
for {
select {
case <- time.Tick(250 * time.Microsecond):
if len(c.queue) > 0 {
c.fire()
}
}
}
}()
return c
}
func (c *client) Push(r *http.Request) {
c.queue = append(c.queue, r)
}
type Response struct {
Header http.Header
Body []byte
Request *http.Request
StatusCode int
}
func (c *client) fire() {
req := c.queue[0]
c.logger.Printf("client %d, begin to request %s.", c.index, req.URL.String())
resp, err := c.client.Do(req)
c.queue = append(c.queue[:0], c.queue[1:]...)
if err != nil {
c.logger.Printf("client %d has error, %s.", c.index, err.Error())
return
}
c.logger.Printf("client %d, finished requesting %s.", c.index, req.URL.String())
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
c.logger.Printf("client %d has error, %s.", c.index, err.Error())
return
}
my_resp := &Response {
Header: resp.Header,
Body: b,
Request: resp.Request,
StatusCode: resp.StatusCode,
}
c.Response <- my_resp
}