Skip to content

Commit

Permalink
Merge pull request #102 from libp2p/feat/connectedness
Browse files Browse the repository at this point in the history
swarm: add optimized method for checking connectedness to peer
  • Loading branch information
whyrusleeping authored Sep 6, 2016

Verified

This commit was signed with the committer’s verified signature.
ignatiusmb Ignatius Bagus
2 parents 23ccae5 + 324aae3 commit 06db400
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 9 additions & 0 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
@@ -286,6 +286,15 @@ func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn {
return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns()))
}

func (s *Swarm) HaveConnsToPeer(p peer.ID) bool {
for _, c := range s.swarm.Conns() {
if c.InGroup(p) {
return true
}
}
return false
}

// Connections returns a slice of all connections.
func (s *Swarm) Connections() []*Conn {
return wrapConns(s.swarm.Conns())
3 changes: 1 addition & 2 deletions p2p/net/swarm/swarm_net.go
Original file line number Diff line number Diff line change
@@ -123,8 +123,7 @@ func (n *Network) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
// Connectedness returns a state signaling connection capabilities
// For now only returns Connected || NotConnected. Expand into more later.
func (n *Network) Connectedness(p peer.ID) inet.Connectedness {
c := n.Swarm().ConnectionsToPeer(p)
if len(c) > 0 {
if n.Swarm().HaveConnsToPeer(p) {
return inet.Connected
}
return inet.NotConnected

0 comments on commit 06db400

Please sign in to comment.