Skip to content

Commit a990a93

Browse files
fix: clippy
1 parent 556ad79 commit a990a93

File tree

12 files changed

+77
-19
lines changed

12 files changed

+77
-19
lines changed

ic-agent/src/agent/http_transport/dynamic_routing/dynamic_route_provider.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
AgentError,
3333
};
3434

35+
///
3536
pub const IC0_SEED_DOMAIN: &str = "ic0.app";
3637

3738
const MAINNET_ROOT_SUBNET_ID: &str =
@@ -45,6 +46,7 @@ const HEALTH_CHECK_PERIOD: Duration = Duration::from_secs(1);
4546

4647
const DYNAMIC_ROUTE_PROVIDER: &str = "DynamicRouteProvider";
4748

49+
///
4850
#[derive(Debug)]
4951
pub struct DynamicRouteProvider<S> {
5052
fetcher: Arc<dyn Fetch>,
@@ -75,6 +77,7 @@ impl<S> DynamicRouteProvider<S>
7577
where
7678
S: RoutingSnapshot + 'static,
7779
{
80+
///
7881
pub fn new(snapshot: S, seeds: Vec<Node>, http_client: Client) -> Self {
7982
let fetcher = Arc::new(NodesFetcher::new(
8083
http_client.clone(),
@@ -94,21 +97,25 @@ where
9497
}
9598
}
9699

100+
///
97101
pub fn with_fetcher(mut self, fetcher: Arc<dyn Fetch>) -> Self {
98102
self.fetcher = fetcher;
99103
self
100104
}
101105

106+
///
102107
pub fn with_fetch_period(mut self, period: Duration) -> Self {
103108
self.fetch_period = period;
104109
self
105110
}
106111

112+
///
107113
pub fn with_checker(mut self, checker: Arc<dyn HealthCheck>) -> Self {
108114
self.checker = checker;
109115
self
110116
}
111117

118+
///
112119
pub fn with_check_period(mut self, period: Duration) -> Self {
113120
self.check_period = period;
114121
self
@@ -183,11 +190,11 @@ where
183190
);
184191

185192
(found_healthy_seeds)
186-
.then(|| ())
187-
.ok_or_else(|| anyhow!("No healthy seeds found"))
193+
.then_some(())
194+
.ok_or(anyhow!("No healthy seeds found"))
188195
}
189196

190-
// Kill all running tasks.
197+
/// Kill all running tasks.
191198
pub async fn stop(&self) {
192199
self.token.cancel();
193200
self.tracker.close();
@@ -517,7 +524,6 @@ mod tests {
517524
// Setup.
518525
setup_tracing();
519526
let node_1 = Node::new(IC0_SEED_DOMAIN).unwrap();
520-
let node_2 = Node::new("api1.com").unwrap();
521527
// Set nodes fetching params: topology, fetching periodicity.
522528
let fetcher = Arc::new(NodesFetcherMock::new());
523529
let fetch_interval = Duration::from_secs(2);

ic-agent/src/agent/http_transport/dynamic_routing/health_check.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,43 @@ use crate::agent::http_transport::dynamic_routing::{
2121

2222
const CHANNEL_BUFFER: usize = 128;
2323

24+
///
2425
#[async_trait]
2526
pub trait HealthCheck: Send + Sync + Debug {
27+
///
2628
async fn check(&self, node: &Node) -> anyhow::Result<HealthCheckStatus>;
2729
}
2830

31+
///
2932
#[derive(Clone, PartialEq, Debug, Default)]
3033
pub struct HealthCheckStatus {
34+
///
3135
pub latency: Option<Duration>,
3236
}
3337

38+
///
3439
impl HealthCheckStatus {
40+
///
3541
pub fn new(latency: Option<Duration>) -> Self {
3642
Self { latency }
3743
}
3844

45+
///
3946
pub fn is_healthy(&self) -> bool {
4047
self.latency.is_some()
4148
}
4249
}
4350

51+
///
4452
#[derive(Debug)]
4553
pub struct HealthChecker {
4654
http_client: Client,
4755
timeout: Duration,
4856
}
4957

58+
///
5059
impl HealthChecker {
60+
///
5161
pub fn new(http_client: Client, timeout: Duration) -> Self {
5262
Self {
5363
http_client,
@@ -133,8 +143,10 @@ impl HealthCheckActor {
133143
}
134144
}
135145

146+
///
136147
pub const HEALTH_MANAGER_ACTOR: &str = "HealthManagerActor";
137148

149+
///
138150
pub struct HealthManagerActor<S> {
139151
checker: Arc<dyn HealthCheck>,
140152
period: Duration,
@@ -153,6 +165,7 @@ impl<S> HealthManagerActor<S>
153165
where
154166
S: RoutingSnapshot,
155167
{
168+
///
156169
pub fn new(
157170
checker: Arc<dyn HealthCheck>,
158171
period: Duration,
@@ -178,6 +191,7 @@ where
178191
}
179192
}
180193

194+
///
181195
pub async fn run(mut self) {
182196
loop {
183197
tokio::select! {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use crate::agent::http_transport::dynamic_routing::{health_check::HealthCheckStatus, node::Node};
22

3+
///
34
#[derive(Debug, Clone)]
45
pub struct FetchedNodes {
6+
///
57
pub nodes: Vec<Node>,
68
}
79

10+
///
811
pub struct NodeHealthState {
12+
///
913
pub node: Node,
14+
///
1015
pub health: HealthCheckStatus,
1116
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
pub mod dynamic_route_provider;
2+
///
23
pub mod health_check;
4+
///
35
pub mod messages;
6+
///
47
pub mod node;
8+
///
59
pub mod nodes_fetch;
10+
///
611
pub mod snapshot;
712
#[cfg(test)]
813
pub mod test_utils;
14+
///
915
pub mod type_aliases;

ic-agent/src/agent/http_transport/dynamic_routing/node.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use url::Url;
33
use crate::agent::ApiBoundaryNode;
44
use anyhow::anyhow;
55

6+
///
67
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
78
pub struct Node {
89
domain: String,
910
}
1011

1112
impl Node {
13+
///
1214
pub fn new(domain: &str) -> anyhow::Result<Self> {
1315
if !is_valid_domain(domain) {
1416
return Err(anyhow!("Invalid domain name {domain}"));
@@ -18,12 +20,14 @@ impl Node {
1820
})
1921
}
2022

23+
///
2124
pub fn domain(&self) -> String {
2225
self.domain.clone()
2326
}
2427
}
2528

2629
impl Node {
30+
///
2731
pub fn to_routing_url(&self) -> Url {
2832
Url::parse(&format!("https://{}/api/v2/", self.domain)).expect("failed to parse URL")
2933
}
@@ -44,6 +48,7 @@ impl TryFrom<&ApiBoundaryNode> for Node {
4448
}
4549
}
4650

51+
///
4752
pub fn is_valid_domain<S: AsRef<str>>(domain: S) -> bool {
4853
// Prepend scheme to make it a valid URL
4954
let url_string = format!("http://{}", domain.as_ref());

ic-agent/src/agent/http_transport/dynamic_routing/nodes_fetch.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ use crate::agent::{
2222

2323
const NODES_FETCH_ACTOR: &str = "NodesFetchActor";
2424

25+
///
2526
#[async_trait]
2627
pub trait Fetch: Sync + Send + Debug {
28+
///
2729
async fn fetch(&self, url: Url) -> anyhow::Result<Vec<Node>>;
2830
}
2931

32+
///
3033
#[derive(Debug)]
3134
pub struct NodesFetcher {
3235
http_client: Client,
3336
subnet_id: Principal,
3437
}
3538

3639
impl NodesFetcher {
40+
///
3741
pub fn new(http_client: Client, subnet_id: Principal) -> Self {
3842
Self {
3943
http_client,
@@ -67,6 +71,7 @@ impl Fetch for NodesFetcher {
6771
}
6872
}
6973

74+
///
7075
pub struct NodesFetchActor<S> {
7176
fetcher: Arc<dyn Fetch>,
7277
period: Duration,
@@ -80,6 +85,7 @@ impl<S> NodesFetchActor<S>
8085
where
8186
S: RoutingSnapshot,
8287
{
88+
///
8389
pub fn new(
8490
fetcher: Arc<dyn Fetch>,
8591
period: Duration,
@@ -98,6 +104,7 @@ where
98104
}
99105
}
100106

107+
///
101108
pub async fn run(self) {
102109
let mut interval = time::interval(self.period);
103110
loop {

ic-agent/src/agent/http_transport/dynamic_routing/snapshot/latency_based_routing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ struct WeightedNode {
2121
weight: f64,
2222
}
2323

24+
///
2425
#[derive(Default, Debug, Clone)]
2526
pub struct LatencyRoutingSnapshot {
2627
weighted_nodes: Vec<WeightedNode>,
2728
existing_nodes: HashSet<Node>,
2829
}
2930

31+
///
3032
impl LatencyRoutingSnapshot {
33+
///
3134
pub fn new() -> Self {
3235
Self {
3336
weighted_nodes: vec![],
@@ -39,7 +42,7 @@ impl LatencyRoutingSnapshot {
3942
// select weight index based on the input number in range [0, 1]
4043
#[inline(always)]
4144
fn weighted_sample(weights: &[f64], number: f64) -> Option<usize> {
42-
if number < 0.0 || number > 1.0 {
45+
if !(0.0..=1.0).contains(&number) {
4346
return None;
4447
}
4548
let sum: f64 = weights.iter().sum();
@@ -74,7 +77,7 @@ impl RoutingSnapshot for LatencyRoutingSnapshot {
7477
}
7578

7679
fn sync_nodes(&mut self, nodes: &[Node]) -> anyhow::Result<bool> {
77-
let new_nodes = HashSet::from_iter(nodes.into_iter().cloned());
80+
let new_nodes = HashSet::from_iter(nodes.iter().cloned());
7881
// Find nodes removed from snapshot.
7982
let nodes_removed: Vec<_> = self
8083
.existing_nodes
@@ -292,7 +295,7 @@ mod tests {
292295
assert_eq!(snapshot.weighted_nodes[0].node, node_2);
293296
// Add node_3 to weighted_nodes manually
294297
snapshot.weighted_nodes.push(WeightedNode {
295-
node: node_3.clone(),
298+
node: node_3,
296299
latency_mov_avg: LatencyMovAvg::new(),
297300
weight: 0.0,
298301
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
///
12
pub mod latency_based_routing;
3+
///
24
pub mod round_robin_routing;
5+
///
36
pub mod routing_snapshot;

ic-agent/src/agent/http_transport/dynamic_routing/snapshot/round_robin_routing.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::agent::http_transport::dynamic_routing::{
1010
health_check::HealthCheckStatus, node::Node, snapshot::routing_snapshot::RoutingSnapshot,
1111
};
1212

13+
///
1314
#[derive(Default, Debug, Clone)]
1415
pub struct RoundRobinRoutingSnapshot {
1516
current_idx: Arc<AtomicUsize>,
@@ -18,6 +19,7 @@ pub struct RoundRobinRoutingSnapshot {
1819
}
1920

2021
impl RoundRobinRoutingSnapshot {
22+
///
2123
pub fn new() -> Self {
2224
Self {
2325
current_idx: Arc::new(AtomicUsize::new(0)),
@@ -44,7 +46,7 @@ impl RoutingSnapshot for RoundRobinRoutingSnapshot {
4446
}
4547

4648
fn sync_nodes(&mut self, nodes: &[Node]) -> anyhow::Result<bool> {
47-
let new_nodes = HashSet::from_iter(nodes.into_iter().cloned());
49+
let new_nodes = HashSet::from_iter(nodes.iter().cloned());
4850
// Find nodes removed from snapshot.
4951
let nodes_removed: Vec<_> = self
5052
.existing_nodes
@@ -69,13 +71,13 @@ impl RoutingSnapshot for RoundRobinRoutingSnapshot {
6971
}
7072

7173
fn update_node(&mut self, node: &Node, health: HealthCheckStatus) -> anyhow::Result<bool> {
72-
if !self.existing_nodes.contains(&node) {
74+
if !self.existing_nodes.contains(node) {
7375
return Ok(false);
7476
}
7577
if health.latency.is_some() {
7678
Ok(self.healthy_nodes.insert(node.clone()))
7779
} else {
78-
Ok(self.healthy_nodes.remove(&node))
80+
Ok(self.healthy_nodes.remove(node))
7981
}
8082
}
8183
}
@@ -192,10 +194,7 @@ mod tests {
192194
snapshot.existing_nodes,
193195
HashSet::from_iter(vec![node_1.clone()])
194196
);
195-
assert_eq!(
196-
snapshot.healthy_nodes,
197-
HashSet::from_iter(vec![node_1.clone()])
198-
);
197+
assert_eq!(snapshot.healthy_nodes, HashSet::from_iter(vec![node_1]));
199198
// Sync with node_2
200199
let node_2 = Node::new("api2.com").unwrap();
201200
let nodes_changed = snapshot.sync_nodes(&[node_2.clone()]).unwrap();
@@ -218,11 +217,8 @@ mod tests {
218217
snapshot.existing_nodes,
219218
HashSet::from_iter(vec![node_3.clone(), node_2.clone()])
220219
);
221-
assert_eq!(
222-
snapshot.healthy_nodes,
223-
HashSet::from_iter(vec![node_2.clone()])
224-
);
225-
snapshot.healthy_nodes.insert(node_3.clone());
220+
assert_eq!(snapshot.healthy_nodes, HashSet::from_iter(vec![node_2]));
221+
snapshot.healthy_nodes.insert(node_3);
226222
// Sync with []
227223
let nodes_changed = snapshot.sync_nodes(&[]).unwrap();
228224
assert!(nodes_changed);

ic-agent/src/agent/http_transport/dynamic_routing/snapshot/routing_snapshot.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ use std::fmt::Debug;
22

33
use crate::agent::http_transport::dynamic_routing::{health_check::HealthCheckStatus, node::Node};
44

5+
///
56
pub trait RoutingSnapshot: Send + Sync + Clone + Debug {
7+
///
68
fn has_nodes(&self) -> bool;
9+
///
710
fn next(&self) -> Option<Node>;
11+
///
812
fn sync_nodes(&mut self, nodes: &[Node]) -> anyhow::Result<bool>;
13+
///
914
fn update_node(&mut self, node: &Node, health: HealthCheckStatus) -> anyhow::Result<bool>;
1015
}

0 commit comments

Comments
 (0)