Skip to content

Commit

Permalink
RSDK-4447: Add dial timeout (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: hexbabe <[email protected]>
  • Loading branch information
hexbabe and hexbabe authored Sep 26, 2023
1 parent 39e453c commit ec2921a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/ffi/dial_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use http::uri::Uri;
use std::{ptr, time::Duration};
use tokio::runtime::Runtime;
use tokio::sync::oneshot;
use tokio::time::timeout;
use tracing::Level;

use crate::rpc::dial::{
Expand Down Expand Up @@ -103,6 +104,7 @@ fn dial_with_cred(
let c = if allow_insec { c.allow_downgrade() } else { c };
Ok(c)
}

/// Returns a path to a UDS proxy to a robot
/// # Safety
///
Expand All @@ -114,6 +116,7 @@ fn dial_with_cred(
/// * `c_type` a C-style string representing the type of robot's secret you want to use, set to NULL if you don't need authentication
/// * `c_payload` a C-style string that is the robot's secret, set to NULL if you don't need authentication
/// * `c_allow_insecure` a bool, set to true when allowing insecure connection to your robot
/// * `c_timeout` a float, set how many seconds we should try to dial before timing out
/// * `rt_ptr` a pointer to a rust runtime previously obtained with init_rust_runtime
#[no_mangle]
pub unsafe extern "C" fn dial(
Expand All @@ -122,6 +125,7 @@ pub unsafe extern "C" fn dial(
c_type: *const c_char,
c_payload: *const c_char,
c_allow_insec: bool,
c_timeout: f32,
rt_ptr: Option<&mut DialFfi>,
) -> *mut c_char {
let uri = {
Expand Down Expand Up @@ -198,11 +202,14 @@ pub unsafe extern "C" fn dial(
}
}
};
let timeout_duration = Duration::from_secs_f32(c_timeout);

let (server, channel) = match runtime.block_on(async move {
let channel = match (r#type, payload) {
(Some(t), Some(p)) => {
dial_with_cred(
let res = timeout(
timeout_duration,
dial_with_cred(
uri_str,
entity_opt,
t.to_str()?,
Expand All @@ -211,11 +218,15 @@ pub unsafe extern "C" fn dial(
disable_webrtc,
)?
.connect()
.await
)
.await?;
res

Check warning on line 223 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

returning the result of a `let` binding from a block

warning: returning the result of a `let` binding from a block --> src/ffi/dial_ffi.rs:223:17 | 210 | / let res = timeout( 211 | | timeout_duration, 212 | | dial_with_cred( 213 | | uri_str, ... | 221 | | ) 222 | | .await?; | |________________________- unnecessary `let` binding 223 | res | ^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return = note: `#[warn(clippy::let_and_return)]` on by default help: return the expression directly | 210 ~ 211 ~ timeout( 212 + timeout_duration, 213 + dial_with_cred( 214 + uri_str, 215 + entity_opt, 216 + t.to_str()?, 217 + p.to_str()?, 218 + allow_insec, 219 + disable_webrtc, 220 + )? 221 + .connect() 222 + ) 223 + .await? |
}
(None, None) => {
let c = dial_without_cred(uri_str, allow_insec, disable_webrtc)?;
c.connect().await
let res = timeout(
timeout_duration,
dial_without_cred(uri_str, allow_insec, disable_webrtc)?.connect()).await?;
res

Check warning on line 229 in src/ffi/dial_ffi.rs

View workflow job for this annotation

GitHub Actions / clippy

returning the result of a `let` binding from a block

warning: returning the result of a `let` binding from a block --> src/ffi/dial_ffi.rs:229:17 | 226 | / let res = timeout( 227 | | timeout_duration, 228 | | dial_without_cred(uri_str, allow_insec, disable_webrtc)?.connect()).await?; | |_______________________________________________________________________________________________- unnecessary `let` binding 229 | res | ^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return help: return the expression directly | 226 ~ 227 ~ timeout( 228 + timeout_duration, 229 + dial_without_cred(uri_str, allow_insec, disable_webrtc)?.connect()).await? |
}
(None, Some(_)) => Err(anyhow::anyhow!("Error missing credential: type")),
(Some(_), None) => Err(anyhow::anyhow!("Error missing credential: payload")),
Expand All @@ -241,7 +252,7 @@ pub unsafe extern "C" fn dial(
}) {
Ok(s) => s,
Err(e) => {
log::error!("Error building GRPC proxy reason : {e:?}");
log::error!("Error building GRPC proxy reason : {}", e);
return ptr::null_mut();
}
};
Expand Down

0 comments on commit ec2921a

Please sign in to comment.