Skip to content

Commit

Permalink
ignore transient connections
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Apr 1, 2021
1 parent 6cdd161 commit b1df681
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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():
Expand All @@ -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():
}
}
Expand Down

0 comments on commit b1df681

Please sign in to comment.