discovery: shorten mutex locking closure

This commit is contained in:
yyforyongyu committed 2022-01-05 04:27:32 +08:00
1 parent 1c3cabee7f
commit 2250cb752b
2 files changed
+15 -5

No files matched your search

+7 -2
View File
@@ -1822,8 +1822,10 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
// making decisions based on this DB state, before it // making decisions based on this DB state, before it
// writes to the DB. // writes to the DB.
d.channelMtx.Lock(msg.ShortChannelID.ToUint64()) d.channelMtx.Lock(msg.ShortChannelID.ToUint64())
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64()) err := d.cfg.Router.AddEdge(edge, schedulerOp...)
if err := d.cfg.Router.AddEdge(edge, schedulerOp...); err != nil { if err != nil {
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
// If the edge was rejected due to already being known, // If the edge was rejected due to already being known,
// then it may be that case that this new message has a // then it may be that case that this new message has a
// fresh channel proof, so we'll check. // fresh channel proof, so we'll check.
@@ -1871,6 +1873,9 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
return nil, false return nil, false
} }
// If err is nil, release the lock immediately.
d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
// If we earlier received any ChannelUpdates for this channel, // If we earlier received any ChannelUpdates for this channel,
// we can now process them, as the channel is added to the // we can now process them, as the channel is added to the
// graph. // graph.
+8 -3
View File
@@ -131,10 +131,10 @@ spawnHandler:
// spawnPeerMsgHandler spawns a peerHandler for the given peer if there isn't // spawnPeerMsgHandler spawns a peerHandler for the given peer if there isn't
// one already active. The boolean returned signals whether there was already // one already active. The boolean returned signals whether there was already
// one active or not. // one active or not.
func (s *reliableSender) spawnPeerHandler(peerPubKey [33]byte) (peerManager, bool) { func (s *reliableSender) spawnPeerHandler(
s.activePeersMtx.Lock() peerPubKey [33]byte) (peerManager, bool) {
defer s.activePeersMtx.Unlock()
s.activePeersMtx.Lock()
msgHandler, ok := s.activePeers[peerPubKey] msgHandler, ok := s.activePeers[peerPubKey]
if !ok { if !ok {
msgHandler = peerManager{ msgHandler = peerManager{
@@ -142,7 +142,12 @@ func (s *reliableSender) spawnPeerHandler(peerPubKey [33]byte) (peerManager, boo
done: make(chan struct{}), done: make(chan struct{}),
} }
s.activePeers[peerPubKey] = msgHandler s.activePeers[peerPubKey] = msgHandler
}
s.activePeersMtx.Unlock()
// If this is a newly initiated peerManager, we will create a
// peerHandler.
if !ok {
s.wg.Add(1) s.wg.Add(1)
go s.peerHandler(msgHandler, peerPubKey) go s.peerHandler(msgHandler, peerPubKey)
} }