From 60cc56edc55290cd0999bd859a1dfa2c8dbc96d5 Mon Sep 17 00:00:00 2001 From: demo Date: Sun, 31 May 2026 00:08:43 -0400 Subject: feat: use for-select instead of range --- workers.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'workers.go') diff --git a/workers.go b/workers.go index 02d7d7d..db22986 100644 --- a/workers.go +++ b/workers.go @@ -33,23 +33,23 @@ func workers(startURL url.URL, maxConcurrency, maxURLs, maxDepth int) { // the urls channel (unseen links.) for i := range maxConcurrency { wg.Go(func() { - for p := range packets { - batch := getBatch(p.url) - - // Convert URLs to Packets. In the - // process, bump up the depth by 1. - ps := convertToPackets(batch, p.depth+1) - + for { select { case <-ctx.Done(): fmt.Printf("exiting early %d\n", i+1) - return - default: + case p, ok := <-packets: + if !ok { + fmt.Printf("exiting because packets is closed %d\n", i+1) + } + + batch := getBatch(p.url) + + // Convert URLs to Packets. In the + // process, bump up the depth by 1. + ps := convertToPackets(batch, p.depth+1) go func() { worklist <- ps }() } } - - fmt.Printf("terminating %d\n", i+1) }) } -- cgit v1.2.3