Commit 11cc37e4 authored by aloknerurkar's avatar aloknerurkar Committed by GitHub

fix(libp2p): cleanup peer connection after ping is done (#2403)

parent 11d393e2
......@@ -37,6 +37,7 @@ const (
peersStreamName = "peers"
messageTimeout = 1 * time.Minute // maximum allowed time for a message to be read or written.
maxBatchSize = 30
pingTimeout = time.Second * 5 // time to wait for ping to succeed
)
var (
......@@ -277,6 +278,9 @@ func (s *Service) checkAndAddPeers(ctx context.Context, peers pb.Peers) {
return
}
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
defer cancel()
// check if the underlay is usable by doing a raw ping using libp2p
if _, err = s.streamer.Ping(ctx, multiUnderlay); err != nil {
s.metrics.UnreachablePeers.Inc()
......
......@@ -860,6 +860,11 @@ func (s *Service) Ping(ctx context.Context, addr ma.Multiaddr) (rtt time.Duratio
// Add the address to libp2p peerstore for it to be dialable
s.pingDialer.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.TempAddrTTL)
// Cleanup connection after ping is done
defer func() {
_ = s.pingDialer.Network().ClosePeer(info.ID)
}()
select {
case <-ctx.Done():
return rtt, ctx.Err()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment