-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #887 from freenet/186081753-add-topology-manager
186081753 - Add topology manager
- Loading branch information
Showing
11 changed files
with
1,418 additions
and
242 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::collections::VecDeque; | ||
use std::time::{Duration, Instant}; | ||
|
||
/// `ConnectionEvaluator` is used to evaluate connection scores within a specified time window. | ||
/// | ||
/// The evaluator records scores and determines whether a given score is better (higher) than | ||
/// any other scores within a predefined time window. A score is considered better if it's higher | ||
/// than all other scores in the time window, or if no scores were recorded within the window's | ||
/// duration. | ||
/// | ||
/// In the Freenet context, this will be used to titrate the rate of new connection requests accepted | ||
/// by a node. The node will only accept a new connection if the score of the connection is better | ||
/// than all other scores within the time window. | ||
pub(crate) struct ConnectionEvaluator { | ||
scores: VecDeque<(Instant, f64)>, | ||
window_duration: Duration, | ||
} | ||
|
||
impl ConnectionEvaluator { | ||
pub fn new(window_duration: Duration) -> Self { | ||
ConnectionEvaluator { | ||
scores: VecDeque::new(), | ||
window_duration, | ||
} | ||
} | ||
|
||
pub fn record_only(&mut self, score: f64) { | ||
self.record_only_with_current_time(score, Instant::now()); | ||
} | ||
|
||
pub fn record_only_with_current_time(&mut self, score: f64, current_time: Instant) { | ||
self.remove_outdated_scores(current_time); | ||
self.scores.push_back((current_time, score)); | ||
} | ||
|
||
pub fn record_and_eval(&mut self, score: f64) -> bool { | ||
self.record_and_eval_with_current_time(score, Instant::now()) | ||
} | ||
|
||
pub fn record_and_eval_with_current_time(&mut self, score: f64, current_time: Instant) -> bool { | ||
self.remove_outdated_scores(current_time); | ||
|
||
let is_better = self.scores.is_empty() || self.scores.iter().all(|&(_, s)| score > s); | ||
|
||
// Important to add new score *after* checking if it's better than all other scores | ||
self.record_only_with_current_time(score, current_time); | ||
|
||
is_better | ||
} | ||
|
||
fn remove_outdated_scores(&mut self, current_time: Instant) { | ||
while let Some(&(time, _)) = self.scores.front() { | ||
if current_time.duration_since(time) > self.window_duration { | ||
self.scores.pop_front(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use super::*; | ||
|
||
#[test] | ||
fn test_record_first_score() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
let current_time = Instant::now(); | ||
assert!(evaluator.record_and_eval_with_current_time(5.0, current_time)); | ||
// Assert evaluator.scores contains the new score | ||
assert_eq!(evaluator.scores.len(), 1); | ||
assert_eq!(evaluator.scores[0].1, 5.0); | ||
assert_eq!(evaluator.scores[0].0, current_time); | ||
} | ||
|
||
#[test] | ||
fn test_not_best_in_time_window() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
|
||
let start_time = Instant::now(); | ||
evaluator.record_and_eval_with_current_time(5.0, start_time); | ||
assert!(!evaluator.record_and_eval_with_current_time(4.0, start_time + Duration::from_secs(5)),); | ||
} | ||
|
||
#[test] | ||
fn test_best_in_time_window() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
|
||
let start_time = Instant::now(); | ||
evaluator.record_and_eval_with_current_time(5.0, start_time); | ||
assert!(evaluator.record_and_eval_with_current_time(4.0, start_time + Duration::from_secs(11)),); | ||
} | ||
|
||
#[test] | ||
fn test_remove_outdated_scores() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
|
||
let start_time = Instant::now(); | ||
evaluator.record_and_eval_with_current_time(5.0, start_time); | ||
evaluator.record_and_eval_with_current_time(6.0, start_time + Duration::from_secs(5)); | ||
evaluator.record_and_eval_with_current_time(4.5, start_time + Duration::from_secs(11)); | ||
assert_eq!(evaluator.scores.len(), 2); | ||
} | ||
|
||
#[test] | ||
fn test_empty_window_duration() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(0)); | ||
let current_time = Instant::now(); | ||
assert!(evaluator.record_and_eval_with_current_time(5.0, current_time)); | ||
assert!(!evaluator.record_and_eval_with_current_time(4.0, current_time)); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_scores_same_timestamp() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
let current_time = Instant::now(); | ||
evaluator.record_only_with_current_time(5.0, current_time); | ||
evaluator.record_only_with_current_time(6.0, current_time); | ||
assert_eq!(evaluator.scores.len(), 2); | ||
assert!( | ||
!evaluator.record_and_eval_with_current_time(4.0, current_time + Duration::from_secs(5)), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_negative_scores() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
let start_time = Instant::now(); | ||
assert!(evaluator.record_and_eval_with_current_time(-5.0, start_time),); | ||
assert!(evaluator.record_and_eval_with_current_time(-4.0, start_time + Duration::from_secs(5)),); | ||
assert!( | ||
!evaluator.record_and_eval_with_current_time(-6.0, start_time + Duration::from_secs(5)), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_large_number_of_scores() { | ||
let mut evaluator = ConnectionEvaluator::new(Duration::from_secs(10)); | ||
let start_time = Instant::now(); | ||
for i in 0..1000 { | ||
evaluator.record_only_with_current_time(i as f64, start_time + Duration::from_secs(i)); | ||
} | ||
assert!( | ||
evaluator.record_and_eval_with_current_time(1000.0, start_time + Duration::from_secs(1001)), | ||
); | ||
} |
Oops, something went wrong.