Remove blocking operations in SyncOracle implementation (#1852)

* remove blocking operations in SyncOracle implementation

* docs

* docs
This commit is contained in:
Gregory Terzian
2019-02-25 21:00:13 +08:00
committed by Gav Wood
parent 733ce7d616
commit 077ed00951
5 changed files with 178 additions and 42 deletions
+14 -14
View File
@@ -16,6 +16,7 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{io, thread};
use log::{warn, debug, error, trace, info};
use futures::{Async, Future, Stream, stream, sync::oneshot};
@@ -117,6 +118,10 @@ impl<B: BlockT, S: NetworkSpecialization<B>> Link<B> for NetworkLink<B, S> {
/// Substrate network service. Handles network IO and manages connectivity.
pub struct Service<B: BlockT + 'static, S: NetworkSpecialization<B>> {
// Are we connected to any peer?
is_offline: Arc<AtomicBool>,
// Are we actively catching up with the chain?
is_major_syncing: Arc<AtomicBool>,
/// Network service
network: Arc<Mutex<NetworkService<Message<B>>>>,
/// Protocol sender
@@ -135,7 +140,12 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>> Service<B, S> {
import_queue: Box<ImportQueue<B>>,
) -> Result<(Arc<Service<B, S>>, NetworkChan<B>), Error> {
let (network_chan, network_port) = network_channel(protocol_id);
// Start in off-line mode, since we're not connected to any nodes yet.
let is_offline = Arc::new(AtomicBool::new(true));
let is_major_syncing = Arc::new(AtomicBool::new(false));
let protocol_sender = Protocol::new(
is_offline.clone(),
is_major_syncing.clone(),
network_chan.clone(),
params.config,
params.chain,
@@ -154,6 +164,8 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>> Service<B, S> {
)?;
let service = Arc::new(Service {
is_offline,
is_major_syncing,
network,
protocol_sender: protocol_sender.clone(),
bg_thread: Some(thread),
@@ -244,22 +256,10 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>> Service<B, S> {
impl<B: BlockT + 'static, S: NetworkSpecialization<B>> ::consensus::SyncOracle for Service<B, S> {
fn is_major_syncing(&self) -> bool {
let (sender, port) = channel::unbounded();
let _ = self
.protocol_sender
.send(ProtocolMsg::IsMajorSyncing(sender));
port.recv().expect("1. Protocol keeps handling messages until all senders are dropped,
or the ProtocolMsg::Stop message is received,
2 Service keeps a sender to protocol, and the ProtocolMsg::Stop is never sent.")
self.is_major_syncing.load(Ordering::Relaxed)
}
fn is_offline(&self) -> bool {
let (sender, port) = channel::unbounded();
let _ = self
.protocol_sender
.send(ProtocolMsg::IsOffline(sender));
port.recv().expect("1. Protocol keeps handling messages until all senders are dropped,
or the ProtocolMsg::Stop message is received,
2 Service keeps a sender to protocol, and the ProtocolMsg::Stop is never sent.")
self.is_offline.load(Ordering::Relaxed)
}
}