Skip to content

Commit

Permalink
chore: reduce public APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-komarevskiy committed Jul 17, 2024
1 parent df7b557 commit cb38300
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HealthCheckStatus, DynamicRouteProviderError>;
}

/// A struct representing the health check status of the node.
#[derive(Clone, PartialEq, Debug, Default)]
pub struct HealthCheckStatus {
pub(crate) struct HealthCheckStatus {
latency: Option<Duration>,
}

Expand All @@ -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,
}
Expand Down Expand Up @@ -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<S> {
pub(super) struct HealthManagerActor<S> {
/// The health checker.
checker: Arc<dyn HealthCheck>,
/// The period of the health check.
Expand Down
16 changes: 8 additions & 8 deletions ic-agent/src/agent/http_transport/dynamic_routing/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 6 additions & 5 deletions ic-agent/src/agent/http_transport/dynamic_routing/node.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down Expand Up @@ -52,7 +53,7 @@ impl TryFrom<&ApiBoundaryNode> for Node {
}

/// Checks if the given domain is a valid URL.
pub fn is_valid_domain<S: AsRef<str>>(domain: S) -> bool {
fn is_valid_domain<S: AsRef<str>>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Node>, 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,
}
Expand Down Expand Up @@ -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<S> {
pub(super) struct NodesFetchActor<S> {
/// The fetcher object responsible for fetching the nodes.
fetcher: Arc<dyn Fetch>,
/// Time period between fetches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<WeightedNode>,
existing_nodes: HashSet<Node>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 8 additions & 5 deletions ic-agent/src/agent/http_transport/dynamic_routing/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ use crate::agent::http_transport::{
route_provider::RouteProvider,
};

pub fn route_n_times(n: usize, f: Arc<impl RouteProvider + ?Sized>) -> Vec<String> {
pub(super) fn route_n_times(n: usize, f: Arc<impl RouteProvider + ?Sized>) -> Vec<String> {
(0..n)
.map(|_| f.route().unwrap().domain().unwrap().to_string())
.collect()
}

pub fn assert_routed_domains<T>(actual: Vec<T>, expected: Vec<T>, expected_repetitions: usize)
where
pub(super) fn assert_routed_domains<T>(
actual: Vec<T>,
expected: Vec<T>,
expected_repetitions: usize,
) where
T: AsRef<str> + Eq + Hash + Debug + Ord,
{
fn build_count_map<T>(items: &[T]) -> HashMap<&T, usize>
Expand Down Expand Up @@ -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<Vec<Node>>,
}
Expand Down Expand Up @@ -86,7 +89,7 @@ impl NodesFetcherMock {
}

#[derive(Debug)]
pub struct NodeHealthCheckerMock {
pub(super) struct NodeHealthCheckerMock {
healthy_nodes: Arc<ArcSwap<HashSet<Node>>>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = watch::Sender<Option<T>>;
pub(super) type SenderWatch<T> = watch::Sender<Option<T>>;

/// A type alias for the receiver end of a watch channel.
pub type ReceiverWatch<T> = watch::Receiver<Option<T>>;
pub(super) type ReceiverWatch<T> = watch::Receiver<Option<T>>;

/// A type alias for the sender end of a multi-producer, single-consumer channel.
pub type SenderMpsc<T> = mpsc::Sender<T>;
pub(super) type SenderMpsc<T> = mpsc::Sender<T>;

/// A type alias for the receiver end of a multi-producer, single-consumer channel.
pub type ReceiverMpsc<T> = mpsc::Receiver<T>;
pub(super) type ReceiverMpsc<T> = mpsc::Receiver<T>;

/// A type alias for an atomic swap operation on a shared value.
pub type AtomicSwap<T> = Arc<ArcSwap<T>>;
pub(super) type AtomicSwap<T> = Arc<ArcSwap<T>>;
3 changes: 2 additions & 1 deletion ic-agent/src/agent/http_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ 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;
#[cfg(all(feature = "reqwest", not(target_family = "wasm")))]
pub mod route_provider;

0 comments on commit cb38300

Please sign in to comment.