Skip to content

Commit

Permalink
refactor: handle_action method
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Sep 8, 2023
1 parent c9c4605 commit 2862a81
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions uplink/src/base/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,44 +207,7 @@ impl Bridge {
select! {
action = self.actions_rx.recv_async() => {
let action = action?;
let action_id = action.action_id.clone();
// Reactlabs setup processes logs generated by uplink
info!("Received action: {:?}", action);

if let Some(current_action) = &self.current_action {
if action.name != TUNSHELL_ACTION {
warn!("Another action is currently occupying uplink; action_id = {}", current_action.id);
self.forward_action_error(action, Error::Busy).await;
continue
}
}

// NOTE: Don't do any blocking operations here
// TODO: Remove blocking here. Audit all blocking functions here
let error = match self.try_route_action(action.clone()) {
Ok(_) => {
let response = ActionResponse::progress(&action_id, "Received", 0);
self.forward_action_response(response).await;
continue;
}
Err(e) => e,
};
// Ignore sending failure status to backend. This makes
// backend retry action.
//
// TODO: Do we need this? Shouldn't backend have an easy way to
// retry failed actions in bulk?
if self.config.ignore_actions_if_no_clients {
error!("No clients connected, ignoring action = {:?}", action_id);
self.current_action = None;
continue;
}

error!("Failed to route action to app. Error = {:?}", error);
self.forward_action_error(action, error).await;

// Remove action because it couldn't be routed
self.clear_current_action()
self.handle_action(action).await;
}
event = self.bridge_rx.recv_async() => {
let event = event?;
Expand Down Expand Up @@ -293,6 +256,50 @@ impl Bridge {
}
}

async fn handle_action(&mut self, action: Action) {
let action_id = action.action_id.clone();
// Reactlabs setup processes logs generated by uplink
info!("Received action: {:?}", action);

if let Some(current_action) = &self.current_action {
if action.name != TUNSHELL_ACTION {
warn!(
"Another action is currently occupying uplink; action_id = {}",
current_action.id
);
self.forward_action_error(action, Error::Busy).await;
return;
}
}

// NOTE: Don't do any blocking operations here
// TODO: Remove blocking here. Audit all blocking functions here
let error = match self.try_route_action(action.clone()) {
Ok(_) => {
let response = ActionResponse::progress(&action_id, "Received", 0);
self.forward_action_response(response).await;
return;
}
Err(e) => e,
};

// Remove action because it couldn't be routed
self.clear_current_action();

// Ignore sending failure status to backend. This makes
// backend retry action.
//
// TODO: Do we need this? Shouldn't backend have an easy way to
// retry failed actions in bulk?
if self.config.ignore_actions_if_no_clients {
error!("No clients connected, ignoring action = {:?}", action_id);
return;
}

error!("Failed to route action to app. Error = {:?}", error);
self.forward_action_error(action, error).await;
}

/// Save current action information in persistence
fn save_current_action(&mut self) -> Result<(), Error> {
let current_action = match self.current_action.take() {
Expand Down

0 comments on commit 2862a81

Please sign in to comment.