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 d6d2135d3..a0349d3f5 100644 --- a/rpc/src/client/transport/http.rs +++ b/rpc/src/client/transport/http.rs @@ -7,6 +7,7 @@ use core::{ use async_trait::async_trait; use reqwest::{header, Proxy}; +use std::time::Duration; use tendermint::{block::Height, evidence::Evidence, Hash}; use tendermint_config::net; @@ -63,6 +64,7 @@ pub struct Builder { url: HttpClientUrl, compat: CompatMode, proxy_url: Option, + timeout: Duration, } impl Builder { @@ -85,9 +87,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) => { @@ -142,6 +155,7 @@ impl HttpClient { url, compat: Default::default(), proxy_url: None, + timeout: Duration::from_secs(30), } }