discovery: shorten mutex locking closure

This commit is contained in:
yyforyongyu 2021-12-14 03:02:41 +08:00
parent 1c3cabee7f
commit 2250cb752b
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 15 additions and 5 deletions

View File

@ -1822,8 +1822,10 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
// making decisions based on this DB state, before it
// writes to the DB.
d.channelMtx.Lock(msg.ShortChannelID.ToUint64())
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
if err := d.cfg.Router.AddEdge(edge, schedulerOp...); err != nil {
err := d.cfg.Router.AddEdge(edge, schedulerOp...)
if err != nil {
defer d.channelMtx.Unlock(msg.ShortChannelID.ToUint64())
// If the edge was rejected due to already being known,
// then it may be that case that this new message has a
// fresh channel proof, so we'll check.
@ -1871,6 +1873,9 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
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,
// we can now process them, as the channel is added to the
// graph.

View File

@ -131,10 +131,10 @@ spawnHandler:
// spawnPeerMsgHandler spawns a peerHandler for the given peer if there isn't
// one already active. The boolean returned signals whether there was already
// one active or not.
func (s *reliableSender) spawnPeerHandler(peerPubKey [33]byte) (peerManager, bool) {
s.activePeersMtx.Lock()
defer s.activePeersMtx.Unlock()
func (s *reliableSender) spawnPeerHandler(
peerPubKey [33]byte) (peerManager, bool) {
s.activePeersMtx.Lock()
msgHandler, ok := s.activePeers[peerPubKey]
if !ok {
msgHandler = peerManager{
@ -142,7 +142,12 @@ func (s *reliableSender) spawnPeerHandler(peerPubKey [33]byte) (peerManager, boo
done: make(chan struct{}),
}
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)
go s.peerHandler(msgHandler, peerPubKey)
}