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 blocking_send instead of the async send #3855

Merged
merged 2 commits into from
Apr 24, 2023
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
6 changes: 1 addition & 5 deletions massa-bootstrap/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,6 @@ fn manage_bootstrap(
massa_trace!("bootstrap.lib.manage_bootstrap", {});
let read_error_timeout: Duration = bootstrap_config.read_error_timeout.into();

// TODO: make network_command_sender non-async and remove both the variable and the method
let rt_hack = massa_network_exports::make_runtime();

let Some(hs_timeout) = step_timeout_duration(&deadline, &bootstrap_config.read_timeout.to_duration()) else {
return Err(BootstrapError::Interupted("insufficient time left to begin handshake".to_string()));
};
Expand Down Expand Up @@ -769,8 +766,7 @@ fn manage_bootstrap(
server.send_msg(
write_timeout,
BootstrapServerMessage::BootstrapPeers {
peers: rt_hack
.block_on(network_command_sender.get_bootstrap_peers())?,
peers: network_command_sender.sync_get_bootstrap_peers()?,
},
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion massa-bootstrap/src/tests/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn test_bootstrap_server() {
let mut mocked1 = NetworkCommandSender::new();
let mut mocked2 = NetworkCommandSender::new();
mocked2
.expect_get_bootstrap_peers()
.expect_sync_get_bootstrap_peers()
.times(1)
.returning(|| Ok(get_peers()));

Expand Down
6 changes: 2 additions & 4 deletions massa-network-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ pub use error::{HandshakeErrorType, NetworkConnectionErrorType, NetworkError};
pub use establisher::{Establisher, Listener, ReadHalf, WriteHalf};

#[cfg(any(test, feature = "testing"))]
pub use network_controller::MockNetworkCommandSender;
pub use network_controller::{make_runtime, MockNetworkCommandSender};

pub use network_controller::{
make_runtime, NetworkCommandSender, NetworkEventReceiver, NetworkManager,
};
pub use network_controller::{NetworkCommandSender, NetworkEventReceiver, NetworkManager};
pub use peers::{
BootstrapPeers, BootstrapPeersDeserializer, BootstrapPeersSerializer, ConnectionCount, Peer,
PeerInfo, PeerType, Peers,
Expand Down
13 changes: 7 additions & 6 deletions massa-network-exports/src/network_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,14 @@ impl NetworkCommandSender {
}

/// Send the order to get bootstrap peers.
pub async fn get_bootstrap_peers(&self) -> Result<BootstrapPeers, NetworkError> {
pub fn sync_get_bootstrap_peers(&self) -> Result<BootstrapPeers, NetworkError> {
let (response_tx, response_rx) = oneshot::channel::<BootstrapPeers>();
self.0
.send(NetworkCommand::GetBootstrapPeers(response_tx))
.await
.blocking_send(NetworkCommand::GetBootstrapPeers(response_tx))
.map_err(|_| {
NetworkError::ChannelError("could not send GetBootstrapPeers command".into())
})?;
response_rx.await.map_err(|_| {
response_rx.blocking_recv().map_err(|_| {
NetworkError::ChannelError("could not send GetBootstrapPeers response upstream".into())
})
}
Expand Down Expand Up @@ -323,8 +322,10 @@ impl NetworkManager {
}
}

/// Used by the bootstrap server to run async tasks, allowing the bootstrap module to
/// remove the tokio dependency.
#[cfg(any(test, feature = "testing"))]
/// The bootstrap server function `get_state` has to be async due to main.rs `tokio::select!` usage to
/// cancel on `ctrl-c`
/// If get state can be cancelled with ctrl-c without an async context, this can be removed
pub fn make_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down