Skip to content

Commit d46ba79

Browse files
committed
Merge commit 'refs/pull/48/head' of github.com:bitcoindevkit/rust-electrum-client
2 parents 078e513 + c8bace9 commit d46ba79

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/client.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use api::ElectrumApi;
1010
use batch::Batch;
1111
use config::Config;
1212
use raw_client::*;
13+
use std::convert::TryFrom;
1314
use types::*;
1415

1516
/// Generalized Electrum client that supports multiple backends. This wraps
@@ -52,12 +53,17 @@ macro_rules! impl_inner_call {
5253
return res;
5354
},
5455
Err(e) => {
55-
warn!("call retry:{}/{} {:?}", errors.len() + 1 , $self.config.retry(), e);
56-
errors.push(e);
57-
if errors.len() as u8 == $self.config.retry() {
56+
let failed_attempts = errors.len() + 1;
57+
58+
if retries_exhausted(failed_attempts, $self.config.retry()) {
59+
warn!("call '{}' failed after {} attempts", stringify!($name), failed_attempts);
5860
return Err(Error::AllAttemptsErrored(errors));
5961
}
6062

63+
warn!("call '{}' failed with {}, retry: {}/{}", stringify!($name), e, failed_attempts, $self.config.retry());
64+
65+
errors.push(e);
66+
6167
// Only one thread will try to recreate the client getting the write lock,
6268
// other eventual threads will get Err and will block at the beginning of
6369
// previous loop when trying to read()
@@ -71,11 +77,16 @@ macro_rules! impl_inner_call {
7177
break;
7278
},
7379
Err(e) => {
74-
warn!("client retry:{}/{} {:?}", errors.len() + 1, $self.config.retry(), e);
75-
errors.push(e);
76-
if errors.len() as u8 == $self.config.retry() {
80+
let failed_attempts = errors.len() + 1;
81+
82+
if retries_exhausted(failed_attempts, $self.config.retry()) {
83+
warn!("re-creating client failed after {} attempts", failed_attempts);
7784
return Err(Error::AllAttemptsErrored(errors));
7885
}
86+
87+
warn!("re-creating client failed with {}, retry: {}/{}", e, failed_attempts, $self.config.retry());
88+
89+
errors.push(e);
7990
}
8091
}
8192
}
@@ -86,6 +97,13 @@ macro_rules! impl_inner_call {
8697
}
8798
}
8899

100+
fn retries_exhausted(failed_attempts: usize, configured_retries: u8) -> bool {
101+
match u8::try_from(failed_attempts) {
102+
Ok(failed_attempts) => failed_attempts > configured_retries,
103+
Err(_) => true, // if the usize doesn't fit into a u8, we definitely exhausted our retries
104+
}
105+
}
106+
89107
impl ClientType {
90108
/// Constructor that supports multiple backends and allows configuration through
91109
/// the [Config]
@@ -289,3 +307,38 @@ impl ElectrumApi for Client {
289307
impl_inner_call!(self, calls_made)
290308
}
291309
}
310+
311+
#[cfg(test)]
312+
mod tests {
313+
use super::*;
314+
315+
#[test]
316+
fn more_failed_attempts_than_retries_means_exhausted() {
317+
let exhausted = retries_exhausted(10, 5);
318+
319+
assert_eq!(exhausted, true)
320+
}
321+
322+
#[test]
323+
fn failed_attempts_bigger_than_u8_means_exhausted() {
324+
let failed_attempts = u8::MAX as usize + 1;
325+
326+
let exhausted = retries_exhausted(failed_attempts, u8::MAX);
327+
328+
assert_eq!(exhausted, true)
329+
}
330+
331+
#[test]
332+
fn less_failed_attempts_means_not_exhausted() {
333+
let exhausted = retries_exhausted(2, 5);
334+
335+
assert_eq!(exhausted, false)
336+
}
337+
338+
#[test]
339+
fn attempts_equals_retries_means_not_exhausted_yet() {
340+
let exhausted = retries_exhausted(2, 2);
341+
342+
assert_eq!(exhausted, false)
343+
}
344+
}

0 commit comments

Comments
 (0)