fix(router): retry messages when server is not ready (#1651)

* fix(router): retry messages when server is not ready

* style(fmt): rustfmt
This commit is contained in:
Aram Drevekenin 2022-08-11 14:35:15 +02:00 committed by GitHub
parent dba5dcbd83
commit e910db9bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -445,6 +445,22 @@ fn route_action(
should_break
}
macro_rules! send_to_screen_or_retry_queue {
($rlocked_sessions:expr, $message:expr, $instruction: expr, $retry_queue:expr) => {{
match $rlocked_sessions.as_ref() {
Some(session_metadata) => {
session_metadata.senders.send_to_screen($message).unwrap();
},
None => {
log::warn!("Server not ready, trying to place instruction in retry queue...");
if let Some(retry_queue) = $retry_queue.as_mut() {
retry_queue.push($instruction);
}
},
}
}};
}
pub(crate) fn route_thread_main(
session_data: Arc<RwLock<Option<SessionMetaData>>>,
session_state: Arc<RwLock<SessionState>>,
@ -453,12 +469,16 @@ pub(crate) fn route_thread_main(
mut receiver: IpcReceiverWithContext<ClientToServerMsg>,
client_id: ClientId,
) {
loop {
let mut retry_queue = vec![];
'route_loop: loop {
match receiver.recv() {
Some((instruction, err_ctx)) => {
err_ctx.update_thread_ctx();
let rlocked_sessions = session_data.read().unwrap();
let handle_instruction = |instruction: ClientToServerMsg,
mut retry_queue: Option<&mut Vec<ClientToServerMsg>>|
-> bool {
let mut should_break = false;
match instruction {
ClientToServerMsg::Action(action, maybe_client_id) => {
let client_id = maybe_client_id.unwrap_or(client_id);
@ -476,7 +496,7 @@ pub(crate) fn route_thread_main(
&to_server,
client_id,
) {
break;
should_break = true;
}
}
},
@ -498,14 +518,13 @@ pub(crate) fn route_thread_main(
.unwrap();
},
ClientToServerMsg::TerminalPixelDimensions(pixel_dimensions) => {
rlocked_sessions
.as_ref()
.unwrap()
.senders
.send_to_screen(ScreenInstruction::TerminalPixelDimensions(
pixel_dimensions,
))
.unwrap();
// this is experimental, please be cautious implementing this elsewhere
send_to_screen_or_retry_queue!(
rlocked_sessions,
ScreenInstruction::TerminalPixelDimensions(pixel_dimensions),
instruction,
retry_queue
);
},
ClientToServerMsg::BackgroundColor(background_color_instruction) => {
rlocked_sessions
@ -563,23 +582,36 @@ pub(crate) fn route_thread_main(
// we don't unwrap this because we don't really care if there's an error here (eg.
// if the main server thread exited before this router thread did)
let _ = to_server.send(ServerInstruction::RemoveClient(client_id));
break;
return true;
},
ClientToServerMsg::KillSession => {
to_server.send(ServerInstruction::KillSession).unwrap();
},
ClientToServerMsg::ConnStatus => {
let _ = to_server.send(ServerInstruction::ConnStatus(client_id));
break;
should_break = true;
},
ClientToServerMsg::DetachSession(client_id) => {
let _ = to_server.send(ServerInstruction::DetachSession(client_id));
break;
should_break = true;
},
ClientToServerMsg::ListClients => {
let _ = to_server.send(ServerInstruction::ActiveClients(client_id));
},
}
should_break
};
for instruction_to_retry in retry_queue.drain(..) {
log::warn!("Server ready, retrying sending instruction.");
let should_break = handle_instruction(instruction_to_retry, None);
if should_break {
break 'route_loop;
}
}
let should_break = handle_instruction(instruction, Some(&mut retry_queue));
if should_break {
break 'route_loop;
}
},
None => {
log::error!("Received empty message from client");