Unverified Commit e6eff445 authored by vyzo's avatar vyzo Committed by GitHub

Ignore transient connections (#412)

parent 7a3d0ca3
This diff is collapsed.
......@@ -449,7 +449,13 @@ func (gs *GossipSubRouter) AddPeer(p peer.ID, proto protocol.ID) {
conns := gs.p.host.Network().ConnsToPeer(p)
loop:
for _, c := range conns {
if c.Stat().Direction == network.DirOutbound {
stat := c.Stat()
if stat.Transient {
continue
}
if stat.Direction == network.DirOutbound {
// only count the connection if it has a pubsub stream
for _, s := range c.GetStreams() {
if s.Protocol() == proto {
......
......@@ -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():
}
}
......
......@@ -303,6 +303,10 @@ func (pg *peerGater) getPeerIP(p peer.ID) string {
// most streams; it's a nightmare to track multiple IPs per peer, so pick the best one.
streams := make(map[string]int)
for _, c := range conns {
if c.Stat().Transient {
// ignore transient
continue
}
streams[c.ID()] = len(c.GetStreams())
}
sort.Slice(conns, func(i, j int) bool {
......
......@@ -973,6 +973,11 @@ func (ps *peerScore) getIPs(p peer.ID) []string {
conns := ps.host.Network().ConnsToPeer(p)
res := make([]string, 0, 1)
for _, c := range conns {
if c.Stat().Transient {
// ignore transient
continue
}
remote := c.RemoteMultiaddr()
ip, err := manet.ToIP(remote)
if err != nil {
......
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