From a11ed03031cb59b412950d62960fc27cf919e2bf Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 2 Feb 2023 16:24:30 +0100 Subject: Avoid sending useless changes to downstream listener --- src/catalog.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/catalog.rs b/src/catalog.rs index cad7f5b..042f745 100644 --- a/src/catalog.rs +++ b/src/catalog.rs @@ -22,7 +22,7 @@ use crate::{Consul, WithIndex}; /// Node summary, as specified in response to "list nodes" API calls in /// -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct Node { pub node: String, @@ -31,7 +31,7 @@ pub struct Node { } /// One of the services returned in a CatalogNode -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct Service { pub service: String, @@ -42,7 +42,7 @@ pub struct Service { /// Full node info, as specified in response to "retrieve map of services for a node" API call in /// -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct CatalogNode { pub node: Node, @@ -55,7 +55,7 @@ pub type ServiceList = HashMap>; /// Node serving a service, as specified in response to "list nodes for a service" API call in /// -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct ServiceNode { pub node: String, @@ -70,7 +70,7 @@ pub struct ServiceNode { /// Node serving a service with health info, /// as specified in response to "list service instances for a service" health API call in /// -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct HealthServiceNode { pub node: Node, @@ -79,7 +79,7 @@ pub struct HealthServiceNode { } /// A health check as returned in HealthServiceNode -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub struct HealthCheck { pub node: String, @@ -244,7 +244,14 @@ async fn do_watch_all_service_health( match watch_res { Ok(nodes) => { let index = nodes.index(); - services.insert(service.clone(), nodes.into_inner().into()); + + let nodes = nodes.into_inner(); + if services.get(&service).as_ref().map(|n| &n[..]) != Some(&nodes[..]) { + services.insert(service.clone(), nodes.into()); + if tx.send(services.clone()).is_err() { + break; + } + } let consul = &consul; service_watchers.push(Box::pin(async move { @@ -252,10 +259,6 @@ async fn do_watch_all_service_health( .map_err(|e| (1, e)); (service, res) })); - - if tx.send(services.clone()).is_err() { - break; - } } Err((err_count, e)) => { warn!("Error getting service {}: {} ({} consecutive errors)", service, e, err_count); -- cgit v1.2.3