diff --git a/mesh/gossip_channel.go b/mesh/gossip_channel.go index f641aa92..38555eb0 100644 --- a/mesh/gossip_channel.go +++ b/mesh/gossip_channel.go @@ -167,20 +167,18 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) { func (c *GossipChannel) Send(srcName PeerName, data GossipData) { // do this outside the lock below so we avoid lock nesting c.routes.EnsureRecalculated() - selectedConnections := make(ConnectionSet) - for name := range c.routes.RandomNeighbours(srcName) { - if conn, found := c.ourself.ConnectionTo(name); found { - selectedConnections[conn] = void - } + destinations := c.routes.RandomNeighbours(srcName) + if len(destinations) == 0 { + return } - c.sendDown(selectedConnections, data) + c.sendDown(c.ourself.ConnectionsTo(destinations), data) } func (c *GossipChannel) SendDown(conn Connection, data GossipData) { - c.sendDown(ConnectionSet{conn: void}, data) + c.sendDown([]Connection{conn}, data) } -func (c *GossipChannel) sendDown(selectedConnections ConnectionSet, data GossipData) { +func (c *GossipChannel) sendDown(selectedConnections []Connection, data GossipData) { if len(selectedConnections) == 0 { return } @@ -207,7 +205,7 @@ func (c *GossipChannel) sendDown(selectedConnections ConnectionSet, data GossipD } } // start senders, if necessary, and send. - for conn := range selectedConnections { + for _, conn := range selectedConnections { sender, found := c.senders[conn] if !found { sender = c.makeSender(conn) diff --git a/mesh/routes.go b/mesh/routes.go index d7787af9..675f1550 100644 --- a/mesh/routes.go +++ b/mesh/routes.go @@ -92,20 +92,24 @@ func (routes *Routes) BroadcastAll(name PeerName) []PeerName { // returns a higher proportion of neighbours than elsewhere. In // extremis, on peers with fewer than log2(n_peers) neighbours, all // neighbours are returned. -func (routes *Routes) RandomNeighbours(except PeerName) PeerNameSet { - res := make(PeerNameSet) +func (routes *Routes) RandomNeighbours(except PeerName) []PeerName { + destinations := make(PeerNameSet) routes.RLock() defer routes.RUnlock() count := int(math.Log2(float64(len(routes.unicastAll)))) // depends on go's random map iteration for _, dst := range routes.unicastAll { if dst != UnknownPeerName && dst != except { - res[dst] = void - if len(res) >= count { + destinations[dst] = void + if len(destinations) >= count { break } } } + res := make([]PeerName, 0, len(destinations)) + for dst := range destinations { + res = append(res, dst) + } return res }