From d6453674292c88676d8551f387d5a3916b5ff360 Mon Sep 17 00:00:00 2001 From: Nikolay Komarevskiy Date: Wed, 17 Jul 2024 19:43:09 +0200 Subject: [PATCH] chore: reduce public APIs --- .../dynamic_routing/health_check.rs | 10 +++++----- .../agent/http_transport/dynamic_routing/mod.rs | 16 ++++++++-------- .../agent/http_transport/dynamic_routing/node.rs | 11 ++++++----- .../dynamic_routing/nodes_fetch.rs | 6 +++--- .../snapshot/latency_based_routing.rs | 2 +- .../dynamic_routing/snapshot/mod.rs | 6 +++--- .../http_transport/dynamic_routing/test_utils.rs | 13 ++++++++----- .../dynamic_routing/type_aliases.rs | 10 +++++----- ic-agent/src/agent/http_transport/mod.rs | 2 +- 9 files changed, 40 insertions(+), 36 deletions(-) diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/health_check.rs b/ic-agent/src/agent/http_transport/dynamic_routing/health_check.rs index d45a480e8..3ac6189c6 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/health_check.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/health_check.rs @@ -23,14 +23,14 @@ const CHANNEL_BUFFER: usize = 128; /// A trait representing a health check of the node. #[async_trait] -pub trait HealthCheck: Send + Sync + Debug { +pub(crate) trait HealthCheck: Send + Sync + Debug { /// Checks the health of the node. async fn check(&self, node: &Node) -> Result; } /// A struct representing the health check status of the node. #[derive(Clone, PartialEq, Debug, Default)] -pub struct HealthCheckStatus { +pub(crate) struct HealthCheckStatus { latency: Option, } @@ -53,7 +53,7 @@ impl HealthCheckStatus { /// A struct implementing the `HealthCheck` for the nodes. #[derive(Debug)] -pub struct HealthChecker { +pub(crate) struct HealthChecker { http_client: Client, timeout: Duration, } @@ -157,12 +157,12 @@ impl HealthCheckActor { } /// The name of the health manager actor. -pub const HEALTH_MANAGER_ACTOR: &str = "HealthManagerActor"; +pub(super) const HEALTH_MANAGER_ACTOR: &str = "HealthManagerActor"; /// A struct managing the health checks of the nodes. /// It receives the fetched nodes from the `NodesFetchActor` and starts the health checks for them. /// It also receives the health status of the nodes from the `HealthCheckActor/s` and updates the routing snapshot. -pub struct HealthManagerActor { +pub(super) struct HealthManagerActor { /// The health checker. checker: Arc, /// The period of the health check. diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/mod.rs b/ic-agent/src/agent/http_transport/dynamic_routing/mod.rs index 9b6b69def..ce64ab836 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/mod.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/mod.rs @@ -1,16 +1,16 @@ //! Dynamic routing implementation. -pub mod dynamic_route_provider; +pub(crate) mod dynamic_route_provider; /// Health check implementation. -pub mod health_check; +pub(super) mod health_check; /// Messages used in dynamic routing. -pub mod messages; +pub(super) mod messages; /// Node implementation. -pub mod node; +pub(crate) mod node; /// Nodes fetch implementation. -pub mod nodes_fetch; +pub(super) mod nodes_fetch; /// Routing snapshot implementation. -pub mod snapshot; +pub(crate) mod snapshot; #[cfg(test)] -pub mod test_utils; +pub(super) mod test_utils; /// Type aliases used in dynamic routing. -pub mod type_aliases; +pub(super) mod type_aliases; diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/node.rs b/ic-agent/src/agent/http_transport/dynamic_routing/node.rs index baf9d1851..4667493f1 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/node.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/node.rs @@ -1,12 +1,13 @@ use url::Url; -use crate::agent::ApiBoundaryNode; - -use super::dynamic_route_provider::DynamicRouteProviderError; +use crate::agent::{ + http_transport::dynamic_routing::dynamic_route_provider::DynamicRouteProviderError, + ApiBoundaryNode, +}; /// Represents a node in the dynamic routing. #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Node { +pub(crate) struct Node { domain: String, } @@ -52,7 +53,7 @@ impl TryFrom<&ApiBoundaryNode> for Node { } /// Checks if the given domain is a valid URL. -pub fn is_valid_domain>(domain: S) -> bool { +fn is_valid_domain>(domain: S) -> bool { // Prepend scheme to make it a valid URL let url_string = format!("http://{}", domain.as_ref()); Url::parse(&url_string).is_ok() diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/nodes_fetch.rs b/ic-agent/src/agent/http_transport/dynamic_routing/nodes_fetch.rs index 88fa372ff..2b2642ae5 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/nodes_fetch.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/nodes_fetch.rs @@ -26,14 +26,14 @@ const NODES_FETCH_ACTOR: &str = "NodesFetchActor"; /// Fetcher of nodes in the topology. #[async_trait] -pub trait Fetch: Sync + Send + Debug { +pub(crate) trait Fetch: Sync + Send + Debug { /// Fetches the nodes from the topology. async fn fetch(&self, url: Url) -> Result, DynamicRouteProviderError>; } /// A struct representing the fetcher of the nodes from the topology. #[derive(Debug)] -pub struct NodesFetcher { +pub(crate) struct NodesFetcher { http_client: Client, subnet_id: Principal, } @@ -83,7 +83,7 @@ impl Fetch for NodesFetcher { } /// A struct representing the actor responsible for fetching existing nodes and communicating it with the listener. -pub struct NodesFetchActor { +pub(super) struct NodesFetchActor { /// The fetcher object responsible for fetching the nodes. fetcher: Arc, /// Time period between fetches. diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/latency_based_routing.rs b/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/latency_based_routing.rs index 1ae101363..4fb3313bc 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/latency_based_routing.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/latency_based_routing.rs @@ -30,7 +30,7 @@ struct WeightedNode { /// In this routing strategy, nodes are randomly selected based on their averaged latency of the last WINDOW_SIZE health checks. /// Nodes with smaller average latencies are preferred for routing. #[derive(Default, Debug, Clone)] -pub struct LatencyRoutingSnapshot { +pub(crate) struct LatencyRoutingSnapshot { weighted_nodes: Vec, existing_nodes: HashSet, } diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/mod.rs b/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/mod.rs index 73df15376..91f635324 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/mod.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/snapshot/mod.rs @@ -1,6 +1,6 @@ /// Snapshot of the routing table. -pub mod latency_based_routing; +pub(crate) mod latency_based_routing; /// Node implementation. -pub mod round_robin_routing; +pub(crate) mod round_robin_routing; /// Routing snapshot implementation. -pub mod routing_snapshot; +pub(crate) mod routing_snapshot; diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/test_utils.rs b/ic-agent/src/agent/http_transport/dynamic_routing/test_utils.rs index 92de4c781..60004d75b 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/test_utils.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/test_utils.rs @@ -17,14 +17,17 @@ use crate::agent::http_transport::{ route_provider::RouteProvider, }; -pub fn route_n_times(n: usize, f: Arc) -> Vec { +pub(super) fn route_n_times(n: usize, f: Arc) -> Vec { (0..n) .map(|_| f.route().unwrap().domain().unwrap().to_string()) .collect() } -pub fn assert_routed_domains(actual: Vec, expected: Vec, expected_repetitions: usize) -where +pub(super) fn assert_routed_domains( + actual: Vec, + expected: Vec, + expected_repetitions: usize, +) where T: AsRef + Eq + Hash + Debug + Ord, { fn build_count_map(items: &[T]) -> HashMap<&T, usize> @@ -54,7 +57,7 @@ where } #[derive(Debug)] -pub struct NodesFetcherMock { +pub(super) struct NodesFetcherMock { // A set of nodes, existing in the topology. pub nodes: AtomicSwap>, } @@ -86,7 +89,7 @@ impl NodesFetcherMock { } #[derive(Debug)] -pub struct NodeHealthCheckerMock { +pub(super) struct NodeHealthCheckerMock { healthy_nodes: Arc>>, } diff --git a/ic-agent/src/agent/http_transport/dynamic_routing/type_aliases.rs b/ic-agent/src/agent/http_transport/dynamic_routing/type_aliases.rs index 04f32b661..6be931fbb 100644 --- a/ic-agent/src/agent/http_transport/dynamic_routing/type_aliases.rs +++ b/ic-agent/src/agent/http_transport/dynamic_routing/type_aliases.rs @@ -3,16 +3,16 @@ use std::sync::Arc; use tokio::sync::{mpsc, watch}; /// A type alias for the sender end of a watch channel. -pub type SenderWatch = watch::Sender>; +pub(super) type SenderWatch = watch::Sender>; /// A type alias for the receiver end of a watch channel. -pub type ReceiverWatch = watch::Receiver>; +pub(super) type ReceiverWatch = watch::Receiver>; /// A type alias for the sender end of a multi-producer, single-consumer channel. -pub type SenderMpsc = mpsc::Sender; +pub(super) type SenderMpsc = mpsc::Sender; /// A type alias for the receiver end of a multi-producer, single-consumer channel. -pub type ReceiverMpsc = mpsc::Receiver; +pub(super) type ReceiverMpsc = mpsc::Receiver; /// A type alias for an atomic swap operation on a shared value. -pub type AtomicSwap = Arc>; +pub(super) type AtomicSwap = Arc>; diff --git a/ic-agent/src/agent/http_transport/mod.rs b/ic-agent/src/agent/http_transport/mod.rs index 7ffdd622c..c7d6ca052 100644 --- a/ic-agent/src/agent/http_transport/mod.rs +++ b/ic-agent/src/agent/http_transport/mod.rs @@ -31,5 +31,5 @@ const ICP_API_SUB_DOMAIN: &str = ".icp-api.io"; #[allow(dead_code)] const LOCALHOST_SUB_DOMAIN: &str = ".localhost"; #[cfg(all(feature = "reqwest", not(target_family = "wasm")))] -pub mod dynamic_routing; +pub(crate) mod dynamic_routing; pub mod route_provider;