-
Notifications
You must be signed in to change notification settings - Fork 0
Extract fairness logic #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,106 @@ | ||
| //! Helpers tracking fairness counters for connection processing. | ||
| //! | ||
| //! This module encapsulates the logic for deciding when high-priority | ||
| //! processing should yield to low-priority traffic based on configured | ||
| //! thresholds and optional time slices. | ||
|
|
||
| use tokio::time::Instant; | ||
|
|
||
| use crate::connection::FairnessConfig; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct Fairness { | ||
| config: FairnessConfig, | ||
| high_counter: usize, | ||
| high_start: Option<Instant>, | ||
| } | ||
|
|
||
| impl Fairness { | ||
| pub(crate) fn new(config: FairnessConfig) -> Self { | ||
| Self { | ||
| config, | ||
| high_counter: 0, | ||
| high_start: None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn set_config(&mut self, config: FairnessConfig) { | ||
| self.config = config; | ||
| self.reset(); | ||
| } | ||
|
|
||
| pub(crate) fn after_high(&mut self) { | ||
| self.high_counter += 1; | ||
| if self.high_counter == 1 { | ||
| self.high_start = Some(Instant::now()); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn should_yield(&self) -> bool { | ||
| let threshold_hit = self.config.max_high_before_low > 0 | ||
| && self.high_counter >= self.config.max_high_before_low; | ||
| let time_hit = self | ||
| .config | ||
| .time_slice | ||
| .zip(self.high_start) | ||
| .is_some_and(|(slice, start)| start.elapsed() >= slice); | ||
| threshold_hit || time_hit | ||
| } | ||
|
|
||
| pub(crate) fn after_low(&mut self) { self.reset(); } | ||
|
|
||
| pub(crate) fn reset(&mut self) { | ||
| self.high_counter = 0; | ||
| self.high_start = None; | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rstest::rstest; | ||
| use tokio::time::{self, Duration}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[rstest] | ||
| #[tokio::test] | ||
| async fn yield_after_threshold() { | ||
| let cfg = FairnessConfig { | ||
| max_high_before_low: 2, | ||
| time_slice: None, | ||
| }; | ||
| let mut fairness = Fairness::new(cfg); | ||
| fairness.after_high(); | ||
| assert!(!fairness.should_yield()); | ||
| fairness.after_high(); | ||
| assert!(fairness.should_yield()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[tokio::test] | ||
| async fn after_low_resets_counter() { | ||
| let cfg = FairnessConfig { | ||
| max_high_before_low: 1, | ||
| time_slice: None, | ||
| }; | ||
| let mut fairness = Fairness::new(cfg); | ||
| fairness.after_high(); | ||
| assert!(fairness.should_yield()); | ||
| fairness.after_low(); | ||
| assert!(!fairness.should_yield()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[tokio::test] | ||
| async fn time_slice_triggers_yield() { | ||
| time::pause(); | ||
| let cfg = FairnessConfig { | ||
| max_high_before_low: 0, | ||
| time_slice: Some(Duration::from_millis(5)), | ||
| }; | ||
| let mut fairness = Fairness::new(cfg); | ||
| fairness.after_high(); | ||
| time::advance(Duration::from_millis(6)).await; | ||
| assert!(fairness.should_yield()); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider refactoring the should_yield method to use early-return if-let patterns and consolidating after_low and reset into a shared helper for clarity.
You can flatten the two checks and avoid the
zip…is_some_andtrick with a straightforward early-returnif letpattern. This makes each “hit” condition explicit and keeps the method under 10 lines:Optionally, since
after_lowis justreset(), you can collapse them by inlining the shared logic into a private helper and calling it from both places: