Skip to content

Commit

Permalink
feat(btcio): adding MAX_RETRIES to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Sep 4, 2024
1 parent 44882b5 commit 7eac515
Showing 1 changed file with 77 additions and 58 deletions.
135 changes: 77 additions & 58 deletions crates/btcio/src/rpc/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fmt,
sync::atomic::{AtomicUsize, Ordering},
sync::atomic::{AtomicUsize, Ordering}, time::Duration,
};

use async_trait::async_trait;
Expand All @@ -20,6 +20,7 @@ use serde_json::{
json,
value::{RawValue, Value},
};
use tokio::time::sleep;
use tracing::*;

use super::traits::BitcoinWallet;
Expand All @@ -32,6 +33,9 @@ use crate::rpc::{
/// This is an alias for the result type returned by the [`BitcoinClient`].
pub type ClientResult<T> = Result<T, ClientError>;

/// The maximum number of retries for a request.
const MAX_RETRIES: u8 = 3;

/// Custom implementation to convert a value to a `Value` type.
pub fn to_value<T>(value: T) -> ClientResult<Value>
where
Expand Down Expand Up @@ -101,68 +105,83 @@ impl BitcoinClient {
method: &str,
params: &[Value],
) -> ClientResult<T> {
trace!(method = %method, params = ?params, "Calling bitcoin client");
let mut retries = 0;
loop {
trace!(%method, ?params, %retries, "Calling bitcoin client");

let id = self.next_id();
let id = self.next_id();

let response = self
.client
.post(&self.url)
.json(&json!({
"jsonrpc": "1.0",
"id": id,
"method": method,
"params": params
}))
.send()
.await;

match response {
Ok(resp) => {
let result = resp.json::<Response<T>>().await;
trace!(result = ?result, "Received response");
let data = result.map_err(|e| ClientError::Parse(e.to_string()))?;
trace!(data = ?data);
if let Some(err) = data.error {
return Err(ClientError::Server(err.code, err.message));
let response = self
.client
.post(&self.url)
.json(&json!({
"jsonrpc": "1.0",
"id": id,
"method": method,
"params": params
}))
.send()
.await;

match response {
Ok(resp) => {
let data = resp
.json::<Response<T>>()
.await
.map_err(|e| ClientError::Parse(e.to_string()))?;
if let Some(err) = data.error {
return Err(ClientError::Server(err.code, err.message));
}
return data
.result
.ok_or_else(|| ClientError::Other("Empty data received".to_string()));
}
data.result
.ok_or_else(|| ClientError::Other("Empty data received".to_string()))
}
Err(err) => {
warn!(err = %err, "Error calling bitcoin client");

if err.is_body() {
Err(ClientError::Body(err.to_string()))
} else if err.is_status() {
let e = match err.status() {
Some(code) => ClientError::Status(code.to_string(), err.to_string()),
_ => ClientError::Other(err.to_string()),
};
return Err(e);
} else if err.is_decode() {
warn!(%err, "decoding error");
return Err(ClientError::MalformedResponse(err.to_string()));
} else if err.is_connect() {
let e = ClientError::Connection(err.to_string());
warn!(%e, "connection error, retrying...");
Err(e)
} else if err.is_timeout() {
let e = ClientError::Timeout;
warn!(%e, "timeout error, retrying...");
Err(e)
} else if err.is_request() {
let e = ClientError::Request(err.to_string());
warn!(%e, "request error, retrying...");
Err(e)
} else if err.is_builder() {
Err(ClientError::ReqBuilder(err.to_string()))
} else if err.is_redirect() {
Err(ClientError::HttpRedirect(err.to_string()))
} else {
Err(ClientError::Other("Unknown error".to_string()))
Err(err) => {
warn!(err = %err, "Error calling bitcoin client");

if err.is_body() {
// Body error is unrecoverable
return Err(ClientError::Body(err.to_string()));
} else if err.is_status() {
// Status error is unrecoverable
let e = match err.status() {
Some(code) => ClientError::Status(code.to_string(), err.to_string()),

Check warning on line 148 in crates/btcio/src/rpc/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/rpc/client.rs#L148

Added line #L148 was not covered by tests
_ => ClientError::Other(err.to_string()),
};
return Err(e);
} else if err.is_decode() {
// Error decoding response, might be recoverable
let e = ClientError::MalformedResponse(err.to_string());
warn!(%e, "decoding error, retrying...");

Check warning on line 155 in crates/btcio/src/rpc/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/rpc/client.rs#L154-L155

Added lines #L154 - L155 were not covered by tests
} else if err.is_connect() {
// Connection error, might be recoverable
let e = ClientError::Connection(err.to_string());
warn!(%e, "connection error, retrying...");
} else if err.is_timeout() {
// Timeout error, might be recoverable
let e = ClientError::Timeout;
warn!(%e, "timeout error, retrying...");
} else if err.is_request() {
// General request error, might be recoverable
let e = ClientError::Request(err.to_string());
warn!(%e, "request error, retrying...");
} else if err.is_builder() {
// Request builder error is unrecoverable
return Err(ClientError::ReqBuilder(err.to_string()));
} else if err.is_redirect() {
// Redirect error is unrecoverable
return Err(ClientError::HttpRedirect(err.to_string()));
} else {
// Unknown error is unrecoverable
return Err(ClientError::Other("Unknown error".to_string()));
}
}
}
retries += 1;
if retries >= MAX_RETRIES {
return Err(ClientError::MaxRetriesExceeded(MAX_RETRIES));
}
sleep(Duration::from_millis(1_000)).await;

Check warning on line 184 in crates/btcio/src/rpc/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/rpc/client.rs#L180-L184

Added lines #L180 - L184 were not covered by tests
}
}
}
Expand Down

0 comments on commit 7eac515

Please sign in to comment.