Skip to content

Commit 3ef8940

Browse files
committed
gracefully recover from libp2p subscribe errors
1 parent 52d4438 commit 3ef8940

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

bitswap/network/ipfs_impl.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,32 @@ func (bsnet *impl) Start(r ...Receiver) {
356356
bsnet.host.SetStreamHandler(proto, bsnet.handleNewStream)
357357
}
358358

359+
// try to subscribe to libp2p events that indicate a change in connection state
360+
// if this fails, continue as normal
361+
err := bsnet.trySubscribePeerUpdates()
362+
if err != nil {
363+
log.Errorf("failed to subscribe to libp2p events: %s", err)
364+
}
365+
366+
// listen for disconnects and start processing the events
367+
bsnet.host.Network().Notify((*netNotifiee)(bsnet))
368+
bsnet.connectEvtMgr.Start()
369+
}
370+
371+
func (bsnet *impl) Stop() {
372+
bsnet.connectEvtMgr.Stop()
373+
bsnet.host.Network().StopNotify((*netNotifiee)(bsnet))
374+
bsnet.cancel()
375+
}
376+
377+
func (bsnet *impl) trySubscribePeerUpdates() error {
359378
// first, subscribe to libp2p events that indicate a change in connection state
360379
sub, err := bsnet.host.EventBus().Subscribe([]interface{}{
361380
&event.EvtPeerProtocolsUpdated{},
362381
&event.EvtPeerIdentificationCompleted{},
363382
})
364383
if err != nil {
365-
panic(err)
384+
return err
366385
}
367386

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

382-
// finally, listen for disconnects and start processing the events
383-
bsnet.host.Network().Notify((*netNotifiee)(bsnet))
384-
bsnet.connectEvtMgr.Start()
385-
}
386-
387-
func (bsnet *impl) Stop() {
388-
bsnet.connectEvtMgr.Stop()
389-
bsnet.host.Network().StopNotify((*netNotifiee)(bsnet))
390-
bsnet.cancel()
401+
return nil
391402
}
392403

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

0 commit comments

Comments
 (0)