Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/wasm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,19 @@ impl Client {
}
}

fn merge_timeout(&self, req: &mut Request) {
if let Some(timeout) = self.config.timeout {
// If a timeout was not already set on the request, set the client's default timeout.
req.timeout_mut().get_or_insert(timeout);
}
}

pub(super) fn execute_request(
&self,
mut req: Request,
) -> impl Future<Output = crate::Result<Response>> {
self.merge_headers(&mut req);
self.merge_timeout(&mut req);
fetch(req)
}
}
Expand Down Expand Up @@ -320,6 +328,17 @@ impl ClientBuilder {
}
self
}

/// Enables a total request timeout.
///
/// The timeout is applied from when the request starts connecting until the
/// response body has finished. Also considered a total deadline.
///
/// Default is no timeout.
pub fn timeout(mut self, timeout: Duration) -> ClientBuilder {
self.config.timeout = Some(timeout);
self
}
}

impl Default for ClientBuilder {
Expand All @@ -331,6 +350,7 @@ impl Default for ClientBuilder {
#[derive(Debug)]
struct Config {
headers: HeaderMap,
timeout: Option<Duration>,
error: Option<crate::Error>,
}

Expand Down