dioxus
dioxus copied to clipboard
Server Function Timeout
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:
- The call site becomes very verbose
- the internal POST request for RPC cannot be explicitly cancelled on timeout
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)
}
}
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.
Yes, the main motivation of this feature request is to reliably notify app users of timeout on network failures.