Randomize the peer we dispatch to (#7844)

This commit is contained in:
Pierre Krieger 2018-02-08 21:36:46 +01:00 committed by Afri Schoedon
parent a59f6d9bd2
commit 2c60a53fef

View File

@ -28,6 +28,7 @@ use futures::{Poll, Future};
use futures::sync::oneshot::{self, Receiver, Canceled};
use network::PeerId;
use parking_lot::{RwLock, Mutex};
use rand;
use net::{
self, Handler, PeerStatus, Status, Capabilities,
@ -363,7 +364,10 @@ impl OnDemand {
*pending = ::std::mem::replace(&mut *pending, Vec::new()).into_iter()
.filter(|pending| !pending.sender.is_canceled())
.filter_map(|pending| {
for (peer_id, peer) in peers.iter() { // .shuffle?
// the peer we dispatch to is chosen randomly
let num_peers = peers.len();
let rng = rand::random::<usize>() % num_peers;
for (peer_id, peer) in peers.iter().chain(peers.iter()).skip(rng).take(num_peers) {
// TODO: see which requests can be answered by the cache?
if !peer.can_fulfill(&pending.required_capabilities) {