refactor: use WaitGroup.Go to simplify code - #2828
Conversation
Greptile SummaryThis PR adopts Confidence Score: 5/5Safe to merge — purely mechanical refactor with no logic changes and confirmed API availability in Go 1.25+. All replacements are semantically identical to the original code; the Go version constraint (1.26) guarantees WaitGroup.Go availability; no new logic is introduced. No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant WaitGroup
participant Goroutine
Note over Caller,Goroutine: Old pattern
Caller->>WaitGroup: Add(1)
Caller->>Goroutine: launch goroutine with defer Done
Goroutine-->>WaitGroup: Done deferred on return
Caller->>WaitGroup: Wait
Note over Caller,Goroutine: New pattern via WaitGroup.Go
Caller->>WaitGroup: Go(f)
WaitGroup->>WaitGroup: Add(1) internally
WaitGroup->>Goroutine: launch goroutine
Goroutine-->>WaitGroup: Done internally on return
Caller->>WaitGroup: Wait
Reviews (1): Last reviewed commit: "refactor: use WaitGroup.Go to simplify c..." | Re-trigger Greptile |
nkryuchkov
left a comment
There was a problem hiding this comment.
Thank you for the contribution
iurii-ssv
left a comment
There was a problem hiding this comment.
Good stuff, @purelualight can you also fix the failed linter pipeline ?
| wg.Go(func() { | ||
| // check number of peers and messages | ||
| for i := 0; i < nValidators; i++ { | ||
| wg.Add(1) |
There was a problem hiding this comment.
What do you think about using a separate innerWg for these goroutines? Seems that calling wg.Add(1) from inside a closure that's itself counted by the same wg is fragile — it works today because the for-loop always runs to completion before the outer goroutine returns, but a future early-return (e.g. on ctx cancel) could let the trailing wg.Wait() unblock before all inner Add calls land, racing any subsequent reuse of the same wg.
3ea988e
@iurii-ssv @nkryuchkov Thank you for your review. I’ve resolved the conflicts and fixed the lint issues (I removed the extra blank lines). Please review it again. |
Signed-off-by: purelualight <purelualight@outlook.com>
|
@purelualight the CI linter seems to have failed again, can you run |
|
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 30 days if there are no updates. Please comment if you would like to keep it open. |
Adopt sync.WaitGroup.Go (Go 1.25) to simplify tracked goroutine spawning.
This replaces the error-prone trio of wg.Add(1), go func(), and defer wg.Done() with a single, self-contained call.
More info: golang/go#63796