Browse Source

mocknet: create a connection on NewStream if we need one

That's what the Swarm does and that's what the function's documentation says it
does.
pull/480/head
Steven Allen 6 years ago
parent
commit
79ba610d21
  1. 22
      p2p/net/mock/mock_peernet.go

22
p2p/net/mock/mock_peernet.go

@ -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()
}

Loading…
Cancel
Save