-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Allow scheduling a tokio::time::Interval to tick as soon as possible or after a custom time? #5874
Comments
Seems reasonable enough to me. |
I would like to help with this. Would this be a good first issue? |
Yes, it's a good first issue. |
I just figured out that, in a pinch, you could emulate the behavior of my foo_update_int = tokio::time::interval(foo_update_int.period()); which works because in order to Still, having these be features of the |
Is your feature request related to a problem? Please describe.
I was writing a message handler for an app that has some state. Clients connecting to it should receive state updates periodically, but also on every state change. I had something like this:
However, this doesn't accomplish the goal of making sure that the update code runs as fast as possible; in fact it delays it until
now + interval
.Describe the solution you'd like
The implementation of
reset()
calls thetokio::time::sleep::Sleep::reset
with a new deadline of (now + self.period).Perhaps there could be an option to schedule the next tick to happen at exactly
now
, or perhaps with a customizable delay, like:Describe alternatives you've considered
For my particular case, I could run the regular
reset()
, and then manually do the same things as thetick()
clause would have done.This may not be easy in all cases however, because the logic there may be complex and not easy to refactor out into a function or closure, and copying it into each such reset site would get hard to maintain.
As for the proposal of a custom time for the next tick, you could perhaps do that with a mutable
Instant
variable you keep around, and the tick handler would check that theInstant
is in the past before running the main update code. Some clever extra logic around resetting the mainInterval
at strategic moments may ensure that the tick happens not too far into the future as compared to theInstant
, but I can't figure out what that should be right away.The text was updated successfully, but these errors were encountered: