From 28c2b619335643272ed59f78a764e37f820f6de8 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Fri, 1 Dec 2023 15:36:14 +0000 Subject: [PATCH] make Monero HTTP RPC timeout configurable. --- coins/monero/src/rpc/http.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/coins/monero/src/rpc/http.rs b/coins/monero/src/rpc/http.rs index 27088638..9af7a087 100644 --- a/coins/monero/src/rpc/http.rs +++ b/coins/monero/src/rpc/http.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, io::Read}; +use std::{sync::Arc, io::Read, time::Duration}; use async_trait::async_trait; @@ -12,6 +12,8 @@ use simple_request::{ use crate::rpc::{RpcError, RpcConnection, Rpc}; +const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30); + #[derive(Clone, Debug)] enum Authentication { // If unauthenticated, use a single client @@ -34,6 +36,7 @@ enum Authentication { pub struct HttpRpc { authentication: Authentication, url: String, + request_timeout: Duration, } impl HttpRpc { @@ -57,7 +60,18 @@ impl HttpRpc { /// /// A daemon requiring authentication can be used via including the username and password in the /// URL. - pub async fn new(mut url: String) -> Result, RpcError> { + pub async fn new(url: String) -> Result, RpcError> { + Self::new_custom_timeout(url, DEFAULT_TIMEOUT).await + } + + /// Create a new HTTP(S) RPC connection with a custom timeout. + /// + /// A daemon requiring authentication can be used via including the username and password in the + /// URL. + pub async fn new_custom_timeout( + mut url: String, + request_timeout: Duration, + ) -> Result, RpcError> { let authentication = if url.contains('@') { // Parse out the username and password let url_clone = url; @@ -105,7 +119,7 @@ impl HttpRpc { Authentication::Unauthenticated(Client::with_connection_pool()) }; - Ok(Rpc(HttpRpc { authentication, url })) + Ok(Rpc(HttpRpc { authentication, url, request_timeout })) } } @@ -265,8 +279,7 @@ impl HttpRpc { #[async_trait] impl RpcConnection for HttpRpc { async fn post(&self, route: &str, body: Vec) -> Result, RpcError> { - // TODO: Make this timeout configurable - tokio::time::timeout(core::time::Duration::from_secs(30), self.inner_post(route, body)) + tokio::time::timeout(self.request_timeout, self.inner_post(route, body)) .await .map_err(|e| RpcError::ConnectionError(format!("{e:?}")))? }