From b1df6818ef305c5351568c2a51a97992a5791c54 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 1 Apr 2021 19:13:39 +0300 Subject: [PATCH] ignore transient connections --- notify.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/notify.go b/notify.go index 07d2e1eb..c11f6210 100644 --- a/notify.go +++ b/notify.go @@ -2,6 +2,7 @@ package pubsub import ( "github.com/libp2p/go-libp2p-core/network" + "github.com/libp2p/go-libp2p-core/peer" ma "github.com/multiformats/go-multiaddr" ) @@ -16,6 +17,10 @@ func (p *PubSubNotif) ClosedStream(n network.Network, s network.Stream) { } func (p *PubSubNotif) Connected(n network.Network, c network.Conn) { + // ignore transient connections + if c.Stat().Transient { + return + } go func() { select { case p.newPeers <- c.RemotePeer(): @@ -34,9 +39,22 @@ func (p *PubSubNotif) ListenClose(n network.Network, _ ma.Multiaddr) { } func (p *PubSubNotif) Initialize() { - for _, pr := range p.host.Network().Peers() { + isTransient := func(pid peer.ID) bool { + for _, c := range p.host.Network().ConnsToPeer(pid) { + if !c.Stat().Transient { + return false + } + } + + return true + } + + for _, pid := range p.host.Network().Peers() { + if isTransient(pid) { + continue + } select { - case p.newPeers <- pr: + case p.newPeers <- pid: case <-p.ctx.Done(): } }