From 7fc6d9adc274a06fdf2503e8d0e7e8677f439d4a Mon Sep 17 00:00:00 2001 From: Martin Dyring-Andersen Date: Wed, 13 Mar 2024 20:48:24 +0100 Subject: [PATCH] rpc: Add HTTP timeout, defaulting to 30 seconds (#1380) * rpc: Add HTTP timeout, default 30 seconds. Closes #1379. * Add .changelog entry --- .../improvements/1379-rpc-http-timeout.md | 2 ++ rpc/src/client/transport/http.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changelog/unreleased/improvements/1379-rpc-http-timeout.md diff --git a/.changelog/unreleased/improvements/1379-rpc-http-timeout.md b/.changelog/unreleased/improvements/1379-rpc-http-timeout.md new file mode 100644 index 000000000..54495f50c --- /dev/null +++ b/.changelog/unreleased/improvements/1379-rpc-http-timeout.md @@ -0,0 +1,2 @@ +- Add request timeout for the RPC HttpClient + ([\#1379](https://github.com/informalsystems/tendermint-rs/issues/1379)) \ No newline at end of file diff --git a/rpc/src/client/transport/http.rs b/rpc/src/client/transport/http.rs index 4d26f36aa..ed1cff71c 100644 --- a/rpc/src/client/transport/http.rs +++ b/rpc/src/client/transport/http.rs @@ -4,6 +4,7 @@ use core::str::FromStr; use async_trait::async_trait; use reqwest::{header, Proxy}; +use std::time::Duration; use tendermint::{block::Height, evidence::Evidence, Hash}; use tendermint_config::net; @@ -60,6 +61,7 @@ pub struct Builder { url: HttpClientUrl, compat: CompatMode, proxy_url: Option, + timeout: Duration, } impl Builder { @@ -82,9 +84,20 @@ impl Builder { self } + /// The timeout is applied from when the request starts connecting until + /// the response body has finished. + /// + /// The default is 30 seconds. + pub fn timeout(mut self, duration: Duration) -> Self { + self.timeout = duration; + self + } + /// Try to create a client with the options specified for this builder. pub fn build(self) -> Result { - let builder = reqwest::ClientBuilder::new().user_agent(USER_AGENT); + let builder = reqwest::ClientBuilder::new() + .user_agent(USER_AGENT) + .timeout(self.timeout); let inner = match self.proxy_url { None => builder.build().map_err(Error::http)?, Some(proxy_url) => { @@ -139,6 +152,7 @@ impl HttpClient { url, compat: Default::default(), proxy_url: None, + timeout: Duration::from_secs(30), } }