summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-26 18:14:00 -0400
committerdemo <demo@antix1>2026-05-26 18:14:00 -0400
commit35e2864220e493f8aa77b5808b0a26674f4f3403 (patch)
tree9e8516091c6e3ea32c92b2d4ea1e30f8979a17ae
parentbd95fa6b7b9862a014bfaf55e98b6849f6122806 (diff)
feat: add cancellation feature
-rw-r--r--classic.go22
1 files changed, 19 insertions, 3 deletions
diff --git a/classic.go b/classic.go
index 9adb5bf..ef138e2 100644
--- a/classic.go
+++ b/classic.go
@@ -1,9 +1,11 @@
package main
import (
+ "context"
"fmt"
"log"
"net/url"
+ "sync"
)
func classic(startURL url.URL, maxConcurrency, maxURLs int) {
@@ -19,6 +21,10 @@ func classic(startURL url.URL, maxConcurrency, maxURLs int) {
seen := make(map[url.URL]bool)
count := 1
+ ctx, cancel := context.WithCancel(context.Background())
+ var wg sync.WaitGroup
+
+loop:
for ; numPendingSends > 0; numPendingSends-- {
batch := <-worklist
for _, u := range batch {
@@ -27,14 +33,24 @@ func classic(startURL url.URL, maxConcurrency, maxURLs int) {
count++
seen[u] = true
+ if len(seen) == maxURLs {
+ break loop
+ }
numPendingSends++
- go func() {
- worklist <- getBatch(u)
- }()
+ wg.Go(func() {
+ select {
+ case <-ctx.Done():
+ return
+ case worklist <- getBatch(u):
+ }
+ })
}
}
}
+
+ cancel()
+ wg.Wait()
}
func getBatch(u url.URL) []url.URL {