Skip to content
Open
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
26 changes: 26 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,32 @@ impl Client {
client,
})
}

/// Creates a client to a bitcoind JSON-RPC server with custom timeout configuration.
pub async fn with_timeouts(
url: &str,
auth: Auth,
timeout: Option<Duration>,
connect_timeout: Option<Duration>,
) -> Result<Self> {
let mut parsed_url = Url::parse(url)?;

if let (Some(user), pass) = auth.get_user_pass()? {
parsed_url
.set_username(&user)
.map_err(|_| Error::Auth("Failed to set username".to_string()))?;
parsed_url
.set_password(pass.as_deref())
.map_err(|_| Error::Auth("Failed to set password".to_string()))?;
}

let transport = ReqwestTransport::with_timeouts(parsed_url, timeout, connect_timeout);
let client = JsonRpcClient::with_transport(transport);

Ok(Self {
client,
})
}
}

#[async_trait]
Expand Down
27 changes: 26 additions & 1 deletion client/src/transport.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
use async_trait::async_trait;
use jsonrpc_async::Transport;
use std::time::Duration;
use url::Url;

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(15);

pub struct ReqwestTransport {
client: reqwest::Client,
url: Url,
}

impl ReqwestTransport {
pub fn new(url: Url) -> Self {
let client = reqwest::Client::builder()
.timeout(DEFAULT_TIMEOUT)
.build()
.expect("Failed to build reqwest client");

Self {
client,
url,
}
}

pub fn with_timeouts(
url: Url,
timeout: Option<Duration>,
connect_timeout: Option<Duration>,
) -> Self {
let builder = reqwest::Client::builder()
.timeout(timeout.unwrap_or(DEFAULT_TIMEOUT))
.connect_timeout(connect_timeout.unwrap_or(DEFAULT_TIMEOUT));

let client = builder.build().expect("Failed to build reqwest client");

Self {
client: reqwest::Client::new(),
client,
url,
}
}
Expand Down
Loading