Created
October 11, 2011 14:00
-
-
Save dlintw/1278147 to your computer and use it in GitHub Desktop.
go tour #69
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
func Crawl(url string, depth, workerCnt int, fetcher Fetcher) { | |
reqch := make(chan int) | |
seen := make(map[string]bool) | |
working := make(chan bool, workerCnt) | |
var crawl func(string, int) | |
crawl = func(url string, depth int) { | |
defer func(){reqch <- -1}() | |
if depth <= 0 { | |
return | |
} | |
if _,ok := seen[url] ; ok { | |
return | |
} | |
seen[url] = true | |
working <- true | |
defer func(){ <-working}() | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
reqch <- len(urls) | |
for _, u := range urls { | |
go crawl(u, depth-1) | |
} | |
} | |
go crawl(url, depth) | |
actsum := 1 | |
for diff :=range reqch { | |
actsum += diff | |
if actsum == 0 { | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment