Skip to content

Exercise 1: Tests fail even with a 5 second delay #42

Open
@p-duke

Description

@p-duke

Hey @loong - love this repo and its helping me improve my Go skills

After adding what I think is the desired solution the tests continue to fail. Even when I include a 5 second delay.

func Crawl(url string, depth int, wg *sync.WaitGroup) {
+	rateLimit := time.Second * 5
+	throttle := time.NewTicker(rateLimit)
+	defer throttle.Stop()
	defer wg.Done()

	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)

	wg.Add(len(urls))
	for _, u := range urls {
		// Do not remove the `go` keyword, as Crawl() must be
		// called concurrently
+		<-throttle.C // rate limit client calls
		go Crawl(u, depth-1, wg)
	}
	return
}
Screenshot 2024-08-18 at 11 21 24 AM

Go version go version go1.21.0 darwin/amd64

Please let me know if I'm missing something here. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions