diff --git a/graph/db/graph.go b/graph/db/graph.go index fa8a6c29c..9e35e58dd 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -12,7 +12,6 @@ import ( "github.com/lightningnetwork/lnd/batch" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/kvdb" - "github.com/lightningnetwork/lnd/lnutils" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -50,26 +49,7 @@ type ChannelGraph struct { graphCache *GraphCache *KVStore - - // ntfnClientCounter is an atomic counter that's used to assign unique - // notification client IDs to new clients. - ntfnClientCounter atomic.Uint64 - - // topologyUpdate is a channel that carries new topology updates - // messages from outside the ChannelGraph to be processed by the - // networkHandler. - topologyUpdate chan any - - // topologyClients maps a client's unique notification ID to a - // topologyClient client that contains its notification dispatch - // channel. - topologyClients *lnutils.SyncMap[uint64, *topologyClient] - - // ntfnClientUpdates is a channel that's used to send new updates to - // topology notification clients to the ChannelGraph. Updates either - // add a new notification client, or cancel notifications for an - // existing client. - ntfnClientUpdates chan *topologyClientUpdate + *topologyManager quit chan struct{} wg sync.WaitGroup @@ -90,11 +70,9 @@ func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph, } g := &ChannelGraph{ - KVStore: store, - topologyUpdate: make(chan any), - topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{}, - ntfnClientUpdates: make(chan *topologyClientUpdate), - quit: make(chan struct{}), + KVStore: store, + topologyManager: newTopologyManager(), + quit: make(chan struct{}), } // The graph cache can be turned off (e.g. for mobile users) for a diff --git a/graph/db/notifications.go b/graph/db/notifications.go index 7d54a7431..2ed2be16f 100644 --- a/graph/db/notifications.go +++ b/graph/db/notifications.go @@ -5,15 +5,50 @@ import ( "image/color" "net" "sync" + "sync/atomic" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/wire" "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/graph/db/models" + "github.com/lightningnetwork/lnd/lnutils" "github.com/lightningnetwork/lnd/lnwire" ) +// topologyManager holds all the fields required to manage the network topology +// subscriptions and notifications. +type topologyManager struct { + // ntfnClientCounter is an atomic counter that's used to assign unique + // notification client IDs to new clients. + ntfnClientCounter atomic.Uint64 + + // topologyUpdate is a channel that carries new topology updates + // messages from outside the ChannelGraph to be processed by the + // networkHandler. + topologyUpdate chan any + + // topologyClients maps a client's unique notification ID to a + // topologyClient client that contains its notification dispatch + // channel. + topologyClients *lnutils.SyncMap[uint64, *topologyClient] + + // ntfnClientUpdates is a channel that's used to send new updates to + // topology notification clients to the ChannelGraph. Updates either + // add a new notification client, or cancel notifications for an + // existing client. + ntfnClientUpdates chan *topologyClientUpdate +} + +// newTopologyManager creates a new instance of the topologyManager. +func newTopologyManager() *topologyManager { + return &topologyManager{ + topologyUpdate: make(chan any), + topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{}, + ntfnClientUpdates: make(chan *topologyClientUpdate), + } +} + // TopologyClient represents an intent to receive notifications from the // channel router regarding changes to the topology of the channel graph. The // TopologyChanges channel will be sent upon with new updates to the channel