diff options
author | Quentin <quentin@dufour.io> | 2021-09-17 10:06:51 +0200 |
---|---|---|
committer | Quentin <quentin@dufour.io> | 2021-09-17 10:06:51 +0200 |
commit | 2bbc9109991f8bb79a09a965a1d2779e1749b25b (patch) | |
tree | 57dcb5b115b854d651ac6f952466d4109bac2d45 /src/consul_actor.rs | |
parent | fa25c54e47decf9f323ba0c614f4d9de106626d5 (diff) | |
parent | bf226d077ef2bea0567a7b36b4d25ce2d0b5191c (diff) | |
download | diplonat-2bbc9109991f8bb79a09a965a1d2779e1749b25b.tar.gz diplonat-2bbc9109991f8bb79a09a965a1d2779e1749b25b.zip |
Merge pull request 'added rustfmt, a guide about this, and a CI job to enforce code quality' (#10) from adrien/diplonat:meta/formating into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/diplonat/pulls/10
Diffstat (limited to 'src/consul_actor.rs')
-rw-r--r-- | src/consul_actor.rs | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/src/consul_actor.rs b/src/consul_actor.rs index ba5d704..d66f7fd 100644 --- a/src/consul_actor.rs +++ b/src/consul_actor.rs @@ -1,24 +1,24 @@ +use crate::consul; +use crate::messages; +use anyhow::Result; +use log::*; +use serde::{Deserialize, Serialize}; +use serde_lexpr::{error, from_str}; use std::cmp; +use std::collections::HashSet; use std::time::Duration; -use log::*; use tokio::sync::watch; use tokio::time::delay_for; -use anyhow::Result; -use serde::{Serialize, Deserialize}; -use serde_lexpr::{from_str,error}; -use crate::messages; -use crate::consul; -use std::collections::HashSet; #[derive(Serialize, Deserialize, Debug)] pub enum DiplonatParameter { tcp_port(HashSet<u16>), - udp_port(HashSet<u16>) + udp_port(HashSet<u16>), } #[derive(Serialize, Deserialize, Debug)] pub enum DiplonatConsul { - diplonat(Vec<DiplonatParameter>) + diplonat(Vec<DiplonatParameter>), } pub struct ConsulActor { @@ -27,13 +27,16 @@ pub struct ConsulActor { consul: consul::Consul, node: String, retries: u32, - tx_open_ports: watch::Sender<messages::PublicExposedPorts> + tx_open_ports: watch::Sender<messages::PublicExposedPorts>, } 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::min(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 to_parameters(catalog: &consul::CatalogNode) -> Vec<DiplonatConsul> { @@ -53,9 +56,9 @@ fn to_parameters(catalog: &consul::CatalogNode) -> Vec<DiplonatConsul> { } fn to_open_ports(params: &Vec<DiplonatConsul>) -> messages::PublicExposedPorts { - let mut op = messages::PublicExposedPorts { + let mut op = messages::PublicExposedPorts { tcp_ports: HashSet::new(), - udp_ports: HashSet::new() + udp_ports: HashSet::new(), }; for conf in params { @@ -73,9 +76,9 @@ fn to_open_ports(params: &Vec<DiplonatConsul>) -> messages::PublicExposedPorts { impl ConsulActor { pub fn new(url: &str, node: &str) -> Self { - let (tx, rx) = watch::channel(messages::PublicExposedPorts{ + let (tx, rx) = watch::channel(messages::PublicExposedPorts { tcp_ports: HashSet::new(), - udp_ports: HashSet::new() + udp_ports: HashSet::new(), }); return Self { @@ -95,7 +98,11 @@ impl ConsulActor { self.consul.watch_node_reset(); self.retries = cmp::min(std::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); + error!( + "Failed to query consul. Will retry in {}s. {}", + will_retry_in.as_secs(), + e + ); delay_for(will_retry_in).await; continue; } |