mirror of
https://github.com/blindlobstar/go-interview-problems
synced 2025-04-22 01:35:15 +00:00
web crawler problem
This commit is contained in:
parent
98820fde18
commit
fa363a1343
13
03-web-crawler/README.md
Normal file
13
03-web-crawler/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
## Web Crawler
|
||||
|
||||
In this exercise you'll use Go's concurrency features to parallelize a web crawler.
|
||||
|
||||
Modify the `Crawl` function to fetch URLs in parallel without fetching the same URL twice.
|
||||
|
||||
~Hint:~ you can keep a cache of the URLs that have been fetched on a map, but maps alone are not safe for concurrent use!
|
||||
|
||||
## Tags
|
||||
`Concurrency`
|
||||
|
||||
## Source
|
||||
- [A Tour of Go](https://go.dev/tour/concurrency/10)
|
109
03-web-crawler/solution/solution.go
Normal file
109
03-web-crawler/solution/solution.go
Normal file
@ -0,0 +1,109 @@
|
||||
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 (
|
||||
m = map[string]bool{}
|
||||
mu sync.RWMutex
|
||||
)
|
||||
|
||||
// Crawl uses fetcher to recursively crawl
|
||||
// pages starting with url, to a maximum of depth.
|
||||
func Crawl(url string, depth int, fetcher Fetcher) {
|
||||
// TODO: Fetch URLs in parallel.
|
||||
// TODO: Don't fetch the same URL twice.
|
||||
// This implementation doesn't do either:
|
||||
if depth <= 0 {
|
||||
return
|
||||
}
|
||||
mu.RLock()
|
||||
if m[url] {
|
||||
mu.RUnlock()
|
||||
return
|
||||
}
|
||||
mu.RUnlock()
|
||||
|
||||
mu.Lock()
|
||||
m[url] = true
|
||||
mu.Unlock()
|
||||
|
||||
body, urls, err := fetcher.Fetch(url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("found: %s %q\n", url, body)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, u := range urls {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
Crawl(u, depth-1, fetcher)
|
||||
}()
|
||||
}
|
||||
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/",
|
||||
},
|
||||
},
|
||||
}
|
85
03-web-crawler/task.go
Normal file
85
03-web-crawler/task.go
Normal file
@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
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) {
|
||||
// TODO: Fetch URLs in parallel.
|
||||
// TODO: Don't fetch the same URL twice.
|
||||
// This implementation doesn't do either:
|
||||
if depth <= 0 {
|
||||
return
|
||||
}
|
||||
body, urls, err := fetcher.Fetch(url)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("found: %s %q\n", url, body)
|
||||
for _, u := range urls {
|
||||
Crawl(u, depth-1, fetcher)
|
||||
}
|
||||
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/",
|
||||
},
|
||||
},
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user