From 5093dc16050ce64628ce1ff7e68f9dc20abb125f Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 5 Jan 2023 17:31:22 +1300 Subject: [PATCH] identify: remove snapshot handling --- p2p/protocol/identify/id.go | 8 +++----- p2p/protocol/identify/peer_loop.go | 30 +++++++++--------------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index e804ca8e5f..3f10c3fc88 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -399,10 +399,7 @@ func (ids *idService) sendIdentifyResp(s network.Stream) { return } - ph.snapshotMu.RLock() - snapshot := ph.snapshot - ph.snapshotMu.RUnlock() - ids.writeChunkedIdentifyMsg(c, snapshot, s) + ids.writeChunkedIdentifyMsg(c, s) log.Debugf("%s sent message to %s %s", ID, c.RemotePeer(), c.RemoteMultiaddr()) } @@ -470,7 +467,8 @@ func (ids *idService) getSnapshot() *identifySnapshot { return snapshot } -func (ids *idService) writeChunkedIdentifyMsg(c network.Conn, snapshot *identifySnapshot, s network.Stream) error { +func (ids *idService) writeChunkedIdentifyMsg(c network.Conn, s network.Stream) error { + snapshot := ids.getSnapshot() mes := ids.createBaseIdentifyResponse(c, snapshot) sr := ids.getSignedRecord(snapshot) mes.SignedPeerRecord = sr diff --git a/p2p/protocol/identify/peer_loop.go b/p2p/protocol/identify/peer_loop.go index 15c7e85b68..589f04a471 100644 --- a/p2p/protocol/identify/peer_loop.go +++ b/p2p/protocol/identify/peer_loop.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "sync" "time" "github.com/libp2p/go-libp2p/core/network" @@ -29,23 +28,15 @@ type peerHandler struct { pid peer.ID - snapshotMu sync.RWMutex - snapshot *identifySnapshot - pushCh chan struct{} } func newPeerHandler(pid peer.ID, ids *idService) *peerHandler { - ph := &peerHandler{ - ids: ids, - pid: pid, - - snapshot: ids.getSnapshot(), - + return &peerHandler{ + ids: ids, + pid: pid, pushCh: make(chan struct{}, 1), } - - return ph } // start starts a handler. This may only be called on a stopped handler, and must @@ -63,7 +54,10 @@ func (ph *peerHandler) start(ctx context.Context, onExit func()) { ctx, cancel := context.WithCancel(ctx) ph.cancel = cancel - go ph.loop(ctx, onExit) + go func() { + ph.loop(ctx) + onExit() + }() } // stop stops a handler. This may not be called concurrently with any @@ -77,9 +71,7 @@ func (ph *peerHandler) stop() error { } // per peer loop for pushing updates -func (ph *peerHandler) loop(ctx context.Context, onExit func()) { - defer onExit() - +func (ph *peerHandler) loop(ctx context.Context) { for { select { // our listen addresses have changed, send an IDPush. @@ -104,11 +96,7 @@ func (ph *peerHandler) sendPush(ctx context.Context) error { } defer dp.Close() - snapshot := ph.ids.getSnapshot() - ph.snapshotMu.Lock() - ph.snapshot = snapshot - ph.snapshotMu.Unlock() - if err := ph.ids.writeChunkedIdentifyMsg(dp.Conn(), snapshot, dp); err != nil { + if err := ph.ids.writeChunkedIdentifyMsg(dp.Conn(), dp); err != nil { _ = dp.Reset() return fmt.Errorf("failed to send push message: %w", err) }