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

+8 -3
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)
}