mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
timer: fix Handle::timeout (#1093)
The old implementation didn't work for Timeout<Stream>, since the method took a deadline rather than a timeout.
This commit is contained in:
committed by
Carl Lerche
parent
d4803bc868
commit
b62d224fac
@@ -43,8 +43,12 @@ impl Delay {
|
|||||||
Delay { registration }
|
Delay { registration }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new_with_handle(deadline: Instant, handle: HandlePriv) -> Delay {
|
pub(crate) fn new_with_handle(
|
||||||
let mut registration = Registration::new(deadline, Duration::from_millis(0));
|
deadline: Instant,
|
||||||
|
duration: Duration,
|
||||||
|
handle: HandlePriv,
|
||||||
|
) -> Delay {
|
||||||
|
let mut registration = Registration::new(deadline, duration);
|
||||||
registration.register_with(handle);
|
registration.register_with(handle);
|
||||||
|
|
||||||
Delay { registration }
|
Delay { registration }
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::clock::now;
|
||||||
use crate::timer::Inner;
|
use crate::timer::Inner;
|
||||||
use crate::{Delay, Error, /*Interval,*/ Timeout};
|
use crate::{Delay, Error, /*Interval,*/ Timeout};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@@ -130,15 +131,21 @@ impl Handle {
|
|||||||
|
|
||||||
/// Create a `Delay` driven by this handle's associated `Timer`.
|
/// Create a `Delay` driven by this handle's associated `Timer`.
|
||||||
pub fn delay(&self, deadline: Instant) -> Delay {
|
pub fn delay(&self, deadline: Instant) -> Delay {
|
||||||
|
self.delay_timeout(deadline, Duration::from_secs(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delay_timeout(&self, deadline: Instant, duration: Duration) -> Delay {
|
||||||
match self.inner {
|
match self.inner {
|
||||||
Some(ref handle_priv) => Delay::new_with_handle(deadline, handle_priv.clone()),
|
Some(ref handle_priv) => {
|
||||||
None => Delay::new(deadline),
|
Delay::new_with_handle(deadline, Duration::from_secs(0), handle_priv.clone())
|
||||||
|
}
|
||||||
|
None => Delay::new_timeout(deadline, duration),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `Timeout` driven by this handle's associated `Timer`.
|
/// Create a `Timeout` driven by this handle's associated `Timer`.
|
||||||
pub fn timeout<T>(&self, value: T, deadline: Instant) -> Timeout<T> {
|
pub fn timeout<T>(&self, value: T, timeout: Duration) -> Timeout<T> {
|
||||||
Timeout::new_with_delay(value, self.delay(deadline))
|
Timeout::new_with_delay(value, self.delay_timeout(now() + timeout, timeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -109,16 +109,13 @@ fn hammer_cancel() {
|
|||||||
barrier.wait();
|
barrier.wait();
|
||||||
|
|
||||||
for _ in 0..PER_THREAD {
|
for _ in 0..PER_THREAD {
|
||||||
let deadline1 =
|
let timeout1 = Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY));
|
||||||
Instant::now() + Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY));
|
let timeout2 = Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY));
|
||||||
|
|
||||||
let deadline2 =
|
let deadline = Instant::now() + cmp::min(timeout1, timeout2);
|
||||||
Instant::now() + Duration::from_millis(rng.gen_range(MIN_DELAY, MAX_DELAY));
|
|
||||||
|
|
||||||
let deadline = cmp::min(deadline1, deadline2);
|
let delay = handle.delay(Instant::now() + timeout1);
|
||||||
|
let join = handle.timeout(delay, timeout2);
|
||||||
let delay = handle.delay(deadline1);
|
|
||||||
let join = handle.timeout(delay, deadline2);
|
|
||||||
|
|
||||||
exec.spawn(async move {
|
exec.spawn(async move {
|
||||||
let _ = join.await;
|
let _ = join.await;
|
||||||
|
|||||||
Reference in New Issue
Block a user