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

feat: make yamux independent of the specific runtime #268

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 15 additions & 3 deletions yamux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ edition = "2018"
[dependencies]
bytes = "0.5.0"
futures = { version = "0.3.0" }
tokio = { version = "0.2.0", features = ["time", "rt-core"] }
tokio = { version = "0.2.0" }
tokio-util = { version = "0.3.0", features = ["codec"] }
log = "0.4"

futures-timer = { version = "3.0.2", optional = true }

[dev-dependencies]
env_logger = "0.6"
rand = "0.6"
rand = "0.7"
bytesize = "1"
tokio = { version = "0.2.0", features = ["time", "dns", "tcp", "io-util"] }
tokio = { version = "0.2.0", features = ["time", "dns", "tcp", "io-util", "rt-core"] }

[features]
default = ["tokio-timer"]
# use tokio timer
tokio-timer = ["tokio/time"]
# generic timer, this means that yamux can run under any runtime
# the difference of `AsyncRead/AsyncWrite` can be converted by `tokio-util`
generic-timer = ["futures-timer"]
# use futures-timer's wasm feature
wasm = ["generic-timer", "futures-timer/wasm-bindgen"]
66 changes: 59 additions & 7 deletions yamux/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use futures::{
Sink, Stream,
};
use log::debug;
use tokio::{
prelude::{AsyncRead, AsyncWrite},
time::Interval,
};
use tokio::prelude::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed;

use crate::{
Expand All @@ -28,6 +25,8 @@ use crate::{
StreamId,
};

use timer::{interval, Interval};

const BUF_SHRINK_THRESHOLD: usize = u8::max_value() as usize;
const TIMEOUT: Duration = Duration::from_secs(30);

Expand All @@ -40,11 +39,11 @@ pub struct Session<T> {
eof: bool,

// remoteGoAway indicates the remote side does
// not want futher connections. Must be first for alignment.
// not want further connections. Must be first for alignment.
remote_go_away: bool,

// localGoAway indicates that we should stop
// accepting futher connections. Must be first for alignment.
// accepting further connections. Must be first for alignment.
local_go_away: bool,

// nextStreamID is the next stream we should
Expand Down Expand Up @@ -118,7 +117,7 @@ where
FrameCodec::default().max_frame_size(config.max_stream_window_size),
);
let keepalive = if config.enable_keepalive {
Some(tokio::time::interval(config.keepalive_interval))
Some(interval(config.keepalive_interval))
} else {
None
};
Expand Down Expand Up @@ -620,6 +619,59 @@ where
}
}

mod timer {
#[cfg(feature = "generic-timer")]
pub use generic_time::{interval, Interval};
#[cfg(feature = "tokio-timer")]
pub use tokio::time::{interval, Interval};

#[cfg(feature = "generic-timer")]
mod generic_time {
use futures::{Future, Stream};
use futures_timer::Delay;
use std::{
pin::Pin,
task::{Context, Poll},
time::Duration,
};

pub struct Interval {
delay: Delay,
period: Duration,
}

impl Interval {
fn new(period: Duration) -> Self {
Self {
delay: Delay::new(period),
period,
}
}
}

impl Stream for Interval {
type Item = ();

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<()>> {
match Pin::new(&mut self.delay).poll(cx) {
Poll::Ready(_) => {
let dur = self.period;
self.delay.reset(dur);
Poll::Ready(Some(()))
}
Poll::Pending => Poll::Pending,
}
}
}

pub fn interval(period: Duration) -> Interval {
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");

Interval::new(period)
}
}
}

#[cfg(test)]
mod test {
use super::Session;
Expand Down
15 changes: 6 additions & 9 deletions yamux/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::{
channel::mpsc::{Receiver, Sender},
stream::FusedStream,
task::AtomicWaker,
SinkExt, Stream,
Stream,
};

use std::{
Expand Down Expand Up @@ -114,9 +114,8 @@ impl StreamHandle {
}

fn send_go_away(&mut self) {
let mut sender = self.event_sender.clone();
self.state = StreamState::LocalClosing;
tokio::spawn(async move { sender.send(StreamEvent::GoAway).await });
let _ignore = self.event_sender.try_send(StreamEvent::GoAway);
}

#[inline]
Expand Down Expand Up @@ -516,12 +515,10 @@ impl Drop for StreamHandle {
let frame = Frame::new_window_update(flags, self.id, 0);
let rst_event = StreamEvent::Frame(frame);
let event = StreamEvent::StateChanged((self.id, StreamState::Closed));
let mut sender = self.event_sender.clone();

tokio::spawn(async move {
let _ignore = sender.send(rst_event).await;
let _ignore = sender.send(event).await;
});
// It is indeed possible that it cannot be sent here, but ignore it for now
// we should wait a `async drop` api to do this instead of any runtime
let _ignore = self.event_sender.try_send(rst_event);
let _ignore = self.event_sender.try_send(event);
}
}
}
Expand Down