clementine_core::utils

Function timed_request

Source
pub async fn timed_request<F, T>(
    duration: Duration,
    description: &str,
    future: F,
) -> Result<T, BridgeError>
where F: Future<Output = Result<T, BridgeError>>,
Expand description

Wraps a future with a timeout, returning a Status::deadline_exceeded gRPC error if the future does not complete within the specified duration.

This is useful for enforcing timeouts on individual asynchronous operations, especially those involving network requests, to prevent them from hanging indefinitely.

§Arguments

  • duration: The maximum Duration to wait for the future to complete.
  • description: A string slice describing the operation, used in the timeout error message.
  • future: The Future to execute. The future should return a Result<T, BridgeError>.

§Returns

Returns Ok(T) if the future completes successfully within the time limit. Returns Err(BridgeError) if the future returns an error or if it times out. A timeout results in a BridgeError that wraps a tonic::Status::deadline_exceeded.