DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
21.65k stars 833 forks source link

Server Function Timeout #3110

Open elbaro opened 1 month ago

elbaro commented 1 month ago

Feature Request

#[server(timeout_secs=1.5)]  // FeatureRequest 1: timeout attribute
async fn remote_call() -> Result<Outcome, ServerFnError> {
    // slow operation
}

match remote_call().await {
  Err(ServerFnError::Timeout) => ...
  ...
}

// // FeatureRequest 2: with_timeout combinator
match remote_call().with_timeout(Duration::..).await {
  ...
}

Setting a timeout from a caller side with gloo_timers has disadvantages:

Implement Suggestion

    use gloo_timers::future::TimeoutFuture;

    match select(rpc_call, TimeoutFuture::new(ms)).await {
        Either::Left((response, timer)) => {
            drop(timer);
            return Ok(response);
        }
        Either::Right((rpc_call, _)) => {
            rpc_call.cancel();
            return Err(ServerFnError::Timeout)
        }
    }
ryo33 commented 1 month ago

It's already achievable by attaching tower middleware to a server function. It's verbose than your suggested solution but it's more modular.

https://docs.rs/dioxus/0.6.0-alpha.3/dioxus/prelude/attr.server.html#adding-layers-to-server-functions

Edit:

I've noticed that middleware only work server side but not client side.

elbaro commented 1 month ago

Yes, the main motivation of this feature request is to reliably notify app users of timeout on network failures.