-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.go
62 lines (50 loc) · 1.26 KB
/
crawl.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
package main
import (
"fmt"
"runtime"
)
func isAlreadyFetched(url string, sm stateManager) bool {
sm.rwMutex.RLock()
defer sm.rwMutex.RUnlock()
var _, ok = sm.fetched[url]
return ok
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, sm stateManager) {
sm.waitGroup.Add(1)
go func() {
// Tell the WaitGroup we're finished whenever we leave this scope
defer sm.waitGroup.Done()
// Don't process further than the specified depth
if depth <= 0 {
return
}
// Safely return if we've already fetched the given URL
if isAlreadyFetched(url, sm) {
fmt.Printf("Skipping url %s (already fetched)\n", url)
return
}
// Handle failure to fetch
fmt.Printf("Fetching url %s at depth %v\n", url, depth)
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
} else {
fmt.Printf(" found body %s\n", body)
}
// Add the fetched URL to SafeFetcher
sm.rwMutex.Lock()
sm.fetched[url] = true
sm.rwMutex.Unlock()
// Continue crawling
for _, u := range urls {
fmt.Printf(" dispatching url %s\n", u)
Crawl(u, depth-1, fetcher, sm)
// Yield so other threads can make progress
runtime.Gosched()
}
}()
return
}