From 7207bf355e2b6418bb0d757859a5cdcdedf32530 Mon Sep 17 00:00:00 2001 From: Thomas Whiteway Date: Wed, 26 Feb 2020 18:55:08 +0000 Subject: time: avoid needing to `poll` DelayQueue after insertion (#2217) If an entry is inserted in the queue before the next deadline, the DelayQueue needs to update the Delay tracking the next time to poll. If there is an existing Delay, reset that rather than replacing it as if it's already been polled the task will be waiting for a notification before it will poll again, and dropping the Delay means that that notification will never be performed. --- tokio/src/time/delay_queue.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'tokio/src/time') diff --git a/tokio/src/time/delay_queue.rs b/tokio/src/time/delay_queue.rs index f6007d74..1790ada8 100644 --- a/tokio/src/time/delay_queue.rs +++ b/tokio/src/time/delay_queue.rs @@ -326,7 +326,12 @@ impl DelayQueue { }; if should_set_delay { - self.delay = Some(delay_until(self.start + Duration::from_millis(when))); + let delay_time = self.start + Duration::from_millis(when); + if let Some(ref mut delay) = &mut self.delay { + delay.reset(delay_time); + } else { + self.delay = Some(delay_until(delay_time)); + } } Key::new(key) -- cgit v1.2.3