-
Notifications
You must be signed in to change notification settings - Fork 28
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
make initial poll loop less aggressive #15
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use crate::{IfEvent, IpNet, Ipv4Net, Ipv6Net}; | ||
use fnv::FnvHashSet; | ||
use futures::channel::mpsc::UnboundedReceiver; | ||
use futures::future::FutureExt; | ||
use futures::future::Either; | ||
use futures::stream::{Stream, TryStreamExt}; | ||
use rtnetlink::constants::{RTMGRP_IPV4_IFADDR, RTMGRP_IPV6_IFADDR}; | ||
use rtnetlink::packet::address::nlas::Nla; | ||
|
@@ -12,6 +12,7 @@ use std::collections::VecDeque; | |
use std::future::Future; | ||
use std::io::{Error, ErrorKind, Result}; | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
use std::ops::DerefMut; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
|
@@ -41,19 +42,28 @@ impl IfWatcher { | |
let mut queue = VecDeque::default(); | ||
|
||
loop { | ||
futures::select! { | ||
msg = stream.try_next().fuse() => match msg { | ||
Ok(Some(msg)) => { | ||
for net in iter_nets(msg) { | ||
if addrs.insert(net) { | ||
queue.push_back(IfEvent::Up(net)); | ||
let fut = futures::future::select(conn, stream.try_next()); | ||
match fut.await { | ||
Either::Left(_) => { | ||
return Err(std::io::Error::new( | ||
ErrorKind::BrokenPipe, | ||
"rtnetlink socket closed", | ||
)) | ||
} | ||
Either::Right((x, c)) => { | ||
conn = c; | ||
match x { | ||
Ok(Some(msg)) => { | ||
for net in iter_nets(msg) { | ||
if addrs.insert(net) { | ||
queue.push_back(IfEvent::Up(net)); | ||
} | ||
} | ||
} | ||
}, | ||
Ok(None) => break, | ||
Err(err) => return Err(Error::new(ErrorKind::Other, err)), | ||
}, | ||
_r = (&mut conn).fuse() => {} | ||
Ok(None) => break, | ||
Err(err) => return Err(Error::new(ErrorKind::Other, err)), | ||
} | ||
} | ||
} | ||
} | ||
Ok(Self { | ||
|
@@ -89,7 +99,13 @@ impl Future for IfWatcher { | |
type Output = Result<IfEvent>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | ||
while Pin::new(&mut self.conn).poll(cx).is_ready() {} | ||
log::trace!("polling IfWatcher {:p}", self.deref_mut()); | ||
if Pin::new(&mut self.conn).poll(cx).is_ready() { | ||
return Poll::Ready(Err(std::io::Error::new( | ||
ErrorKind::BrokenPipe, | ||
"rtnetlink socket closed", | ||
))); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change looks fine to me |
||
while let Poll::Ready(Some((message, _))) = Pin::new(&mut self.messages).poll_next(cx) { | ||
match message.payload { | ||
NetlinkPayload::Error(err) => return Poll::Ready(Err(err.to_io())), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
so this should be equivalent. is it really easier to read? in the special case that you only need to poll two futures, maybe, maybe not. for polling more futures/streams
select!
macro is the way to go.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.
Rust async poses an interesting challenge for developers due to the interactions with other language features. Having all
.await
points exposed and clearly visible in anasync
scope is the only way to make that manageable — a macro that expands to.await
within the currentasync
scope is actively harmful. Imagine having to debug compiler error messages that some faraway type is not Sync?