Skip to content

Commit

Permalink
Connection timeout after successful timeout fix (#171)
Browse files Browse the repository at this point in the history
* Fixing connection timeout after while_timeout was refactored wrong making it timeout even after succesful connection

Signed-off-by: Apokalip <simeon@manta.network>

* format

Signed-off-by: Apokalip <simeon@manta.network>

* Fix comment

Signed-off-by: Apokalip <simeon@manta.network>

Signed-off-by: Apokalip <simeon@manta.network>
  • Loading branch information
Apokalip authored Sep 15, 2022
1 parent d0df86d commit 4a41dab
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions ui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ impl AppState {
/// Application State
pub static APP_STATE: AppState = AppState::new();

/// Repeatedly executes `f` until the `timeout` is reached calling `exit` to return from the
/// function.
#[inline]
pub fn while_timeout<F, E, T>(timeout: Duration, mut f: F, exit: E) -> T
where
F: FnMut(),
E: FnOnce(Instant, Duration) -> T,
{
let time_start = Instant::now();
loop {
f();
if time_start.elapsed() >= timeout {
return exit(time_start, timeout);
/// While with a timeout
/// Loop over body code block until specified time elapses and exits executing a given code block
/// Needs to be a macro to be able to break early in the main loop body code block
/// i.e. loop over waiting to connect and then break the loop, or timeout with Error message after
/// a specified time.
macro_rules! while_w_timeout{
($body:block, $timeout_d:expr, $failure:block) => {{
let time_start = Instant::now();
let timeout = Duration::from_millis($timeout_d);
loop {
$body
if time_start.elapsed() >= timeout {
$failure
}
}
}
}};
}

/// User
pub struct User {
/// Main Window
Expand Down Expand Up @@ -177,23 +177,20 @@ impl Authorizer for User {
fn setup<'s>(&'s mut self, setup: &'s Setup) -> UnitFuture<'s> {
let window = self.window.clone();
Box::pin(async move {
while_timeout(
Duration::from_millis(5000),
move || {
while_w_timeout!(
{
if APP_STATE.get_ui_connected() {
return;
break;
}
window
.emit("connect", setup)
.expect("The `connect` command failed to be emitted to the window.");
},
move |time_start, timeout| {
panic!(
"Connection attempt timed-out! Started: {:?} with {:?} timeout.",
time_start, timeout
);
},
)
5000,
{
panic!("Connection attempt timedout!");
}
);
})
}

Expand Down

1 comment on commit 4a41dab

@vercel
Copy link

@vercel vercel bot commented on 4a41dab Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.