Commit ee8d7e13 authored by Jeromy's avatar Jeromy

use optimized 'HaveConnToPeer' checks

parent e5826f12
...@@ -69,9 +69,9 @@ ...@@ -69,9 +69,9 @@
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmTMNkpso2WRMevXC8ZxgyBhJvoEHvk24SNeUr9Mf9UM1a", "hash": "QmPQnnKQxdtUg8FPXa7BEEkR8gcgo8BuU3T6jtTCB4CR6M",
"name": "go-peerstream", "name": "go-peerstream",
"version": "2.0.2" "version": "2.0.3"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
......
...@@ -273,7 +273,7 @@ func (s *Swarm) SetStreamHandler(handler inet.StreamHandler) { ...@@ -273,7 +273,7 @@ func (s *Swarm) SetStreamHandler(handler inet.StreamHandler) {
// NewStreamWithPeer creates a new stream on any available connection to p // NewStreamWithPeer creates a new stream on any available connection to p
func (s *Swarm) NewStreamWithPeer(ctx context.Context, p peer.ID) (*Stream, error) { func (s *Swarm) NewStreamWithPeer(ctx context.Context, p peer.ID) (*Stream, error) {
// if we have no connections, try connecting. // if we have no connections, try connecting.
if len(s.ConnectionsToPeer(p)) == 0 { if !s.HaveConnsToPeer(p) {
log.Debug("Swarm: NewStreamWithPeer no connections. Attempting to connect...") log.Debug("Swarm: NewStreamWithPeer no connections. Attempting to connect...")
if _, err := s.Dial(ctx, p); err != nil { if _, err := s.Dial(ctx, p); err != nil {
return nil, err return nil, err
...@@ -288,16 +288,11 @@ func (s *Swarm) NewStreamWithPeer(ctx context.Context, p peer.ID) (*Stream, erro ...@@ -288,16 +288,11 @@ func (s *Swarm) NewStreamWithPeer(ctx context.Context, p peer.ID) (*Stream, erro
// ConnectionsToPeer returns all the live connections to p // ConnectionsToPeer returns all the live connections to p
func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn { func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn {
return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns())) return wrapConns(s.swarm.ConnsWithGroup(p))
} }
func (s *Swarm) HaveConnsToPeer(p peer.ID) bool { func (s *Swarm) HaveConnsToPeer(p peer.ID) bool {
for _, c := range s.swarm.Conns() { return len(s.swarm.ConnsWithGroup(p)) > 0
if c.InGroup(p) {
return true
}
}
return false
} }
// Connections returns a slice of all connections. // Connections returns a slice of all connections.
......
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