Skip to content

Commit

Permalink
gracefully recover from libp2p subscribe errors
Browse files Browse the repository at this point in the history
  • Loading branch information
peterargue committed Sep 3, 2024
1 parent 52d4438 commit 3ef8940
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,32 @@ func (bsnet *impl) Start(r ...Receiver) {
bsnet.host.SetStreamHandler(proto, bsnet.handleNewStream)
}

// try to subscribe to libp2p events that indicate a change in connection state
// if this fails, continue as normal
err := bsnet.trySubscribePeerUpdates()
if err != nil {
log.Errorf("failed to subscribe to libp2p events: %s", err)
}

// listen for disconnects and start processing the events
bsnet.host.Network().Notify((*netNotifiee)(bsnet))
bsnet.connectEvtMgr.Start()
}

func (bsnet *impl) Stop() {
bsnet.connectEvtMgr.Stop()
bsnet.host.Network().StopNotify((*netNotifiee)(bsnet))
bsnet.cancel()
}

func (bsnet *impl) trySubscribePeerUpdates() error {
// first, subscribe to libp2p events that indicate a change in connection state
sub, err := bsnet.host.EventBus().Subscribe([]interface{}{
&event.EvtPeerProtocolsUpdated{},
&event.EvtPeerIdentificationCompleted{},
})
if err != nil {
panic(err)
return err
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -379,15 +398,7 @@ func (bsnet *impl) Start(r ...Receiver) {
}
}

// finally, listen for disconnects and start processing the events
bsnet.host.Network().Notify((*netNotifiee)(bsnet))
bsnet.connectEvtMgr.Start()
}

func (bsnet *impl) Stop() {
bsnet.connectEvtMgr.Stop()
bsnet.host.Network().StopNotify((*netNotifiee)(bsnet))
bsnet.cancel()
return nil
}

func (bsnet *impl) peerUpdatedSubscription(ctx context.Context, sub event.Subscription) {
Expand Down

0 comments on commit 3ef8940

Please sign in to comment.