-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
238 lines (217 loc) · 4.77 KB
/
pool.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
package youcrawl
import (
"fmt"
"github.com/rs/xid"
"sync"
)
type TaskPool interface {
AddURLs(urls ...string)
AddTasks(task ...*Task)
GetOneTask(e *Engine) <-chan *Task
GetUnRequestedTask() (target *Task)
OnTaskDone(task *Task)
GetDoneChan() chan struct{}
Close()
SetPrevent(isPrevent bool)
GetTotal() (int, error)
GetUnRequestCount() (int, error)
GetCompleteCount() (int, error)
}
type RequestPool struct {
Tasks []Task
Total int
CompleteCount int
NextTask *Task
GetTaskChan chan *Task
DoneChan chan struct{}
CompleteChan chan *Task
PreventStop bool
Store GlobalStore
sync.RWMutex
}
func (p *RequestPool) GetCompleteCount() (int, error) {
p.Lock()
defer p.Unlock()
count := 0
for _, task := range p.Tasks {
if task.Completed {
count += 1
}
}
return count, nil
}
func (p *RequestPool) GetUnRequestCount() (int, error) {
p.Lock()
defer p.Unlock()
count := 0
for _, task := range p.Tasks {
if !task.Requested {
count += 1
}
}
return count, nil
}
func (p *RequestPool) GetTotal() (int, error) {
p.Lock()
defer p.Unlock()
return p.Total, nil
}
func (p *RequestPool) SetPrevent(isPrevent bool) {
p.Lock()
defer p.Unlock()
p.PreventStop = isPrevent
if !isPrevent {
for idx := range p.Tasks {
if !p.Tasks[idx].Requested || !p.Tasks[idx].Completed {
// not done task exist, not kill
return
}
}
EngineLogger.Info("no more task to resume , go to done!")
p.DoneChan <- struct{}{}
}
}
type RequestPoolOption struct {
UseCookie bool
PreventStop bool
}
func NewRequestPool(option RequestPoolOption, store GlobalStore) *RequestPool {
pool := &RequestPool{
Tasks: []Task{},
DoneChan: make(chan struct{}),
Total: 0,
CompleteCount: 0,
Store: store,
PreventStop: option.PreventStop,
}
return pool
}
func NewTask(url string, item interface{}) Task {
return Task{
ID: xid.New().String(),
Url: url,
Context: Context{
Item: item,
},
}
}
// add task
func (p *RequestPool) AddTasks(tasks ...*Task) {
EngineLogger.Info(fmt.Sprintf("append new url with len = %d", len(tasks)))
p.Lock()
defer p.Unlock()
p.Total += len(tasks)
for _, addTask := range tasks {
item := addTask.Context.Item
if item == nil {
item = DefaultItem{
Store: map[string]interface{}{},
}
}
p.Tasks = append(p.Tasks, Task{
ID: addTask.ID,
Url: addTask.Url,
Context: Context{
Item: addTask.Context.Item,
},
})
}
// suspend task requirement exist,resume
// see also `RequestPool.GetOneTask` method
if p.GetTaskChan != nil {
resumeTask := p.GetUnRequestedTask()
if resumeTask != nil {
resumeTask = p.initTask(resumeTask)
resumeTask.Requested = true
p.GetTaskChan <- resumeTask
p.GetTaskChan = nil
}
}
}
// add task to task pool
func (p *RequestPool) AddURLs(urls ...string) {
EngineLogger.Info(fmt.Sprintf("append new url with len = %d", len(urls)))
p.Lock()
defer p.Unlock()
p.Total += len(urls)
for _, url := range urls {
p.Tasks = append(p.Tasks, Task{
ID: xid.New().String(),
Url: url,
Context: Context{
Item: DefaultItem{
Store: map[string]interface{}{},
},
},
})
}
// suspend task requirement exist,resume
// see also `RequestPool.GetOneTask` method
if p.GetTaskChan != nil {
resumeTask := p.GetUnRequestedTask()
if resumeTask != nil {
resumeTask = p.initTask(resumeTask)
resumeTask.Requested = true
p.GetTaskChan <- resumeTask
p.GetTaskChan = nil
}
}
}
func (p *RequestPool) initTask(task *Task) *Task {
task.Context.Pool = p
task.ID = xid.New().String()
task.Context.GlobalStore = p.Store
return task
}
func (p *RequestPool) GetOneTask(e *Engine) <-chan *Task {
taskChan := make(chan *Task)
go func(callbackChan chan *Task) {
p.Lock()
unRequestedTask := p.GetUnRequestedTask()
if unRequestedTask != nil {
unRequestedTask = p.initTask(unRequestedTask)
unRequestedTask.Requested = true
callbackChan <- unRequestedTask
}
waitChannel := make(chan *Task)
p.GetTaskChan = waitChannel
p.Unlock()
callbackChan <- <-waitChannel
}(taskChan)
return taskChan
}
// find unreauested task
func (p *RequestPool) GetUnRequestedTask() (target *Task) {
for idx := range p.Tasks {
iterTask := &p.Tasks[idx]
if !iterTask.Requested {
target = iterTask
return
}
}
return nil
}
// get task from pool task
func (p *RequestPool) OnTaskDone(task *Task) {
p.Lock()
defer p.Unlock()
// set true
for idx := range p.Tasks {
if task.ID == p.Tasks[idx].ID {
p.Tasks[idx].Completed = true
}
}
// no new request
if !p.PreventStop {
EngineLogger.Info("no more task to resume , go to done!")
p.DoneChan <- struct{}{}
}
}
func (p *RequestPool) GetDoneChan() chan struct{} {
return p.DoneChan
}
func (p *RequestPool) Close() {
p.Lock()
defer p.Unlock()
p.DoneChan <- struct{}{}
}