Skip to content

Commit

Permalink
fix: handle EvtPeerProtocolsUpdated in local discovery driver
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jun 23, 2023
1 parent 756b2c0 commit 8067e91
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/tinder/driver_localdiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,15 @@ func (ld *LocalDiscovery) handleConnection(ctx context.Context, p peer.ID) {
conns := ld.h.Network().ConnsToPeer(p)
for _, conn := range conns {
if manet.IsPrivateAddr(conn.RemoteMultiaddr()) || isProximityProtocol(conn.RemoteMultiaddr()) {
ld.logger.Info("found local peer", logutil.PrivateString("peer", conn.RemotePeer().String()))
go func() {
records := ld.getLocalReccord()
if err := ld.sendRecordsTo(ctx, p, records); err != nil {
ld.logger.Warn("unable to send local record",
logutil.PrivateString("peer", p.String()),
zap.Int("records", len(records.Records)),
zap.Error(err))
} else {
ld.logger.Info("send topics to local peer", logutil.PrivateString("peer", conn.RemotePeer().String()))
}
}()

Expand All @@ -420,8 +421,10 @@ func (ld *LocalDiscovery) handleConnection(ctx context.Context, p peer.ID) {
}

func (ld *LocalDiscovery) monitorConnection(ctx context.Context) error {
sub, err := ld.h.EventBus().Subscribe(new(event.EvtPeerConnectednessChanged),
eventbus.Name("weshnet/tinder/monitor-connection"))
sub, err := ld.h.EventBus().Subscribe([]interface{}{
new(event.EvtPeerConnectednessChanged),
new(event.EvtPeerProtocolsUpdated),
}, eventbus.Name("weshnet/tinder/monitor-connection"))
if err != nil {
return fmt.Errorf("unable to subscribe to `EvtPeerConnectednessChanged`: %w", err)
}
Expand All @@ -443,14 +446,20 @@ func (ld *LocalDiscovery) monitorConnection(ctx context.Context) error {
return
}

evt := e.(event.EvtPeerConnectednessChanged)

// send record to connected peer only
if evt.Connectedness != network.Connected {
continue
switch evt := e.(type) {
case event.EvtPeerConnectednessChanged:
// send record to connected peer only
if evt.Connectedness == network.Connected {
ld.handleConnection(ctx, evt.Peer)
}
case event.EvtPeerProtocolsUpdated:
for _, added := range evt.Added {
if added == recProtocolID {
ld.handleConnection(ctx, evt.Peer)
break
}
}
}

ld.handleConnection(ctx, evt.Peer)
}
}()

Expand Down

0 comments on commit 8067e91

Please sign in to comment.