-
Notifications
You must be signed in to change notification settings - Fork 274
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
fixed ipc leak #277
fixed ipc leak #277
Changes from 2 commits
adc2f69
a3e4401
2ccdeb6
feecb98
dd70e1f
e39aca6
93be6f1
5ce80c7
cf248b9
98a7dc2
8151fd5
15e6c03
a7630db
bb76cf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use jsonrpc::futures::{Poll, Async}; | ||
use jsonrpc::futures::stream::{Stream, Fuse}; | ||
|
||
pub trait SelectBothExt: Stream { | ||
fn select_both<S>(self, other: S) -> SelectBoth<Self, S> | ||
where S: Stream<Item = Self::Item, Error = Self::Error>, Self: Sized; | ||
} | ||
|
||
impl<T> SelectBothExt for T where T: Stream { | ||
fn select_both<S>(self, other: S) -> SelectBoth<Self, S> | ||
where S: Stream<Item = Self::Item, Error = Self::Error>, Self: Sized { | ||
new(self, other) | ||
} | ||
} | ||
|
||
/// An adapter for merging the output of two streams. | ||
/// | ||
/// The merged stream produces items from either of the underlying streams as | ||
/// they become available, and the streams are polled in a round-robin fashion. | ||
/// Errors, however, are not merged: you get at most one error at a time. | ||
/// | ||
/// Finishes when either of the streams stops responding | ||
#[derive(Debug)] | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct SelectBoth<S1, S2> { | ||
stream1: Fuse<S1>, | ||
stream2: Fuse<S2>, | ||
flag: bool, | ||
} | ||
|
||
fn new<S1, S2>(stream1: S1, stream2: S2) -> SelectBoth<S1, S2> | ||
where S1: Stream, | ||
S2: Stream<Item = S1::Item, Error = S1::Error> | ||
{ | ||
SelectBoth { | ||
stream1: stream1.fuse(), | ||
stream2: stream2.fuse(), | ||
flag: false, | ||
} | ||
} | ||
|
||
impl<S1, S2> Stream for SelectBoth<S1, S2> | ||
where S1: Stream, | ||
S2: Stream<Item = S1::Item, Error = S1::Error> | ||
{ | ||
type Item = S1::Item; | ||
type Error = S1::Error; | ||
|
||
fn poll(&mut self) -> Poll<Option<S1::Item>, S1::Error> { | ||
let (a, b) = if self.flag { | ||
(&mut self.stream2 as &mut Stream<Item=_, Error=_>, | ||
&mut self.stream1 as &mut Stream<Item=_, Error=_>) | ||
} else { | ||
(&mut self.stream1 as &mut Stream<Item=_, Error=_>, | ||
&mut self.stream2 as &mut Stream<Item=_, Error=_>) | ||
}; | ||
self.flag = !self.flag; | ||
|
||
match a.poll()? { | ||
Async::Ready(Some(item)) => return Ok(Some(item).into()), | ||
Async::Ready(None) => return Ok(None.into()), | ||
Async::NotReady => (), | ||
}; | ||
|
||
self.flag = !self.flag; | ||
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. is this second negation of 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. yes |
||
|
||
match b.poll()? { | ||
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. Am I right that this whole match can be replaced with simple 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. 🤦♂️ 👍 |
||
Async::Ready(Some(item)) => Ok(Some(item).into()), | ||
Async::Ready(None) => Ok(None.into()), | ||
Async::NotReady => Ok(Async::NotReady), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ use server_utils::tokio_io::AsyncRead; | |
use server_utils::{reactor, session, codecs}; | ||
|
||
use meta::{MetaExtractor, NoopExtractor, RequestContext}; | ||
use select_both::SelectBothExt; | ||
|
||
/// IPC server session | ||
pub struct Service<M: Metadata = (), S: Middleware<M> = NoopMiddleware> { | ||
|
@@ -172,7 +173,7 @@ impl<M: Metadata, S: Middleware<M>> ServerBuilder<M, S> { | |
}) | ||
}) | ||
.filter_map(|x| x) | ||
.select(receiver.map_err(|e| { | ||
.select_both(receiver.map_err(|e| { | ||
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. I for one would appreciate a comment as to why we're using |
||
warn!(target: "ipc", "Notification error: {:?}", e); | ||
std::io::ErrorKind::Other.into() | ||
})); | ||
|
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.
eating a virtual dispatch on every call to poll doesn't seem great to me.
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.
(a macro
poll_inner!(a, b)
would probably end up being less verbose in the end)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 is just modified the version of
select
from futures library https://docs.rs/futures/0.1.21/src/futures/stream/select.rs.html#11-15