Skip to content
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

implement extra reset variants for Interval #5878

Merged
merged 1 commit into from
Aug 4, 2023

Conversation

victor-timofei
Copy link
Contributor

@victor-timofei victor-timofei commented Jul 17, 2023

Fixes: #5874

Motivation

The use case is explained in #5874

Solution

It just calls the internal reset on the specified Instant.

I have added a new field to the Interval struct to store the previous period.
On each tick if previous period is Some() it will be restored.

Integration tests will be added on a later commit.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-time Module: tokio/time labels Jul 17, 2023
tokio/src/time/interval.rs Show resolved Hide resolved

/// Resets the interval to a [`crate::time::Instant`] deadline.
///
/// This method ignores [`MissedTickBehavior`] strategy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to rephrase this as:

Sets the next tick to expire at the given instant. If the instant is in the past, then the MissedTickBehavior strategy will be used to catch up. If the instant is in the future, then the next tick will complete at the given instant, even if that means that it will sleep for longer than the duration of this Interval. If the Interval had any missed ticks before calling this method, then those are discarded.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. I see you've still marked it as a draft, though.

Comment on lines +595 to +597
/// catch up. If the instant is in the future, then the next tick will
/// complete at the given instant, even if that means that it will sleep for
/// longer than the duration of this [`Interval`]. If the [`Interval`] had
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the "even if that means that it will sleep for longer than the duration" part is not currently tested.

@victor-timofei victor-timofei marked this pull request as ready for review August 3, 2023 18:14
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@Darksonn Darksonn merged commit dbda204 into tokio-rs:master Aug 4, 2023
71 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-time Module: tokio/time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow scheduling a tokio::time::Interval to tick as soon as possible or after a custom time?
3 participants