Skip to content

Commit 4dee897

Browse files
authored
refactor: use WaitGroup.Go to simplify code (#871)
Signed-off-by: tsinglua <tsinglua@outlook.com>
1 parent a2117cf commit 4dee897

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

cmd/report/report.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ func generateReport(ctx context.Context, ec *ethrpc.Client, report *BlockReport,
278278
// Start worker goroutines
279279
var wg sync.WaitGroup
280280
for range concurrency {
281-
wg.Add(1)
282-
go func() {
283-
defer wg.Done()
281+
wg.Go(func() {
284282
for req := range blockChan {
285283
// Check if worker context is canceled
286284
if workerCtx.Err() != nil {
@@ -329,7 +327,7 @@ func generateReport(ctx context.Context, ec *ethrpc.Client, report *BlockReport,
329327
close(retryChan) // No more retries needed
330328
}
331329
}
332-
}()
330+
})
333331
}
334332

335333
// Monitor goroutine to close blockChan when all work is done

loadtest/gasmanager/gas_vault_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ func TestGasVault_ConcurrentAccess(t *testing.T) {
189189

190190
// Spawn multiple goroutines trying to spend gas concurrently
191191
for i := 0; i < numGoroutines; i++ {
192-
wg.Add(1)
193-
go func() {
194-
defer wg.Done()
192+
wg.Go(func() {
195193
err := vault.SpendOrWaitAvailableBudget(ctx, spendAmount)
196194
if err != nil {
197195
errors <- err
198196
}
199-
}()
197+
})
200198
}
201199

202200
wg.Wait()
@@ -229,24 +227,20 @@ func TestGasVault_ConcurrentAddAndSpend(t *testing.T) {
229227

230228
// Adders
231229
for i := 0; i < numAdders; i++ {
232-
wg.Add(1)
233-
go func() {
234-
defer wg.Done()
230+
wg.Go(func() {
235231
vault.AddGas(addAmount)
236-
}()
232+
})
237233
}
238234

239235
// Spenders
240236
errors := make(chan error, numSpenders)
241237
for i := 0; i < numSpenders; i++ {
242-
wg.Add(1)
243-
go func() {
244-
defer wg.Done()
238+
wg.Go(func() {
245239
err := vault.SpendOrWaitAvailableBudget(ctx, spendAmount)
246240
if err != nil {
247241
errors <- err
248242
}
249-
}()
243+
})
250244
}
251245

252246
wg.Wait()
@@ -278,14 +272,12 @@ func TestGasVault_MultipleSpendersWaiting(t *testing.T) {
278272

279273
// Start multiple spenders that will all wait
280274
for i := 0; i < numSpenders; i++ {
281-
wg.Add(1)
282-
go func() {
283-
defer wg.Done()
275+
wg.Go(func() {
284276
err := vault.SpendOrWaitAvailableBudget(ctx, spendAmount)
285277
if err == nil {
286278
successCount <- 1
287279
}
288-
}()
280+
})
289281
}
290282

291283
// Give goroutines time to start waiting

loadtest/preconf.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,21 @@ func (pt *PreconfTracker) Track(txHash common.Hash) {
101101
var preconfStatus bool
102102
var preconfError error
103103
var preconfDuration time.Duration
104-
wg.Add(1)
105-
go func() {
106-
defer wg.Done()
104+
wg.Go(func() {
107105

108106
preconfStartTime := time.Now()
109107
defer func() {
110108
preconfDuration = time.Since(preconfStartTime)
111109
}()
112110

113111
preconfStatus, preconfError = util.WaitPreconf(context.Background(), pt.client, txHash, time.Minute)
114-
}()
112+
})
115113

116114
// wait for receipt
117115
var receipt *types.Receipt
118116
var receiptError error
119117
var receiptDuration time.Duration
120-
wg.Add(1)
121-
go func() {
122-
defer wg.Done()
118+
wg.Go(func() {
123119

124120
time.Sleep(100 * time.Millisecond)
125121

@@ -129,7 +125,7 @@ func (pt *PreconfTracker) Track(txHash common.Hash) {
129125
}()
130126

131127
receipt, receiptError = util.WaitReceiptWithTimeout(context.Background(), pt.client, txHash, time.Minute)
132-
}()
128+
})
133129

134130
wg.Wait()
135131

0 commit comments

Comments
 (0)