diff options
| author | demo <demo@antix1> | 2026-05-26 18:14:00 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-26 18:14:00 -0400 |
| commit | 35e2864220e493f8aa77b5808b0a26674f4f3403 (patch) | |
| tree | 9e8516091c6e3ea32c92b2d4ea1e30f8979a17ae /classic.go | |
| parent | bd95fa6b7b9862a014bfaf55e98b6849f6122806 (diff) | |
feat: add cancellation feature
Diffstat (limited to 'classic.go')
| -rw-r--r-- | classic.go | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -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 { |
