diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-22 15:19:49 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-22 15:19:49 +0200 |
commit | ec777652c17ccefed9b332b13e7a233de1a32867 (patch) | |
tree | 758a6e3016c3ee54611f7008b6f067117ec6125a | |
parent | 28b661aa47dcdae680847635115c0e0b18a7c67b (diff) | |
download | diplonat-ec777652c17ccefed9b332b13e7a233de1a32867.tar.gz diplonat-ec777652c17ccefed9b332b13e7a233de1a32867.zip |
Better retry mechanism
-rw-r--r-- | src/consul.rs | 4 | ||||
-rw-r--r-- | src/consul_actor.rs | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/src/consul.rs b/src/consul.rs index 01dff46..1bb30aa 100644 --- a/src/consul.rs +++ b/src/consul.rs @@ -27,6 +27,10 @@ impl Consul { }; } + pub fn watch_node_reset(&mut self) -> () { + self.idx = None; + } + pub async fn watch_node(&mut self, host: &str) -> Result<CatalogNode> { let url = match self.idx { Some(i) => format!("{}/v1/catalog/node/{}?index={}", self.url, host, i), diff --git a/src/consul_actor.rs b/src/consul_actor.rs index 163334d..94ed9ab 100644 --- a/src/consul_actor.rs +++ b/src/consul_actor.rs @@ -31,7 +31,7 @@ pub struct ConsulActor { fn retry_to_time(retries: u32, max_time: Duration) -> Duration { // 1.2^x seems to be a good value to exponentially increase time at a good pace // eg. 1.2^32 = 341 seconds ~= 5 minutes - ie. after 32 retries we wait 5 minutes - return Duration::from_secs(cmp::max(max_time.as_secs(), 1.2f64.powf(retries as f64) as u64)) + return Duration::from_secs(cmp::min(max_time.as_secs(), 1.2f64.powf(retries as f64) as u64)) } fn from_catalog_to_open_ports(catalog: &consul::CatalogNode) -> messages::OpenPorts { @@ -71,6 +71,7 @@ impl ConsulActor { let catalog = match self.consul.watch_node(&self.node).await { Ok(c) => c, Err(e) => { + self.consul.watch_node_reset(); self.retries = cmp::min(u32::MAX - 1, self.retries) + 1; let will_retry_in = retry_to_time(self.retries, Duration::from_secs(600)); error!("Failed to query consul. Will retry in {}s. {}", will_retry_in.as_secs(), e); @@ -78,7 +79,7 @@ impl ConsulActor { continue; } }; - + self.retries = 0; info!("{:#?}", from_catalog_to_open_ports(&catalog)); } } |