-
-
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
timer: introduce delay function shortcut #1440
Conversation
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.
Thanks 👍 Great work.
looks like this needs a |
Should then also other similar things like |
I think |
Ok :) Switched |
Oops, making |
@@ -64,6 +64,11 @@ pub fn sleep(duration: Duration) -> Delay { | |||
Delay::new(Instant::now() + duration) | |||
} | |||
|
|||
/// Create a Future that completes at `deadline`. | |||
pub fn delay(deadline: Instant) -> Delay { |
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.
Should this be tagged with #[inline]
?
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.
The compiler should be smart enough about that.
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.
Does rust do any cross-crate inlining of monomorphic functions that aren't explicitly marked whatsoever right now?
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.
According to rust-random/rand#472 (comment), it only inlines monomorphic functions only when LTO is used or it's marked explicitly. However, Delay::new
wasn't marked inline before, so nothing has really changed, the compiler can inline Delay::new
into delay
inside the crate and to the outside the situation will stay the same.
I created a binary crate outside of tokio
, included tokio-timer
, called delay
, compiled in release mode and objdump
showed that the call to delay
is not inlined, but Delay::new
was inlined into delay
. So the situation should stay as it was before with this PR.
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.
We can change this in a follow up PR
This looks good to me, we would just need CI to pass 👍 |
@carllerche Do you really want to have If we change this, shouldn't we also change the other types to make it consistent? At the very least, |
@Kobzol I believe so. I believe that w/ async / await, the pattern becomes: tokio::timer::delay(Duration::from_millis(100)).await the type no longer is referenced. In fact, I think it would be possible to get rid of the You are also correct that we should update all other APIs. |
I see, this will require a lot more changes then. I removed usages of I agree that with
pub fn interval(at: Instant, duration: Duration) -> impl Stream<Item=()> {
unfold(0, move |i| {
delay(at + duration * i).map(move |_| Some(((), i + 1)))
})
}
I can finish the |
@Kobzol we should only use |
Okay, thanks :) The |
@Kobzol It has to be moved out of the lib into the |
But then it cannot use |
@Kobzol it should be possible to rework the test to not have to name it. The simplest option is probably to use |
This commit adds a simple delay shortcut to avoid writing Delay::new everywhere and removes usages of Delay::new.
Thank you. I was under the impression that Btw do you also want to remove |
pub fn delay(deadline: Instant) -> Delay { | ||
Delay::new(deadline) | ||
} | ||
|
||
/// Create a Future that completes in `duration` from now. | ||
pub fn sleep(duration: Duration) -> Delay { |
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.
Not for this PR, but it looks like sleep
and delay
do the same thing now... I added a note to #1261 to figure this out. My inclination is to remove sleep
in favor of delay
.
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.
It's not exactly the same, delay
will wake at a specified time, sleep
will wake at now
+ specified duration. In my experience sleep
is actually much more useful, since usually I want to asynchronously "sleep" for some time from now
, rather than asynchronously wake up at some specific moment in the future. Maybe the names of the functions may be changed, but sleep
sounds reasonable to me, as it is an async counterpart to an actual thread::sleep
.
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.
ok, we can keep both for now
@Kobzol Thanks for the follow through and getting this done 👍 Great work. |
* test(http): use async/.await Signed-off-by: Weihang Lo <me@weihanglo.tw> * test(pool): use async/.await * test(pool): pass &mut Future into PollOnce * test(client): tests/benches using async/.await * test(client): change due to PR #1917 * test(client): change Delay to delay fucntion Ref: tokio-rs/tokio#1440 * test(client): remove warning triggers
This PR adds a simple delay shortcut to avoid writing
Delay::new
everywhere.I noticed that the
sleep
shortcut defined above my addition is not used anywhere in the repository, should I at least use it in tests (i.e.delay(now() + n)
->sleep(n)
)?Suggested by: #1261