accounts/keystore: using atomic.Bool

This commit is contained in:
cuiweixie 2025-07-30 23:30:17 +08:00
parent d14d4d2af0
commit 5f81e3cdc6

View file

@ -30,6 +30,7 @@ import (
"reflect" "reflect"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
@ -68,7 +69,7 @@ type KeyStore struct {
wallets []accounts.Wallet // Wallet wrappers around the individual key files wallets []accounts.Wallet // Wallet wrappers around the individual key files
updateFeed event.Feed // Event feed to notify wallet additions/removals updateFeed event.Feed // Event feed to notify wallet additions/removals
updateScope event.SubscriptionScope // Subscription scope tracking current live listeners updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
updating bool // Whether the event notification loop is running updating atomic.Bool // Whether the event notification loop is running
mu sync.RWMutex mu sync.RWMutex
importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing
@ -182,8 +183,7 @@ func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscripti
sub := ks.updateScope.Track(ks.updateFeed.Subscribe(sink)) sub := ks.updateScope.Track(ks.updateFeed.Subscribe(sink))
// Subscribers require an active notification loop, start it // Subscribers require an active notification loop, start it
if !ks.updating { if !ks.updating.CompareAndSwap(false, true) {
ks.updating = true
go ks.updater() go ks.updater()
} }
return sub return sub
@ -207,7 +207,7 @@ func (ks *KeyStore) updater() {
// If all our subscribers left, stop the updater // If all our subscribers left, stop the updater
ks.mu.Lock() ks.mu.Lock()
if ks.updateScope.Count() == 0 { if ks.updateScope.Count() == 0 {
ks.updating = false ks.updating.Store(false)
ks.mu.Unlock() ks.mu.Unlock()
return return
} }
@ -491,9 +491,7 @@ func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (account
// isUpdating returns whether the event notification loop is running. // isUpdating returns whether the event notification loop is running.
// This method is mainly meant for tests. // This method is mainly meant for tests.
func (ks *KeyStore) isUpdating() bool { func (ks *KeyStore) isUpdating() bool {
ks.mu.RLock() return ks.updating.Load()
defer ks.mu.RUnlock()
return ks.updating
} }
// zeroKey zeroes a private key in memory. // zeroKey zeroes a private key in memory.