Skip to content

Commit d757b3b

Browse files
committed
graph: refactor Builder network message handling
The point of the `graph.Builder`'s `networkHandler` goroutine is to ensure that certain requests are handled in a synchronous fashion. However, any requests received on the `networkUpdates` channel, are currently immediately handled in a goroutine which calls `handleNetworkUpdate` which calls `processUpdate` before doing topology notifications. In other words, there is no reason for these `networkUpdates` to be handled in the `networkHandler` since they are always handled asynchronously anyways. This design is most likely due to the fact that originally the gossiper and graph builder code lived in the same system and so the pattern was copied across. So in this commit, we just remove the complexity. The only part we need to spin off in a goroutine is the topology notifications.
1 parent 276b335 commit d757b3b

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

graph/builder.go

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ type Builder struct {
131131
// of our currently known best chain are sent over.
132132
staleBlocks <-chan *chainview.FilteredBlock
133133

134-
// networkUpdates is a channel that carries new topology updates
135-
// messages from outside the Builder to be processed by the
136-
// networkHandler.
137-
networkUpdates chan *routingMsg
138-
139134
// topologyClients maps a client's unique notification ID to a
140135
// topologyClient client that contains its notification dispatch
141136
// channel.
@@ -172,7 +167,6 @@ var _ ChannelGraphSource = (*Builder)(nil)
172167
func NewBuilder(cfg *Config) (*Builder, error) {
173168
return &Builder{
174169
cfg: cfg,
175-
networkUpdates: make(chan *routingMsg),
176170
topologyClients: &lnutils.SyncMap[uint64, *topologyClient]{},
177171
ntfnClientUpdates: make(chan *topologyClientUpdate),
178172
channelEdgeMtx: multimutex.NewMutex[uint64](),
@@ -721,8 +715,8 @@ func (b *Builder) handleNetworkUpdate(update *routingMsg) {
721715

722716
// networkHandler is the primary goroutine for the Builder. The roles of
723717
// this goroutine include answering queries related to the state of the
724-
// network, pruning the graph on new block notification, applying network
725-
// updates, and registering new topology clients.
718+
// network, pruning the graph on new block notification and registering new
719+
// topology clients.
726720
//
727721
// NOTE: This MUST be run as a goroutine.
728722
func (b *Builder) networkHandler() {
@@ -742,17 +736,6 @@ func (b *Builder) networkHandler() {
742736
}
743737

744738
select {
745-
// A new fully validated network update has just arrived. As a
746-
// result we'll modify the channel graph accordingly depending
747-
// on the exact type of the message.
748-
case update := <-b.networkUpdates:
749-
b.wg.Add(1)
750-
go b.handleNetworkUpdate(update)
751-
752-
// TODO(roasbeef): remove all unconnected vertexes
753-
// after N blocks pass with no corresponding
754-
// announcements.
755-
756739
case chainUpdate, ok := <-b.staleBlocks:
757740
// If the channel has been closed, then this indicates
758741
// the daemon is shutting down, so we exit ourselves.
@@ -1182,14 +1165,12 @@ func (b *Builder) AddNode(node *models.LightningNode,
11821165
err: make(chan error, 1),
11831166
}
11841167

1168+
b.wg.Add(1)
1169+
go b.handleNetworkUpdate(rMsg)
1170+
11851171
select {
1186-
case b.networkUpdates <- rMsg:
1187-
select {
1188-
case err := <-rMsg.err:
1189-
return err
1190-
case <-b.quit:
1191-
return ErrGraphBuilderShuttingDown
1192-
}
1172+
case err := <-rMsg.err:
1173+
return err
11931174
case <-b.quit:
11941175
return ErrGraphBuilderShuttingDown
11951176
}
@@ -1235,14 +1216,12 @@ func (b *Builder) AddEdge(edge *models.ChannelEdgeInfo,
12351216
err: make(chan error, 1),
12361217
}
12371218

1219+
b.wg.Add(1)
1220+
go b.handleNetworkUpdate(rMsg)
1221+
12381222
select {
1239-
case b.networkUpdates <- rMsg:
1240-
select {
1241-
case err := <-rMsg.err:
1242-
return err
1243-
case <-b.quit:
1244-
return ErrGraphBuilderShuttingDown
1245-
}
1223+
case err := <-rMsg.err:
1224+
return err
12461225
case <-b.quit:
12471226
return ErrGraphBuilderShuttingDown
12481227
}
@@ -1437,14 +1416,12 @@ func (b *Builder) UpdateEdge(update *models.ChannelEdgePolicy,
14371416
err: make(chan error, 1),
14381417
}
14391418

1419+
b.wg.Add(1)
1420+
go b.handleNetworkUpdate(rMsg)
1421+
14401422
select {
1441-
case b.networkUpdates <- rMsg:
1442-
select {
1443-
case err := <-rMsg.err:
1444-
return err
1445-
case <-b.quit:
1446-
return ErrGraphBuilderShuttingDown
1447-
}
1423+
case err := <-rMsg.err:
1424+
return err
14481425
case <-b.quit:
14491426
return ErrGraphBuilderShuttingDown
14501427
}

0 commit comments

Comments
 (0)