Skip to content

Commit

Permalink
mocknet: create a connection on NewStream if we need one
Browse files Browse the repository at this point in the history
That's what the Swarm does and that's what the function's documentation says it
does.
  • Loading branch information
Stebalien committed Nov 8, 2018
1 parent a8e25bf commit 79ba610
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions p2p/net/mock/mock_peernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,26 +330,10 @@ func (pn *peernet) Connectedness(p peer.ID) inet.Connectedness {
// NewStream returns a new stream to given peer p.
// If there is no connection to p, attempts to create one.
func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
pn.Lock()
cs, found := pn.connsByPeer[p]
if !found || len(cs) < 1 {
pn.Unlock()
return nil, fmt.Errorf("no connection to peer")
c, err := pn.DialPeer(ctx, p)
if err != nil {
return nil, err
}

// if many conns are found, how do we select? for now, randomly...
// this would be an interesting place to test logic that can measure
// links (network interfaces) and select properly
n := rand.Intn(len(cs))
var c *conn
for c = range cs {
if n == 0 {
break
}
n--
}
pn.Unlock()

return c.NewStream()
}

Expand Down

0 comments on commit 79ba610

Please sign in to comment.