summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-26 21:24:42 -0400
committerdemo <demo@antix1>2026-05-26 21:26:18 -0400
commit00f30b53a746d45586c59773614f4c780880a788 (patch)
tree061bb5abeef4ad27c7942b1f5dc4bae52e7f9470
parent487a3877000e94c6ac85a198195ae36582dff3c1 (diff)
feat: add early termination condition based on maxURLs
-rw-r--r--workers.go11
1 files changed, 10 insertions, 1 deletions
diff --git a/workers.go b/workers.go
index fcfb504..ce1001e 100644
--- a/workers.go
+++ b/workers.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"fmt"
"net/url"
"sync"
@@ -24,13 +25,20 @@ func workers(startURL url.URL, maxConcurrency, maxURLs int) {
}()
var wg sync.WaitGroup
+ ctx, cancel := context.WithCancel(context.Background())
+
// Create maxConcurrency worker goroutines to demultiplex from
// the urls channel (unseen links.)
for range maxConcurrency {
wg.Go(func() {
for u := range urls {
batch := getBatch(u)
- go func() { worklist <- batch }()
+ select {
+ case <-ctx.Done():
+ return
+ default:
+ go func() { worklist <- batch }()
+ }
}
})
}
@@ -58,5 +66,6 @@ loop:
}
}
+ cancel()
wg.Wait()
}