Created
August 5, 2018 08:13
-
-
Save stek29/792e4c53d119402dd4a54d05be7f5d2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
type Fetcher interface { | |
// Fetch returns the body of URL and | |
// a slice of URLs found on that page. | |
Fetch(url string) (body string, urls []string, err error) | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
var crawl func(url string, depth int) | |
var wg sync.WaitGroup | |
type cachedResult struct { | |
body string | |
urls []string | |
} | |
var crawlerCache = struct { | |
fetched_urls map[string]cachedResult | |
mux sync.RWMutex | |
}{fetched_urls: make(map[string]cachedResult)} | |
crawl = func(url string, depth int) { | |
defer wg.Done() | |
if depth <= 0 { | |
return | |
} | |
crawlerCache.mux.RLock() | |
hot, ok := crawlerCache.fetched_urls[url] | |
crawlerCache.mux.RUnlock() | |
var urls []string | |
if ok { | |
body, turls := hot.body, hot.urls | |
urls = turls | |
fmt.Printf("cached: %s %q\n", url, body) | |
} else { | |
body, turls, err := fetcher.Fetch(url) | |
urls = turls | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("fetched: %s %q\n", url, body) | |
crawlerCache.mux.Lock() | |
crawlerCache.fetched_urls[url] = cachedResult{body, urls} | |
crawlerCache.mux.Unlock() | |
} | |
wg.Add(len(urls)) | |
for _, u := range urls { | |
go crawl(u, depth-1) | |
} | |
} | |
wg.Add(1) | |
go crawl(url, depth) | |
wg.Wait() | |
return | |
} | |
func main() { | |
Crawl("https://golang.org/", 4, fetcher) | |
} | |
// fakeFetcher is Fetcher that returns canned results. | |
type fakeFetcher map[string]*fakeResult | |
type fakeResult struct { | |
body string | |
urls []string | |
} | |
func (f fakeFetcher) Fetch(url string) (string, []string, error) { | |
if res, ok := f[url]; ok { | |
return res.body, res.urls, nil | |
} | |
return "", nil, fmt.Errorf("not found: %s", url) | |
} | |
// fetcher is a populated fakeFetcher. | |
var fetcher = fakeFetcher{ | |
"https://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"https://golang.org/pkg/", | |
"https://golang.org/cmd/", | |
}, | |
}, | |
"https://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/cmd/", | |
"https://golang.org/pkg/fmt/", | |
"https://golang.org/pkg/os/", | |
}, | |
}, | |
"https://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
"https://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment