Упражнение из: https://tour.golang.org/concurrency/10
Описание:
В этом упражнении вы будете использовать функции параллелизма Go для распараллеливания веб-сканера.
ИзменитьФункция сканирования для параллельного извлечения URL-адресов без повторного извлечения одного и того же URL-адреса.
Подсказка: вы можете хранить кэш URL-адресов, извлеченных на карте, но только карты не безопасны для одновременного использования!
Вот мой ответ:
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)
}
var crawledURLs = make(map[string]bool)
var mux sync.Mutex
func CrawlURL(url string, depth int, fetcher Fetcher, quit chan bool) {
defer func() { quit <- true }()
if depth <= 0 {
return
}
mux.Lock()
_, isCrawled := crawledURLs[url]
if isCrawled {
return
}
crawledURLs[url] = true
mux.Unlock()
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("found: %s %q\n", url, body)
quitThis := make(chan bool)
for _, u := range urls {
go CrawlURL(u, depth-1, fetcher, quitThis)
}
for range urls {
<-quitThis
}
return
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher) {
CrawlURL(url, depth, fetcher, make(chan bool))
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/",
},
},
}
И вывод:
found: https://golang.org/ "The Go Programming Language"
not found: https://golang.org/cmd/
found: https://golang.org/pkg/ "Packages"
found: https://golang.org/pkg/os/ "Package os"
fatal error: all goroutines are asleep - deadlock!
Мне было интересно, почему произойдет тупик?Это потому, что я неправильно использую каналы?
Заметив, что я забыл освободить мьютекс в ветке if isCrawled {}
, поэтому я отредактировал свой код следующим образом:
...
if isCrawled {
mux.Unlock() // added this line
return
}
...
Но тупик по-прежнему существует, и вывод другой:
found: https://golang.org/ "The Go Programming Language"
not found: https://golang.org/cmd/
found: https://golang.org/pkg/ "Packages"
found: https://golang.org/pkg/os/ "Package os"
found: https://golang.org/pkg/fmt/ "Package fmt"
fatal error: all goroutines are asleep - deadlock!