use an elegant way to close the coroutine instead of closing it inside a function

This commit is contained in:
xiaodong 2024-05-21 23:47:02 +08:00
parent ffac091de8
commit fea1f5bbf8
No known key found for this signature in database
GPG key ID: 8CCE1F1B2E204F2B

View file

@ -312,7 +312,10 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A
// Start the reorg loop early, so it can handle requests generated during // Start the reorg loop early, so it can handle requests generated during
// journal loading. // journal loading.
pool.wg.Add(1) pool.wg.Add(1)
go pool.scheduleReorgLoop() go func() {
defer pool.wg.Done()
pool.scheduleReorgLoop()
}()
// If local transactions and journaling is enabled, load from disk // If local transactions and journaling is enabled, load from disk
if pool.journal != nil { if pool.journal != nil {
@ -324,7 +327,11 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A
} }
} }
pool.wg.Add(1) pool.wg.Add(1)
go pool.loop() go func() {
defer pool.wg.Done()
pool.loop()
}()
return nil return nil
} }
@ -332,7 +339,6 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A
// outside blockchain events as well as for various reporting and transaction // outside blockchain events as well as for various reporting and transaction
// eviction events. // eviction events.
func (pool *LegacyPool) loop() { func (pool *LegacyPool) loop() {
defer pool.wg.Done()
var ( var (
prevPending, prevQueued, prevStales int prevPending, prevQueued, prevStales int
@ -1188,8 +1194,6 @@ func (pool *LegacyPool) queueTxEvent(tx *types.Transaction) {
// call those methods directly, but request them being run using requestReset and // call those methods directly, but request them being run using requestReset and
// requestPromoteExecutables instead. // requestPromoteExecutables instead.
func (pool *LegacyPool) scheduleReorgLoop() { func (pool *LegacyPool) scheduleReorgLoop() {
defer pool.wg.Done()
var ( var (
curDone chan struct{} // non-nil while runReorg is active curDone chan struct{} // non-nil while runReorg is active
nextDone = make(chan struct{}) nextDone = make(chan struct{})