Last active
August 29, 2015 13:56
-
-
Save rhysd/8938506 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 Page struct { | |
url, body string | |
} | |
type Finished struct{} | |
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 SyncURLs struct { | |
mutex *sync.Mutex | |
urls map[string]bool | |
} | |
func (self *SyncURLs) contains(url string) bool { | |
_, ok := self.urls[url] | |
return ok | |
} | |
func (self *SyncURLs) add(url string) { | |
defer self.mutex.Unlock() | |
self.mutex.Lock() | |
self.urls[url] = true | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher, ch chan Page, quit chan Finished, knownUrls SyncURLs) { | |
// Notify I've completed | |
defer func() { quit <- Finished{} }() | |
// TODO: Don't fetch the same URL twice. | |
if depth <= 0 { | |
return | |
} | |
if knownUrls.contains(url) { | |
return | |
} | |
knownUrls.add(url) | |
// Fetch URL | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// Scatter goroutines | |
ch <- Page{url, body} | |
finished := make(chan Finished) | |
for _, u := range urls { | |
go Crawl(u, depth-1, fetcher, ch, finished, knownUrls) | |
} | |
// Wait for all children completing | |
// TODO: use channel or WaitGroup() | |
for i := 0; i < len(urls); i++ { | |
<-finished | |
} | |
return | |
} | |
func main() { | |
ch, finished := make(chan Page), make(chan Finished) | |
go Crawl("http://golang.org/", 4, fetcher, ch, finished, SyncURLs{new(sync.Mutex), map[string]bool{}}) | |
// var result Page | |
for { | |
select { | |
case result := <-ch: | |
fmt.Printf("found: %s %s\n", result.url, result.body) | |
case <-finished: | |
return | |
} | |
} | |
} | |
// 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{ | |
"http://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"http://golang.org/pkg/", | |
"http://golang.org/cmd/", | |
}, | |
}, | |
"http://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/cmd/", | |
"http://golang.org/pkg/fmt/", | |
"http://golang.org/pkg/os/", | |
}, | |
}, | |
"http://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
"http://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"http://golang.org/", | |
"http://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment