From d7beae73c248e5601e0fce78dca0b65cb6abd066 Mon Sep 17 00:00:00 2001 From: demo Date: Tue, 26 May 2026 21:59:10 -0400 Subject: fix: make sure all workers terminate by the end --- workers.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'workers.go') diff --git a/workers.go b/workers.go index ce1001e..01c6888 100644 --- a/workers.go +++ b/workers.go @@ -29,17 +29,26 @@ func workers(startURL url.URL, maxConcurrency, maxURLs int) { // Create maxConcurrency worker goroutines to demultiplex from // the urls channel (unseen links.) - for range maxConcurrency { + for i := range maxConcurrency { wg.Go(func() { + loop: for u := range urls { batch := getBatch(u) select { case <-ctx.Done(): - return + break loop default: - go func() { worklist <- batch }() + go func() { + select { + case <-ctx.Done(): + return + case worklist <- batch: + } + }() } } + + fmt.Printf("terminating %d\n", i+1) }) } @@ -66,6 +75,8 @@ loop: } } + close(urls) + cancel() wg.Wait() } -- cgit v1.2.3