From 20b442841344bae523f2009152b2eab6aeedc923 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Thu, 25 Apr 2024 10:50:04 +0200 Subject: [PATCH] Port "rpc: Add HTTP timeout" (tendermint-rs#1380) (#26) --- .../improvements/1379-rpc-http-timeout.md | 3 +++ rpc/src/client/transport/http.rs | 16 +++++++++++++++- 2 files changed, 18 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 00000000..dfd90433 --- /dev/null +++ b/.changelog/unreleased/improvements/1379-rpc-http-timeout.md @@ -0,0 +1,3 @@ +- Allow specifying a request timeout for the RPC `HttpClient` ([#20](https://github.com/cometbft/cometbft-rs/issues/20)) + The `http::Builder` struct now provides a `.timeout(Duration)` method to specify the request timeout. + If not specified, the default value is 30 seconds diff --git a/rpc/src/client/transport/http.rs b/rpc/src/client/transport/http.rs index f74d63a1..814555d0 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 cometbft::{block::Height, evidence::Evidence, Hash}; use cometbft_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), } }