-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
189 lines (151 loc) · 4.09 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
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
package crawl
import (
"github.com/jjmschofield/gocrawl/internal/caches"
"github.com/jjmschofield/gocrawl/internal/counters"
"github.com/jjmschofield/gocrawl/internal/pages"
"github.com/jjmschofield/gocrawl/internal/scrape"
"github.com/jjmschofield/gocrawl/internal/writers"
"log"
"net/url"
"sync"
)
//go:generate counterfeiter . Crawler
type Crawler interface {
Crawl(startUrl url.URL) Counters
}
type PageCrawler struct {
Config Config
caches Caches
channels WorkerChannels
counters Counters
scraper scrape.Scraper
worker QueueWorker
writer writers.Writer
}
type Config struct {
Caches Caches
WorkerCount int
Scraper scrape.Scraper
Worker QueueWorker
Writer writers.Writer
}
type Caches struct {
Processing caches.ThreadSafeCache
Crawled caches.ThreadSafeCache
}
type Counters struct {
Discovered counters.AtomicInt64 // Pages discovered so far
Processing counters.AtomicInt64 // Pages that we need to complete processing
Crawling counters.AtomicInt64 // Pages that we are currently crawling
CrawlComplete counters.AtomicInt64 // Pages that we have CrawledId
CrawlsQueued counters.AtomicInt64 // Pages currently queued for crawling
}
func (c *PageCrawler) Crawl(startUrl url.URL) Counters {
var wg sync.WaitGroup
go c.writer.Start(c.channels.Write)
go c.resultReceiver()
c.startWorkers(c.Config.WorkerCount, &wg)
c.enqueueUrl(startUrl)
wg.Wait()
return c.counters
}
func (c *PageCrawler) startWorkers(workerCount int, wg *sync.WaitGroup) {
wg.Add(workerCount)
for i := 0; i < c.Config.WorkerCount; i++ {
go c.worker.Start(c.channels, &c.counters.CrawlsQueued, &c.counters.Crawling, wg)
}
}
func (c *PageCrawler) enqueueJob(job WorkerJob) {
if !c.caches.Crawled.Has(job.Id) && !c.caches.Processing.Has(job.Id) {
c.counters.Processing.Add(1)
c.counters.Discovered.Add(1)
c.caches.Processing.Add(job.Id)
go func() {
c.counters.CrawlsQueued.Add(1)
c.channels.In <- job
}()
}
}
func (c *PageCrawler) resultReceiver() {
for result := range c.channels.Out {
c.caches.Crawled.Add(result.CrawledId)
c.counters.CrawlComplete.Add(1)
c.enqueuePageGroup(result.Result.OutPages)
c.counters.Processing.Sub(1)
c.caches.Processing.Remove(result.CrawledId)
log.Printf(
"Crawled %s Discovered: %v, Processing: %v, In Scrape Queue: %v, Scraping: %v, Scrape Complete: %v",
result.CrawledId,
c.counters.Discovered.Count(),
c.counters.Processing.Count(),
c.counters.CrawlsQueued.Count(),
c.counters.Crawling.Count(),
c.counters.CrawlComplete.Count())
if !c.hasWorkRemaining() {
c.closeChannels()
}
}
}
func (c *PageCrawler) enqueueUrl(srcUrl url.URL) {
pageId, normalizedUrl := pages.CalcPageId(srcUrl)
job := WorkerJob{
Id: pageId,
URL: normalizedUrl,
}
c.enqueueJob(job)
}
func (c *PageCrawler) enqueuePageGroup(pageGroup pages.PageGroup) {
for pageId, href := range pageGroup.Internal {
pageUrl, _ := url.Parse(href)
job := WorkerJob{
Id: pageId,
URL: *pageUrl,
}
c.enqueueJob(job)
}
}
func (c *PageCrawler) hasWorkRemaining() bool {
if c.counters.Processing.Count() > 0 {
return true
}
if c.counters.CrawlsQueued.Count() > 0 {
return true
}
if c.counters.Crawling.Count() > 0 {
return true
}
return false
}
func (c *PageCrawler) closeChannels() {
close(c.channels.In)
close(c.channels.Out)
close(c.channels.Write)
}
func NewPageCrawler(config Config) PageCrawler {
return PageCrawler{
Config: config,
caches: config.Caches,
channels: WorkerChannels{
In: make(chan WorkerJob),
Out: make(chan WorkerResult),
Write: make(chan pages.Page),
},
worker: config.Worker,
writer: config.Writer,
}
}
func NewDefaultPageCrawler(workerCount int, filePath string) PageCrawler {
crawledCache := caches.NewStrThreadSafe()
processingCache := caches.NewStrThreadSafe()
config := Config{
Caches: Caches{
Crawled: &crawledCache,
Processing: &processingCache,
},
Worker: &Worker{Scraper: scrape.PageScraper{}},
WorkerCount: workerCount,
Writer: &writers.FileWriter{FilePath: filePath},
}
crawler := NewPageCrawler(config)
return crawler
}