Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
introduce backpressure between network and protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Jan 11, 2019
1 parent 4616698 commit 029c255
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core/network-libp2p/src/service_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum NetworkMsg {
RemoveReservedPeer(PeerId),
AcceptUnreservedPeers,
DenyUnreservedPeers,
SkipNetworkPoll,
}

pub struct NetworkPort {
Expand Down Expand Up @@ -285,7 +286,10 @@ pub enum ServiceEvent {
DisconnectNode {
/// Index of the node.
node_index: NodeIndex,
}
},

/// Skipped a network poll, while still handling incoming protocol messages.
SkippedNetworkPoll,
}

/// Network service. Must be polled regularly in order for the networking to work.
Expand Down Expand Up @@ -1039,6 +1043,9 @@ impl Service {
};
for msg in messages {
match msg {
// Injecting a ServiceEvent results in not polling the network in the current poll.
// If protocol is too busy, ensure at least one event is injected.
NetworkMsg::SkipNetworkPoll => self.injected_events.push(ServiceEvent::SkippedNetworkPoll),
NetworkMsg::PeerIds(node_idxs, sender) => {
let reply = node_idxs.into_iter().map(|idx| {
(idx, self.peer_id_of_node(idx).map(|p| p.clone()))
Expand Down
11 changes: 11 additions & 0 deletions core/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ use tokio::runtime::Runtime;

pub use network_libp2p::NetworkMsg;

// Number of messages in the protocol queue after which we start throttling network requests.
const PROTOCOL_HIGH_WATERMARK: usize = 10;

/// Type that represents fetch completion future.
pub type FetchFuture = oneshot::Receiver<Vec<u8>>;

Expand Down Expand Up @@ -352,6 +355,9 @@ fn run_thread<B: BlockT + 'static>(
// The network service produces events about what happens on the network. Let's process them.
stream::poll_fn(move || network_service.lock().poll()).for_each(move |event| {
match event {
NetworkServiceEvent::SkippedNetworkPoll => {
// Network skipped a network poll, while still remaining responsive to messages from protocol.
}
NetworkServiceEvent::DisconnectNode { node_index } => {
let _ = protocol_sender.send(ProtocolMsg::PeerDisconnected(node_index));
}
Expand Down Expand Up @@ -394,6 +400,11 @@ fn run_thread<B: BlockT + 'static>(
};
}
};
// If protocol is too busy, tell network to skip one polling of the network,
// while remaining responsive to incoming messages from protocol
if protocol_sender.len() > PROTOCOL_HIGH_WATERMARK {
let _ = network_sender.send(NetworkMsg::SkipNetworkPoll);
}
Ok(())
})
}

0 comments on commit 029c255

Please sign in to comment.