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

refactor: Simplify timeout calculation in Poll::poll() #214

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc, time::Duration};
use std::{
cell::RefCell,
collections::HashMap,
rc::Rc,
sync::Arc,
time::{Duration, Instant},
};

#[cfg(unix)]
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd as Borrowed, RawFd as Raw};
Expand Down Expand Up @@ -215,17 +221,15 @@ impl Poll {
}

pub(crate) fn poll(&self, mut timeout: Option<Duration>) -> crate::Result<Vec<PollEvent>> {
let now = std::time::Instant::now();

// Adjust the timeout for the timers.
if let Some(next_timeout) = self.timers.borrow().next_deadline() {
if next_timeout <= now {
timeout = Some(Duration::ZERO);
} else if let Some(deadline) = timeout {
timeout = Some(std::cmp::min(deadline, next_timeout - now));
} else {
timeout = Some(next_timeout - now);
}
let timeout2 = self
kenoss marked this conversation as resolved.
Show resolved Hide resolved
.timers
.borrow()
.next_deadline()
.map(|deadline| deadline.saturating_duration_since(Instant::now()));
timeout = match (timeout, timeout2) {
(Some(timeout), Some(timeout2)) => Some(timeout.min(timeout2)),
_ => timeout.or(timeout2),
};

let mut events = self.events.borrow_mut();
Expand Down Expand Up @@ -263,8 +267,7 @@ impl Poll {

drop(events);

// Update 'now' as some time may have elapsed in poll()
let now = std::time::Instant::now();
let now = Instant::now();
let mut timers = self.timers.borrow_mut();
while let Some((_, token)) = timers.next_expired(now) {
poll_events.push(PollEvent {
Expand Down