Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multi-thread executor to execute network futures #5

Merged
merged 1 commit into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions network/src/ckb_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ impl CKBService {
match network.ckb_protocol_connec(&peer_id, protocol_id, endpoint, addresses.clone()) {
Ok(protocol_connec) => protocol_connec,
Err(err) => {
return Box::new(future::err(IoError::new(IoErrorKind::Other, err)))
as Box<Future<Item = (), Error = IoError> + Send>
return Box::new(future::err(IoError::new(
IoErrorKind::Other,
format!("handle ckb_protocol connection error: {}", err),
))) as Box<Future<Item = (), Error = IoError> + Send>
}
};
if protocol_connec.state() == UniqueConnecState::Full {
Expand Down Expand Up @@ -133,13 +135,19 @@ impl CKBService {
peer_store.report(&peer_id, Behaviour::Connect);
peer_store.report_status(&peer_id, Status::Connected);
}
protocol_handler.connected(
Box::new(DefaultCKBProtocolContext::new(
Arc::clone(&network),
protocol_id,
)),
peer_index,
);
{
let handle_connected = future::lazy(move || {
protocol_handler.connected(
Box::new(DefaultCKBProtocolContext::new(
Arc::clone(&network),
protocol_id,
)),
peer_index,
);
Ok(())
});
tokio::spawn(handle_connected);
}
Box::new(protocol_future) as Box<_>
}
}
Expand Down
2 changes: 2 additions & 0 deletions network/src/discovery_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ impl DiscoveryService {
//};

let handling_future = Box::new(
// why use loop_fn?????????????
// does client disconnect after discovery?
future::loop_fn(kademlia_stream, {
let peer_id = peer_id.clone();
let kad_system = Arc::clone(&self.kad_system);
Expand Down
6 changes: 4 additions & 2 deletions network/src/network_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::boxed::Box;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::sync::Arc;
use std::thread;
use tokio::runtime::current_thread;
use tokio::runtime;

pub struct NetworkService {
network: Arc<Network>,
Expand Down Expand Up @@ -82,7 +82,9 @@ impl NetworkService {
let network_future =
Network::build_network_future(network, &config, close_rx).unwrap();
init_tx.send(()).unwrap();
match current_thread::block_on_all(network_future) {
// here we use default config
let network_runtime = runtime::Runtime::new().unwrap();
match network_runtime.block_on_all(network_future) {
Ok(_) => info!(target: "network", "network service exit"),
Err(err) => panic!("network service exit unexpected {}", err),
}
Expand Down