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

Don't send replies for SendMessage requests when using TCP #195

Merged
merged 1 commit into from
Mar 7, 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
19 changes: 9 additions & 10 deletions apis/rust/node/src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,18 @@ impl ControlChannel {
metadata: Metadata<'static>,
data: Vec<u8>,
) -> eyre::Result<()> {
let request = DaemonRequest::SendMessage {
output_id,
metadata,
data,
};
let reply = self
.channel
.request(&DaemonRequest::SendMessage {
output_id,
metadata,
data,
})
.wrap_err("failed to send SendEmptyMessage request to dora-daemon")?;
.request(&request)
.wrap_err("failed to send SendMessage request to dora-daemon")?;
match reply {
dora_core::daemon_messages::DaemonReply::Result(result) => {
result.map_err(|err| eyre!(err))
}
other => bail!("unexpected SendEmptyMessage reply: {other:?}"),
dora_core::daemon_messages::DaemonReply::Empty => Ok(()),
other => bail!("unexpected SendMessage reply: {other:?}"),
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions apis/rust/node/src/daemon/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ use std::{

pub fn request(connection: &mut TcpStream, request: &DaemonRequest) -> eyre::Result<DaemonReply> {
send_message(connection, request)?;
receive_reply(connection)
.and_then(|reply| reply.ok_or_else(|| eyre!("server disconnected unexpectedly")))
if request.expects_tcp_reply() {
receive_reply(connection)
.and_then(|reply| reply.ok_or_else(|| eyre!("server disconnected unexpectedly")))
} else {
Ok(DaemonReply::Empty)
}
}

fn send_message(connection: &mut TcpStream, message: &DaemonRequest) -> eyre::Result<()> {
Expand Down
5 changes: 4 additions & 1 deletion binaries/daemon/src/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ where
.send_daemon_event(event)
.await
.map_err(|_| "failed to receive send_empty_message reply".to_owned());
self.send_reply(DaemonReply::Result(result))
if let Err(err) = result {
tracing::warn!("{err:?}");
}
self.send_reply(DaemonReply::Empty)
.await
.wrap_err("failed to send SendEmptyMessage reply")?;
}
Expand Down
4 changes: 4 additions & 0 deletions binaries/daemon/src/listener/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl super::Connection for TcpConnection {
}

async fn send_reply(&mut self, message: DaemonReply) -> eyre::Result<()> {
if matches!(message, DaemonReply::Empty) {
// don't send empty replies
return Ok(());
}
let serialized =
bincode::serialize(&message).wrap_err("failed to serialize DaemonReply")?;
tcp_send(&mut self.0, &serialized)
Expand Down
11 changes: 11 additions & 0 deletions libraries/core/src/daemon_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@ pub enum DaemonRequest {
},
}

impl DaemonRequest {
pub fn expects_tcp_reply(&self) -> bool {
#[allow(clippy::match_like_matches_macro)]
match self {
DaemonRequest::SendMessage { .. } => false,
_ => true,
}
}
}

type SharedMemoryId = String;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum DaemonReply {
Result(Result<(), String>),
PreparedMessage { shared_memory_id: SharedMemoryId },
NextEvents(Vec<NodeEvent>),
Empty,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
Expand Down