From a507d12bd461876650094dc0277fed6f8f4a184c Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 24 Aug 2021 15:00:34 +0100 Subject: [PATCH] clean up messy defer logic in IDService.sendIdentifyResp --- p2p/protocol/identify/id.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index c950289d..5f9a9fe4 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -381,14 +381,7 @@ func (ids *IDService) identifyConn(c network.Conn, signal chan struct{}) { } func (ids *IDService) sendIdentifyResp(s network.Stream) { - var ph *peerHandler - - defer func() { - _ = s.Close() - if ph != nil { - ph.snapshotMu.RUnlock() - } - }() + defer s.Close() c := s.Conn() @@ -399,6 +392,7 @@ func (ids *IDService) sendIdentifyResp(s network.Stream) { return } + var ph *peerHandler select { case ph = <-phCh: case <-ids.ctx.Done(): @@ -412,7 +406,9 @@ func (ids *IDService) sendIdentifyResp(s network.Stream) { } ph.snapshotMu.RLock() - ids.writeChunkedIdentifyMsg(c, ph.snapshot, s) + snapshot := ph.snapshot + ph.snapshotMu.RUnlock() + ids.writeChunkedIdentifyMsg(c, snapshot, s) log.Debugf("%s sent message to %s %s", ID, c.RemotePeer(), c.RemoteMultiaddr()) }