-
Notifications
You must be signed in to change notification settings - Fork 68
A0-1796: add justification broadcast ticker #833
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 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b4809b3
add justification broadcast ticker
maciejnems ebe044c
Merge branch 'main' into A0-1796-broadcast-ticker
maciejnems c989c8e
Join periodic and normal broadcast last Instant
maciejnems 78c5068
move clippy dead code
maciejnems d5a908b
rename to ticker
maciejnems bf0011c
restructure tests
maciejnems ab8954c
change timeout after try tick true
maciejnems 0d138ab
hmmmmmm
maciejnems 4a0d9ad
apply suggested changes to docs
maciejnems 699b965
add assert to constructor
maciejnems d5a351c
or maybe no assert
maciejnems 2d645e3
the in docs
maciejnems 3cc1a9e
wait_and_tick
maciejnems 10c94e5
Merge branch 'main' into A0-1796-broadcast-ticker
maciejnems 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 @@ | ||
| mod ticker; |
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,125 @@ | ||
| use tokio::time::{sleep, Duration, Instant}; | ||
|
|
||
| /// This struct is used for rate limiting as an on-demand ticker. It can be used for ticking | ||
| /// at least once `max_timeout` but not more than once every `min_timeout`. | ||
| /// Example usage would be to use `wait` method in main select loop and `try_tick` whenever | ||
| /// you would like to tick sooner in another branch of select. | ||
| pub struct Ticker { | ||
| last_tick: Instant, | ||
| current_timeout: Duration, | ||
| max_timeout: Duration, | ||
| min_timeout: Duration, | ||
| } | ||
|
|
||
| impl Ticker { | ||
| /// Retruns new Ticker struct. Behaves as if last tick happened during creation of TIcker. | ||
| /// Requires `max_timeout` >= `min_timeout`. | ||
| pub fn new(max_timeout: Duration, min_timeout: Duration) -> Self { | ||
kostekIV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert!( | ||
| max_timeout >= min_timeout, | ||
| "Max timoeut ({:?}) must be bigger then min timeout ({:?}) in Ticker", | ||
| max_timeout, | ||
| min_timeout | ||
| ); | ||
| Self { | ||
| last_tick: Instant::now(), | ||
| current_timeout: max_timeout, | ||
| max_timeout, | ||
| min_timeout, | ||
| } | ||
| } | ||
|
|
||
| /// Returns whether at least `min_timeout` time elapsed since last tick. | ||
| /// If `min_timeout` elapsed since last tick, returns true and records a tick. | ||
| /// If not, returns false and calls to `wait` will return when `min_timeout` | ||
| /// elapses until the next tick. | ||
maciejnems marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn try_tick(&mut self) -> bool { | ||
| let now = Instant::now(); | ||
| if now.saturating_duration_since(self.last_tick) >= self.min_timeout { | ||
| self.last_tick = now; | ||
| self.current_timeout = self.max_timeout; | ||
| true | ||
| } else { | ||
| self.current_timeout = self.min_timeout; | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Sleeps until next tick should happen. | ||
| /// When enough time elapsed, returns and records a tick. | ||
| pub async fn wait(&mut self) { | ||
kostekIV marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let since_last = Instant::now().saturating_duration_since(self.last_tick); | ||
| sleep(self.current_timeout.saturating_sub(since_last)).await; | ||
| self.current_timeout = self.max_timeout; | ||
| self.last_tick = Instant::now(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use tokio::time::{sleep, timeout, Duration}; | ||
|
|
||
| use super::Ticker; | ||
|
|
||
| const MAX_TIMEOUT: Duration = Duration::from_millis(700); | ||
| const MIN_TIMEOUT: Duration = Duration::from_millis(100); | ||
|
|
||
| const MAX_TIMEOUT_PLUS: Duration = Duration::from_millis(800); | ||
| const MIN_TIMEOUT_PLUS: Duration = Duration::from_millis(200); | ||
|
|
||
| fn setup_ticker() -> Ticker { | ||
| Ticker::new(MAX_TIMEOUT, MIN_TIMEOUT) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn try_tick() { | ||
| let mut ticker = setup_ticker(); | ||
|
|
||
| assert!(!ticker.try_tick()); | ||
| sleep(MIN_TIMEOUT).await; | ||
| assert!(ticker.try_tick()); | ||
| assert!(!ticker.try_tick()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn wait() { | ||
| let mut ticker = setup_ticker(); | ||
|
|
||
| assert_ne!(timeout(MIN_TIMEOUT_PLUS, ticker.wait()).await, Ok(())); | ||
| assert_eq!(timeout(MAX_TIMEOUT, ticker.wait()).await, Ok(())); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn wait_after_try_tick_true() { | ||
| let mut ticker = setup_ticker(); | ||
|
|
||
| assert!(!ticker.try_tick()); | ||
| sleep(MIN_TIMEOUT).await; | ||
| assert!(ticker.try_tick()); | ||
|
|
||
| assert_ne!(timeout(MIN_TIMEOUT_PLUS, ticker.wait()).await, Ok(())); | ||
| assert_eq!(timeout(MAX_TIMEOUT, ticker.wait()).await, Ok(())); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn wait_after_try_tick_false() { | ||
| let mut ticker = setup_ticker(); | ||
|
|
||
| assert!(!ticker.try_tick()); | ||
|
|
||
| assert_eq!(timeout(MIN_TIMEOUT_PLUS, ticker.wait()).await, Ok(())); | ||
| assert_ne!(timeout(MIN_TIMEOUT_PLUS, ticker.wait()).await, Ok(())); | ||
| assert_eq!(timeout(MAX_TIMEOUT, ticker.wait()).await, Ok(())); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn try_tick_after_wait() { | ||
| let mut ticker = setup_ticker(); | ||
|
|
||
| assert_eq!(timeout(MAX_TIMEOUT_PLUS, ticker.wait()).await, Ok(())); | ||
|
|
||
| assert!(!ticker.try_tick()); | ||
| sleep(MIN_TIMEOUT).await; | ||
| assert!(ticker.try_tick()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.