fix: replace all time.after with the time.newtimer (#400)

This commit is contained in:
Banana-J 2024-01-27 21:01:24 +11:00 committed by GitHub
parent 0d7dd867b7
commit 2504961a33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 4 deletions

View file

@ -198,11 +198,19 @@ func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscripti
// forces a manual refresh (only triggers for systems where the filesystem notifier
// is not running).
func (ks *KeyStore) updater() {
// Create a timer for the wallet refresh cycle
timer := time.NewTimer(walletRefreshCycle)
defer timer.Stop()
for {
// Wait for an account update or a refresh timeout
select {
case <-ks.changes:
case <-time.After(walletRefreshCycle):
// Stop the timer if we receive an account update before the timer fires
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
}
// Run the wallet refresher
ks.refreshWallets()
@ -215,6 +223,9 @@ func (ks *KeyStore) updater() {
return
}
ks.mu.Unlock()
// Reset the timer for the next cycle
timer.Reset(walletRefreshCycle)
}
}

View file

@ -1088,8 +1088,10 @@ func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types
// Periodlly execution(Attached to engine initialisation during "new"). Used for pool cleaning etc
func (x *XDPoS_v2) periodicJob() {
go func() {
ticker := time.NewTicker(utils.PeriodicJobPeriod * time.Second)
defer ticker.Stop()
for {
<-time.After(utils.PeriodicJobPeriod * time.Second)
<-ticker.C
x.hygieneVotePool()
x.hygieneTimeoutPool()
}

View file

@ -156,12 +156,16 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
defer close(done)
go func() {
waitDuration := 3 * time.Second
timer := time.NewTimer(waitDuration)
defer timer.Stop()
for {
select {
case <-done:
return
case <-time.After(3 * time.Second):
case <-timer.C:
logger.Info("Generating ethash verification cache", "percentage", atomic.LoadUint32(&progress)*100/uint32(rows)/4, "elapsed", common.PrettyDuration(time.Since(start)))
timer.Reset(waitDuration)
}
}
}()

View file

@ -606,6 +606,9 @@ func (s *MatcherSession) DeliverSections(bit uint, sections []uint64, bitsets []
// of the session, any request in-flight need to be responded to! Empty responses
// are fine though in that case.
func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan *Retrieval) {
ticker := time.NewTicker(wait)
defer ticker.Stop()
for {
// Allocate a new bloom bit index to retrieve data for, stopping when done
bit, ok := s.AllocateRetrieval()
@ -621,7 +624,7 @@ func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan
s.DeliverSections(bit, []uint64{}, [][]byte{})
return
case <-time.After(wait):
case <-ticker.C:
// Throttling up, fetch whatever's available
}
}