forked from hishamkaram/colly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
259 lines (236 loc) · 5.54 KB
/
queue.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
package queue
import (
"net/url"
"sync"
"github.com/sam-ulrich1/colly/v2"
)
const stop = true
// Storage is the interface of the queue's storage backend
// Storage must be concurrently safe for multiple goroutines.
type Storage interface {
// Init initializes the storage
Init() error
// AddRequest adds a serialized request to the queue
AddRequest([]byte) error
// GetRequest pops the next request from the queue
// or returns error if the queue is empty
GetRequest() ([]byte, error)
// QueueSize returns with the size of the queue
QueueSize() (int, error)
}
// Queue is a request queue which uses a Collector to consume
// requests in multiple threads
type Queue struct {
// Threads defines the number of consumer threads
Threads int
storage Storage
wake chan struct{}
mut sync.Mutex // guards wake and running
running bool
}
// InMemoryQueueStorage is the default implementation of the Storage interface.
// InMemoryQueueStorage holds the request queue in memory.
type InMemoryQueueStorage struct {
// MaxSize defines the capacity of the queue.
// New requests are discarded if the queue size reaches MaxSize
MaxSize int
lock *sync.RWMutex
size int
first *inMemoryQueueItem
last *inMemoryQueueItem
}
type inMemoryQueueItem struct {
Request []byte
Next *inMemoryQueueItem
}
// New creates a new queue with a Storage specified in argument
// A standard InMemoryQueueStorage is used if Storage argument is nil.
func New(threads int, s Storage) (*Queue, error) {
if s == nil {
s = &InMemoryQueueStorage{MaxSize: 100000}
}
if err := s.Init(); err != nil {
return nil, err
}
return &Queue{
Threads: threads,
storage: s,
running: true,
}, nil
}
// IsEmpty returns true if the queue is empty
func (q *Queue) IsEmpty() bool {
s, _ := q.Size()
return s == 0
}
// AddURL adds a new URL to the queue
func (q *Queue) AddURL(URL string) error {
u, err := url.Parse(URL)
if err != nil {
return err
}
r := &colly.Request{
URL: u,
Method: "GET",
}
d, err := r.Marshal()
if err != nil {
return err
}
return q.storage.AddRequest(d)
}
// AddRequest adds a new Request to the queue
func (q *Queue) AddRequest(r *colly.Request) error {
q.mut.Lock()
waken := q.wake != nil
q.mut.Unlock()
if !waken {
return q.storeRequest(r)
}
err := q.storeRequest(r)
if err != nil {
return err
}
q.wake <- struct{}{}
return nil
}
func (q *Queue) storeRequest(r *colly.Request) error {
d, err := r.Marshal()
if err != nil {
return err
}
return q.storage.AddRequest(d)
}
// Size returns the size of the queue
func (q *Queue) Size() (int, error) {
return q.storage.QueueSize()
}
// Run starts consumer threads and calls the Collector
// to perform requests. Run blocks while the queue has active requests
// The given Storage must not be used directly while Run blocks.
func (q *Queue) Run(c *colly.Collector) error {
q.mut.Lock()
if q.wake != nil && q.running == true {
q.mut.Unlock()
panic("cannot call duplicate Queue.Run")
}
q.wake = make(chan struct{})
q.running = true
q.mut.Unlock()
requestc := make(chan *colly.Request)
complete, errc := make(chan struct{}), make(chan error, 1)
for i := 0; i < q.Threads; i++ {
go independentRunner(requestc, complete)
}
go q.loop(c, requestc, complete, errc)
defer close(requestc)
return <-errc
}
// Stop will stop the running queue
func (q *Queue) Stop() {
q.mut.Lock()
q.running = false
q.mut.Unlock()
}
func (q *Queue) loop(c *colly.Collector, requestc chan<- *colly.Request, complete <-chan struct{}, errc chan<- error) {
var active int
for {
size, err := q.storage.QueueSize()
if err != nil {
errc <- err
break
}
if size == 0 && active == 0 || !q.running {
// Terminate when
// 1. No active requests
// 2. Emtpy queue
errc <- nil
break
}
sent := requestc
var req *colly.Request
if size > 0 {
req, err = q.loadRequest(c)
if err != nil {
// ignore an error returned by GetRequest() or
// UnmarshalRequest()
continue
}
} else {
sent = nil
}
Sent:
for {
select {
case sent <- req:
active++
break Sent
case <-q.wake:
if sent == nil {
break Sent
}
case <-complete:
active--
if sent == nil && active == 0 {
break Sent
}
}
}
}
}
func independentRunner(requestc <-chan *colly.Request, complete chan<- struct{}) {
for req := range requestc {
req.Do()
complete <- struct{}{}
}
}
func (q *Queue) loadRequest(c *colly.Collector) (*colly.Request, error) {
buf, err := q.storage.GetRequest()
if err != nil {
return nil, err
}
copied := make([]byte, len(buf))
copy(copied, buf)
return c.UnmarshalRequest(copied)
}
// Init implements Storage.Init() function
func (q *InMemoryQueueStorage) Init() error {
q.lock = &sync.RWMutex{}
return nil
}
// AddRequest implements Storage.AddRequest() function
func (q *InMemoryQueueStorage) AddRequest(r []byte) error {
q.lock.Lock()
defer q.lock.Unlock()
// Discard URLs if size limit exceeded
if q.MaxSize > 0 && q.size >= q.MaxSize {
return colly.ErrQueueFull
}
i := &inMemoryQueueItem{Request: r}
if q.first == nil {
q.first = i
} else {
q.last.Next = i
}
q.last = i
q.size++
return nil
}
// GetRequest implements Storage.GetRequest() function
func (q *InMemoryQueueStorage) GetRequest() ([]byte, error) {
q.lock.Lock()
defer q.lock.Unlock()
if q.size == 0 {
return nil, nil
}
r := q.first.Request
q.first = q.first.Next
q.size--
return r, nil
}
// QueueSize implements Storage.QueueSize() function
func (q *InMemoryQueueStorage) QueueSize() (int, error) {
q.lock.Lock()
defer q.lock.Unlock()
return q.size, nil
}