make Monero HTTP RPC timeout configurable.

This commit is contained in:
Boog900 2023-12-01 15:36:14 +00:00 committed by Luke Parker
parent cb1ef0d71f
commit 28c2b61933

View file

@ -1,4 +1,4 @@
use std::{sync::Arc, io::Read}; use std::{sync::Arc, io::Read, time::Duration};
use async_trait::async_trait; use async_trait::async_trait;
@ -12,6 +12,8 @@ use simple_request::{
use crate::rpc::{RpcError, RpcConnection, Rpc}; use crate::rpc::{RpcError, RpcConnection, Rpc};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum Authentication { enum Authentication {
// If unauthenticated, use a single client // If unauthenticated, use a single client
@ -34,6 +36,7 @@ enum Authentication {
pub struct HttpRpc { pub struct HttpRpc {
authentication: Authentication, authentication: Authentication,
url: String, url: String,
request_timeout: Duration,
} }
impl HttpRpc { impl HttpRpc {
@ -57,7 +60,18 @@ impl HttpRpc {
/// ///
/// A daemon requiring authentication can be used via including the username and password in the /// A daemon requiring authentication can be used via including the username and password in the
/// URL. /// URL.
pub async fn new(mut url: String) -> Result<Rpc<HttpRpc>, RpcError> { pub async fn new(url: String) -> Result<Rpc<HttpRpc>, 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<Rpc<HttpRpc>, RpcError> {
let authentication = if url.contains('@') { let authentication = if url.contains('@') {
// Parse out the username and password // Parse out the username and password
let url_clone = url; let url_clone = url;
@ -105,7 +119,7 @@ impl HttpRpc {
Authentication::Unauthenticated(Client::with_connection_pool()) 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] #[async_trait]
impl RpcConnection for HttpRpc { impl RpcConnection for HttpRpc {
async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> { async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> {
// TODO: Make this timeout configurable tokio::time::timeout(self.request_timeout, self.inner_post(route, body))
tokio::time::timeout(core::time::Duration::from_secs(30), self.inner_post(route, body))
.await .await
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))? .map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?
} }