identify: remove snapshot handling

This commit is contained in:
Marten Seemann 2023-01-05 17:31:22 +13:00
parent 56078a18f7
commit 5095f44da2
2 changed files with 12 additions and 26 deletions

View File

@ -400,10 +400,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())
}
@ -471,7 +468,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

View File

@ -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)
}