Backport of #7844 and #7917 to beta (#7940)

* Randomize the peer we dispatch to

* Fix a division by zero in light client RPC handler
This commit is contained in:
Pierre Krieger 2018-02-19 13:03:49 +01:00 committed by Rando
parent deecf8927c
commit 6a29fea23c

View File

@ -18,6 +18,7 @@
//! The request service is implemented using Futures. Higher level request handlers
//! will take the raw data received here and extract meaningful results from it.
use std::cmp;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
@ -28,6 +29,7 @@ use futures::{Async, Poll, Future};
use futures::sync::oneshot::{self, Sender, Receiver, Canceled};
use network::PeerId;
use parking_lot::{RwLock, Mutex};
use rand;
use net::{
self, Handler, PeerStatus, Status, Capabilities,
@ -389,7 +391,10 @@ impl OnDemand {
true => None,
})
.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>() % cmp::max(num_peers, 1);
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) {