Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: Add HTTP timeout, default 30 seconds. #1380

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/1379-rpc-http-timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add request timeout for the RPC HttpClient
([\#1379](https://github.com/informalsystems/tendermint-rs/issues/1379))
16 changes: 15 additions & 1 deletion rpc/src/client/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct Builder {
url: HttpClientUrl,
compat: CompatMode,
proxy_url: Option<HttpClientUrl>,
timeout: Duration,
}

impl Builder {
Expand All @@ -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<HttpClient, Error> {
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) => {
Expand Down Expand Up @@ -142,6 +155,7 @@ impl HttpClient {
url,
compat: Default::default(),
proxy_url: None,
timeout: Duration::from_secs(30),
}
}

Expand Down