Last active
November 8, 2019 20:35
-
-
Save PartyLich/471328e45bdffc928a9a618fed86af35 to your computer and use it in GitHub Desktop.
Tour of Go -> Exercise: Web Crawler
This file contains hidden or 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) | |
} | |
type MutMap struct { | |
cache map[string]bool | |
mut sync.Mutex | |
} | |
func (m MutMap) Get(key string) bool { | |
m.mut.Lock() | |
defer m.mut.Unlock() | |
_, ok := m.cache[key] | |
return ok | |
} | |
func (m MutMap) Set(key string, val bool) { | |
m.mut.Lock() | |
defer m.mut.Unlock() | |
m.cache[key] = val | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func CrawlParallel(url string, depth int, fetcher Fetcher, cache MutMap, quit chan bool) { | |
// check cache for url | |
if depth <= 0 || cache.Get(url) { | |
quit <- true | |
return | |
} | |
// url not found | |
cache.Set(url, true) | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
quit <- true | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
// launch goroutine for each url | |
c := make(chan bool) | |
for _, u := range urls { | |
go CrawlParallel(u, depth-1, fetcher, cache, c) | |
} | |
// drain response channel | |
for range urls { | |
<-c | |
} | |
quit <- true | |
} | |
func main() { | |
quit := make(chan bool) | |
go CrawlParallel("https://golang.org/", 4, fetcher, MutMap{cache: make(map[string]bool)}, quit) | |
<-quit | |
} | |
// 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