Skip to content

Commit

Permalink
identify: remove snapshot handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 6, 2023
1 parent f6a420a commit 5093dc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
8 changes: 3 additions & 5 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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
Expand Down
30 changes: 9 additions & 21 deletions p2p/protocol/identify/peer_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/libp2p/go-libp2p/core/network"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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)
}
Expand Down

0 comments on commit 5093dc1

Please sign in to comment.