-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkqueue.go
More file actions
52 lines (45 loc) · 853 Bytes
/
workqueue.go
File metadata and controls
52 lines (45 loc) · 853 Bytes
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
package wikicrawl
import (
"sync"
"time"
)
type WorkQueue struct {
crawler Crawler
wait sync.WaitGroup
todo chan Link
Result *CrawlResult
}
func (wq *WorkQueue) AddWork(href Link) {
wq.wait.Add(1)
for {
select {
case wq.todo <- href:
return
case <-time.After(5 * time.Second):
panic("Queue full")
}
}
}
func (wq *WorkQueue) Start(pool int) {
for i := 0; i < pool; i++ {
go func() {
for work := range wq.todo {
func() {
defer wq.wait.Done()
wq.crawler.FollowLink(work, wq)
}()
}
}()
}
}
func (wq *WorkQueue) Wait() {
wq.wait.Wait()
close(wq.todo)
}
func NewWorkQueue(crawler Crawler, limit int) *WorkQueue {
queue := new(WorkQueue)
queue.crawler = crawler
queue.todo = make(chan Link, limit)
queue.Result = &CrawlResult{Visited: NewLinkSet(), Broken: NewLinkSet()}
return queue
}