diff options
| author | demo <demo@antix1> | 2026-05-26 21:59:10 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-26 21:59:10 -0400 |
| commit | d7beae73c248e5601e0fce78dca0b65cb6abd066 (patch) | |
| tree | b972e69dc3cadd68505c3adac5de39f37b3ea21c /workers.go | |
| parent | 00f30b53a746d45586c59773614f4c780880a788 (diff) | |
fix: make sure all workers terminate by the end
Diffstat (limited to 'workers.go')
| -rw-r--r-- | workers.go | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -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() } |
