Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
p-duke opened this issue Aug 18, 2024 · 1 comment
Open

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

p-duke opened this issue Aug 18, 2024 · 1 comment

Comments

@p-duke
Copy link

p-duke commented Aug 18, 2024

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!

@loong
Copy link
Owner

loong commented Sep 11, 2024

@p-duke Hi! Have you figured it out by now?

The issue is that the throttle ticker gets initialized each time Crawl() is called. Meaning you are creating a new ticker every time!

You can initialize one ticker and pass it down to Crawl() as a parameter. Here is an example:
https://github.com/loong/go-concurrency-exercises/pull/19/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants