Skip to content

Commit 329dfe3

Browse files
committed
go/worker/storage/statesync: Remove redundant waitgroup
In case of termination due to error exiting main for loop or canceled context there is no point in waiting for go routines to finish fetching/doing the cleanup. As long we cancel the context for them and use it properly in the select statements this should be safe and better.
1 parent ad6d0fa commit 329dfe3

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

go/worker/storage/statesync/diff_sync.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (w *Worker) syncDiffs(
181181
return fmt.Errorf("failed to fetch missing block headers: %w", err) // Suggestion: databases can fail, consider retrying.
182182
}
183183

184-
w.triggerRoundFetches(fetchCtx, &wg, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
184+
w.triggerRoundFetches(fetchCtx, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
185185
case item := <-diffCh:
186186
if item.err != nil {
187187
w.logger.Error("error calling getdiff",
@@ -199,12 +199,12 @@ func (w *Worker) syncDiffs(
199199
// Item was successfully processed, trigger more round fetches.
200200
// This ensures that new rounds are processed as fast as possible
201201
// when we're syncing and are far behind.
202-
w.triggerRoundFetches(fetchCtx, &wg, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
202+
w.triggerRoundFetches(fetchCtx, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
203203
heartbeat.reset()
204204
case <-heartbeat.C:
205205
if latestBlockRound != w.undefinedRound {
206206
w.logger.Debug("heartbeat", "in_flight_rounds", len(syncingRounds))
207-
w.triggerRoundFetches(fetchCtx, &wg, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
207+
w.triggerRoundFetches(fetchCtx, fetchPool, diffCh, syncingRounds, summaryCache, lastFullyAppliedRound+1, latestBlockRound)
208208
}
209209
case finalized := <-finalizedCh:
210210
var err error
@@ -273,7 +273,6 @@ func (w *Worker) fetchMissingBlockHeaders(ctx context.Context, lastFullyAppliedR
273273

274274
func (w *Worker) triggerRoundFetches(
275275
ctx context.Context,
276-
wg *sync.WaitGroup,
277276
fetchPool *workerpool.Pool,
278277
diffCh chan<- *fetchedDiff,
279278
syncingRounds map[uint64]*inFlight,
@@ -329,9 +328,7 @@ func (w *Worker) triggerRoundFetches(
329328
rootType := prevRoots[i].Type
330329
if !syncing.outstanding.contains(rootType) && syncing.awaitingRetry.contains(rootType) {
331330
syncing.scheduleDiff(rootType)
332-
wg.Add(1)
333331
fetchPool.Submit(func() {
334-
defer wg.Done()
335332
w.fetchDiff(ctx, diffCh, this.Round, prevRoots[i], this.Roots[i])
336333
})
337334
}

0 commit comments

Comments
 (0)