diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index c0e08fecef00..93854794c4e4 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -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) } } diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 9364d69e2047..fc2c46edd84f 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -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() } diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go index 45e28f02bf08..f8dca9c4083b 100644 --- a/consensus/ethash/algorithm.go +++ b/consensus/ethash/algorithm.go @@ -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) } } }() diff --git a/core/bloombits/matcher.go b/core/bloombits/matcher.go index 2b219b33fb06..724114137574 100644 --- a/core/bloombits/matcher.go +++ b/core/bloombits/matcher.go @@ -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 } }