Skip to content

Commit ec08863

Browse files
authored
all: use WaigGroup.Go() to simplify code (XinFinOrg#1699)
1 parent e6b1fe9 commit ec08863

32 files changed

+100
-210
lines changed

accounts/keystore/keystore_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,12 @@ func TestImportRace(t *testing.T) {
406406
_, ks2 := tmpKeyStore(t)
407407
var atom atomic.Uint32
408408
var wg sync.WaitGroup
409-
wg.Add(2)
410409
for i := 0; i < 2; i++ {
411-
go func() {
412-
defer wg.Done()
410+
wg.Go(func() {
413411
if _, err := ks2.Import(json, "new", "new"); err != nil {
414412
atom.Add(1)
415413
}
416-
}()
414+
})
417415
}
418416
wg.Wait()
419417
if atom.Load() != 1 {

bmt/bmt_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,21 @@ func TestHasherConcurrency(t *testing.T) {
263263
defer pool.Drain(0)
264264
wg := sync.WaitGroup{}
265265
cycles := 100
266-
wg.Add(maxproccnt * cycles)
267266
errc := make(chan error)
268267

269268
for p := 0; p < maxproccnt; p++ {
270269
for i := 0; i < cycles; i++ {
271-
go func() {
270+
wg.Go(func() {
272271
bmt := New(pool)
273272
n := rand.Intn(4096)
274273
tdata := testDataReader(n)
275274
data := make([]byte, n)
276275
tdata.Read(data)
277276
err := testHasherCorrectness(bmt, hasher, data, n, 128)
278-
wg.Done()
279277
if err != nil {
280278
errc <- err
281279
}
282-
}()
280+
})
283281
}
284282
}
285283
go func() {
@@ -386,18 +384,16 @@ func benchmarkBMTBaseline(n int, t *testing.B) {
386384
for i := 0; i < t.N; i++ {
387385
count := int32((n-1)/hasher().Size() + 1)
388386
wg := sync.WaitGroup{}
389-
wg.Add(maxproccnt)
390387
var i int32
391388
for j := 0; j < maxproccnt; j++ {
392-
go func() {
393-
defer wg.Done()
389+
wg.Go(func() {
394390
h := hasher()
395391
for atomic.AddInt32(&i, 1) < count {
396392
h.Reset()
397393
h.Write(data)
398394
h.Sum(nil)
399395
}
400-
}()
396+
})
401397
}
402398
wg.Wait()
403399
}
@@ -437,15 +433,13 @@ func benchmarkHasherReuse(poolsize, n int, t *testing.B) {
437433
t.ResetTimer()
438434
for i := 0; i < t.N; i++ {
439435
wg := sync.WaitGroup{}
440-
wg.Add(cycles)
441436
for j := 0; j < cycles; j++ {
442-
bmt := New(pool)
443-
go func() {
444-
defer wg.Done()
437+
wg.Go(func() {
438+
bmt := New(pool)
445439
bmt.Reset()
446440
bmt.Write(data)
447441
bmt.Sum(nil)
448-
}()
442+
})
449443
}
450444
wg.Wait()
451445
}

common/prque/lazyqueue_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ func TestLazyQueue(t *testing.T) {
7979
stopCh = make(chan chan struct{})
8080
)
8181
defer wg.Wait()
82-
wg.Add(1)
83-
go func() {
84-
defer wg.Done()
82+
wg.Go(func() {
8583
for {
8684
select {
8785
case <-clock.After(testQueueRefresh):
@@ -92,7 +90,7 @@ func TestLazyQueue(t *testing.T) {
9290
return
9391
}
9492
}
95-
}()
93+
})
9694

9795
for c := 0; c < testSteps; c++ {
9896
i := rand.Intn(testItems)

consensus/XDPoS/engines/engine_v2/vote.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ func (x *XDPoS_v2) verifyVotes(chain consensus.ChainReader, votes map[common.Has
164164
emptySigner := common.Address{}
165165
// Filter out non-Master nodes signatures
166166
var wg sync.WaitGroup
167-
wg.Add(len(votes))
168-
for h, vote := range votes {
169-
go func(hash common.Hash, v *types.Vote) {
170-
defer wg.Done()
167+
for _, vote := range votes {
168+
wg.Go(func() {
169+
v := vote.(*types.Vote)
171170
signerAddress := v.GetSigner()
172171
if signerAddress != emptySigner {
173172
// verify that signer belongs to the final masternodes, we have not do so in previous steps
@@ -199,7 +198,7 @@ func (x *XDPoS_v2) verifyVotes(chain consensus.ChainReader, votes map[common.Has
199198
return
200199
}
201200
v.SetSigner(masterNode)
202-
}(h, vote.(*types.Vote))
201+
})
203202
}
204203
wg.Wait()
205204
elapsed := time.Since(start)

console/console.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ func New(config Config) (*Console, error) {
119119
return nil, err
120120
}
121121

122-
console.wg.Add(1)
123-
go console.interruptHandler()
122+
console.wg.Go(console.interruptHandler)
124123

125124
return console, nil
126125
}
@@ -341,8 +340,6 @@ func (c *Console) Evaluate(statement string) {
341340
// interruptHandler runs in its own goroutine and waits for signals.
342341
// When a signal is received, it interrupts the JS interpreter.
343342
func (c *Console) interruptHandler() {
344-
defer c.wg.Done()
345-
346343
// During Interactive, liner inhibits the signal while it is prompting for
347344
// input. However, the signal will be received while evaluating JS.
348345
//

core/blockchain.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
334334
}
335335

336336
// Start future block processor.
337-
bc.wg.Add(1)
338-
go bc.futureBlocksLoop()
337+
bc.wg.Go(bc.futureBlocksLoop)
339338

340339
return bc, nil
341340
}
@@ -2580,8 +2579,6 @@ func (bc *BlockChain) PostChainEvents(events []interface{}, logs []*types.Log) {
25802579

25812580
// futureBlocksLoop processes the 'future block' queue.
25822581
func (bc *BlockChain) futureBlocksLoop() {
2583-
defer bc.wg.Done()
2584-
25852582
futureTimer := time.NewTicker(100 * time.Millisecond)
25862583
defer futureTimer.Stop()
25872584

core/bloombits/scheduler.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ func (s *scheduler) run(sections chan uint64, dist chan *request, done chan []by
6262
pend := make(chan uint64, cap(dist))
6363

6464
// Start the pipeline schedulers to forward between user -> distributor -> user
65-
wg.Add(2)
66-
go s.scheduleRequests(sections, dist, pend, quit, wg)
67-
go s.scheduleDeliveries(pend, done, quit, wg)
65+
wg.Go(func() { s.scheduleRequests(sections, dist, pend, quit) })
66+
wg.Go(func() { s.scheduleDeliveries(pend, done, quit) })
6867
}
6968

7069
// reset cleans up any leftovers from previous runs. This is required before a
@@ -84,9 +83,8 @@ func (s *scheduler) reset() {
8483
// scheduleRequests reads section retrieval requests from the input channel,
8584
// deduplicates the stream and pushes unique retrieval tasks into the distribution
8685
// channel for a database or network layer to honour.
87-
func (s *scheduler) scheduleRequests(reqs chan uint64, dist chan *request, pend chan uint64, quit chan struct{}, wg *sync.WaitGroup) {
86+
func (s *scheduler) scheduleRequests(reqs chan uint64, dist chan *request, pend chan uint64, quit chan struct{}) {
8887
// Clean up the goroutine and pipeline when done
89-
defer wg.Done()
9088
defer close(pend)
9189

9290
// Keep reading and scheduling section requests
@@ -131,9 +129,8 @@ func (s *scheduler) scheduleRequests(reqs chan uint64, dist chan *request, pend
131129

132130
// scheduleDeliveries reads section acceptance notifications and waits for them
133131
// to be delivered, pushing them into the output data buffer.
134-
func (s *scheduler) scheduleDeliveries(pend chan uint64, done chan []byte, quit chan struct{}, wg *sync.WaitGroup) {
132+
func (s *scheduler) scheduleDeliveries(pend chan uint64, done chan []byte, quit chan struct{}) {
135133
// Clean up the goroutine and pipeline when done
136-
defer wg.Done()
137134
defer close(done)
138135

139136
// Keep reading notifications and scheduling deliveries

core/state/statedb_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,10 @@ func TestCopy(t *testing.T) {
182182
}
183183

184184
// Finalise the changes on all concurrently
185-
finalise := func(wg *sync.WaitGroup, db *StateDB) {
186-
defer wg.Done()
187-
db.Finalise(true)
188-
}
189-
190185
var wg sync.WaitGroup
191-
wg.Add(3)
192-
go finalise(&wg, orig)
193-
go finalise(&wg, copy)
194-
go finalise(&wg, ccopy)
186+
wg.Go(func() { orig.Finalise(true) })
187+
wg.Go(func() { copy.Finalise(true) })
188+
wg.Go(func() { ccopy.Finalise(true) })
195189
wg.Wait()
196190

197191
// Verify that the three states have been updated independently

core/state_processor.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,17 @@ func InitSignerInTransactions(config *params.ChainConfig, header *types.Header,
585585
chunkSize++
586586
}
587587
wg := sync.WaitGroup{}
588-
wg.Add(nWorker)
589588
for i := 0; i < nWorker; i++ {
590589
from := i * chunkSize
591590
to := from + chunkSize
592591
if to > txs.Len() {
593592
to = txs.Len()
594593
}
595-
go func(from int, to int) {
594+
wg.Go(func() {
596595
for j := from; j < to; j++ {
597596
types.CacheSigner(signer, txs[j])
598597
}
599-
wg.Done()
600-
}(from, to)
598+
})
601599
}
602600
wg.Wait()
603601
}

core/txpool/lending_pool.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ func NewLendingPool(chainconfig *params.ChainConfig, chain blockChainLending) *L
182182
pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh)
183183

184184
// Start the event loop and return
185-
pool.wg.Add(1)
186-
go pool.loop()
185+
pool.wg.Go(pool.loop)
187186

188187
return pool
189188
}
@@ -192,8 +191,6 @@ func NewLendingPool(chainconfig *params.ChainConfig, chain blockChainLending) *L
192191
// outside blockchain events as well as for various reporting and transaction
193192
// eviction events.
194193
func (pool *LendingPool) loop() {
195-
defer pool.wg.Done()
196-
197194
// Start the stats reporting and transaction eviction tickers
198195
var prevPending, prevQueued int
199196

0 commit comments

Comments
 (0)