accounts/keystore: use ticker to avoid timer allocations #32732 (#1592)

This commit is contained in:
wit liu 2025-10-08 12:50:48 +08:00 committed by GitHub
parent f9f47d2836
commit 7c5d2a45da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -196,23 +196,17 @@ 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()
ticker := time.NewTicker(walletRefreshCycle)
defer ticker.Stop()
for {
// Wait for an account update or a refresh timeout
select {
case <-ks.changes:
// Stop the timer if we receive an account update before the timer fires
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
case <-ticker.C:
}
// Run the wallet refresher
ks.refreshWallets()
// If all our subscribers left, stop the updater
ks.mu.Lock()
if ks.updateScope.Count() == 0 {
@ -221,9 +215,6 @@ func (ks *KeyStore) updater() {
return
}
ks.mu.Unlock()
// Reset the timer for the next cycle
timer.Reset(walletRefreshCycle)
}
}