diff options
author | Antoine A <> | 2023-10-05 00:45:03 +0200 |
---|---|---|
committer | Antoine A <> | 2023-10-05 00:45:03 +0200 |
commit | 2e685701559118da856a0cde10849d82a112c9c7 (patch) | |
tree | f30d0bd02d43e45f14b68469d80090aab3688366 /common/src/reconnect.rs | |
parent | b898b96922ac80325d6371c0aafa3ee55c2c21b2 (diff) | |
download | depolymerization-2e685701559118da856a0cde10849d82a112c9c7.tar.gz depolymerization-2e685701559118da856a0cde10849d82a112c9c7.tar.bz2 depolymerization-2e685701559118da856a0cde10849d82a112c9c7.zip |
Test support for bitcoind 24.1 and small improvements
Diffstat (limited to 'common/src/reconnect.rs')
-rw-r--r-- | common/src/reconnect.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/common/src/reconnect.rs b/common/src/reconnect.rs index f569fc9..f2892f5 100644 --- a/common/src/reconnect.rs +++ b/common/src/reconnect.rs @@ -15,10 +15,13 @@ */ use std::time::Duration; +use exponential_backoff::Backoff; use log::error; use postgres::{Client, NoTls}; -const RECONNECT_DELAY: Duration = Duration::from_secs(5); +const MIN_RECONNECT_DELAY: Duration = Duration::from_millis(300); +const MAX_RECONNECT_DELAY: Duration = Duration::from_secs(10); +const VALID_DELAY: Duration = Duration::from_secs(3); pub struct AutoReconnect<S, C> { config: S, @@ -39,10 +42,12 @@ impl<S, C> AutoReconnect<S, C> { /// Create a new client, loop on error fn connect(config: &S, connect: fn(&S) -> Option<C>) -> C { + let backoff = Backoff::new(8, MIN_RECONNECT_DELAY, MAX_RECONNECT_DELAY); + let mut iter = backoff.iter(); loop { match connect(config) { Some(new) => return new, - None => std::thread::sleep(RECONNECT_DELAY), + None => std::thread::sleep(iter.next().unwrap_or(MAX_RECONNECT_DELAY)), } } } @@ -67,6 +72,6 @@ pub fn auto_reconnect_db(config: postgres::Config) -> AutoReconnectDb { .map_err(|err| error!("connect DB: {}", err)) .ok() }, - |client| client.is_valid(RECONNECT_DELAY).is_err(), + |client| client.is_valid(VALID_DELAY).is_err(), ) } |