Update manager.go

This commit is contained in:
songzhibin97 2024-03-02 18:49:31 +08:00
parent 0a2f33946b
commit e5827c9708

View file

@ -20,6 +20,7 @@ import (
"reflect" "reflect"
"sort" "sort"
"sync" "sync"
"sync/atomic"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
@ -56,9 +57,10 @@ type Manager struct {
feed event.Feed // Wallet feed notifying of arrivals/departures feed event.Feed // Wallet feed notifying of arrivals/departures
quit chan chan error quit chan chan error
term chan struct{} // Channel is closed upon termination of the update loop term chan struct{} // Channel is closed upon termination of the update loop
lock sync.RWMutex close atomic.Bool // Whether the manager has been closed
lock sync.RWMutex
} }
// NewManager creates a generic account manager to sign transaction via various // NewManager creates a generic account manager to sign transaction via various
@ -98,6 +100,10 @@ func NewManager(config *Config, backends ...Backend) *Manager {
// Close terminates the account manager's internal notification processes. // Close terminates the account manager's internal notification processes.
func (am *Manager) Close() error { func (am *Manager) Close() error {
if am.close.Swap(true) {
return nil
}
for _, w := range am.wallets { for _, w := range am.wallets {
w.Close() w.Close()
} }
@ -114,6 +120,10 @@ func (am *Manager) Config() *Config {
// AddBackend starts the tracking of an additional backend for wallet updates. // AddBackend starts the tracking of an additional backend for wallet updates.
// cmd/geth assumes once this func returns the backends have been already integrated. // cmd/geth assumes once this func returns the backends have been already integrated.
func (am *Manager) AddBackend(backend Backend) { func (am *Manager) AddBackend(backend Backend) {
if am.close.Load() {
return
}
done := make(chan struct{}) done := make(chan struct{})
am.newBackends <- newBackendEvent{backend, done} am.newBackends <- newBackendEvent{backend, done}
<-done <-done