diff options
| author | demo <demo@antix1> | 2026-05-26 21:24:42 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-26 21:26:18 -0400 |
| commit | 00f30b53a746d45586c59773614f4c780880a788 (patch) | |
| tree | 061bb5abeef4ad27c7942b1f5dc4bae52e7f9470 /workers.go | |
| parent | 487a3877000e94c6ac85a198195ae36582dff3c1 (diff) | |
feat: add early termination condition based on maxURLs
Diffstat (limited to 'workers.go')
| -rw-r--r-- | workers.go | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -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() } |
