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

Support smol runtime #1719

Merged
merged 3 commits into from
Jan 24, 2024
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
3 changes: 3 additions & 0 deletions quinn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tls-rustls = ["rustls", "proto/tls-rustls", "ring"]
ring = ["proto/ring"]
runtime-tokio = ["tokio/time", "tokio/rt", "tokio/net"]
runtime-async-std = ["async-io", "async-std"]
runtime-smol = ["async-io", "smol"]

# Write logs via the `log` crate when no `tracing` subscriber exists
log = ["tracing/log", "proto/log", "udp/log"]

Expand All @@ -42,6 +44,7 @@ rustc-hash = "1.1"
pin-project-lite = "0.2"
proto = { package = "quinn-proto", path = "../quinn-proto", version = "0.11", default-features = false }
rustls = { version = "0.21.0", default-features = false, features = ["quic"], optional = true }
smol = { version = "2", optional = true }
thiserror = "1.0.21"
tracing = "0.1.10"
tokio = { version = "1.28.1", features = ["sync"] }
Expand Down
2 changes: 2 additions & 0 deletions quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub use crate::endpoint::{Accept, Endpoint};
pub use crate::recv_stream::{ReadError, ReadExactError, ReadToEndError, RecvStream};
#[cfg(feature = "runtime-async-std")]
pub use crate::runtime::AsyncStdRuntime;
#[cfg(feature = "runtime-smol")]
pub use crate::runtime::SmolRuntime;
#[cfg(feature = "runtime-tokio")]
pub use crate::runtime::TokioRuntime;
pub use crate::runtime::{default_runtime, AsyncTimer, AsyncUdpSocket, Runtime};
Expand Down
18 changes: 12 additions & 6 deletions quinn/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ pub trait AsyncUdpSocket: Send + Sync + Debug + 'static {
///
/// If `runtime-tokio` is enabled and this function is called from within a Tokio runtime context,
/// then `TokioRuntime` is returned. Otherwise, if `runtime-async-std` is enabled, `AsyncStdRuntime`
/// is returned. Otherwise, `None` is returned.
/// is returned. Otherwise, if `runtime-smol` is enabled, `SmolRuntime` is returned.
/// Otherwise, `None` is returned.
pub fn default_runtime() -> Option<Arc<dyn Runtime>> {
#[cfg(feature = "runtime-tokio")]
{
Expand All @@ -84,7 +85,12 @@ pub fn default_runtime() -> Option<Arc<dyn Runtime>> {
return Some(Arc::new(AsyncStdRuntime));
}

#[cfg(not(feature = "runtime-async-std"))]
#[cfg(all(feature = "runtime-smol", not(feature = "runtime-async-std")))]
{
return Some(Arc::new(SmolRuntime));
al8n marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(not(any(feature = "runtime-async-std", feature = "runtime-smol")))]
None
}

Expand All @@ -93,7 +99,7 @@ mod tokio;
#[cfg(feature = "runtime-tokio")]
pub use self::tokio::TokioRuntime;

#[cfg(feature = "runtime-async-std")]
mod async_std;
#[cfg(feature = "runtime-async-std")]
pub use self::async_std::AsyncStdRuntime;
#[cfg(feature = "async-io")]
mod async_io;
#[cfg(feature = "async-io")]
pub use self::async_io::*;
74 changes: 60 additions & 14 deletions quinn/src/runtime/async_std.rs → quinn/src/runtime/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,61 @@ use async_io::{Async, Timer};

use super::{AsyncTimer, AsyncUdpSocket, Runtime};

/// A Quinn runtime for async-std
#[derive(Debug)]
pub struct AsyncStdRuntime;
#[cfg(feature = "smol")]
pub use self::smol::SmolRuntime;

impl Runtime for AsyncStdRuntime {
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> {
Box::pin(Timer::at(t))
}
#[cfg(feature = "smol")]
mod smol {
use super::*;

/// A Quinn runtime for smol
al8n marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug)]
pub struct SmolRuntime;

impl Runtime for SmolRuntime {
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> {
Box::pin(Timer::at(t))
}

fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
::smol::spawn(future).detach();
}

fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
async_std::task::spawn(future);
fn wrap_udp_socket(
&self,
sock: std::net::UdpSocket,
) -> io::Result<Arc<dyn AsyncUdpSocket>> {
Ok(Arc::new(UdpSocket::new(sock)?))
}
}
}

fn wrap_udp_socket(&self, sock: std::net::UdpSocket) -> io::Result<Arc<dyn AsyncUdpSocket>> {
Ok(Arc::new(UdpSocket {
inner: udp::UdpSocketState::new((&sock).into())?,
io: Async::new(sock)?,
}))
#[cfg(feature = "async-std")]
pub use self::async_std::AsyncStdRuntime;

#[cfg(feature = "async-std")]
mod async_std {
use super::*;

/// A Quinn runtime for async-std
#[derive(Debug)]
pub struct AsyncStdRuntime;

impl Runtime for AsyncStdRuntime {
fn new_timer(&self, t: Instant) -> Pin<Box<dyn AsyncTimer>> {
Box::pin(Timer::at(t))
}

fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
::async_std::task::spawn(future);
}

fn wrap_udp_socket(
&self,
sock: std::net::UdpSocket,
) -> io::Result<Arc<dyn AsyncUdpSocket>> {
Ok(Arc::new(UdpSocket::new(sock)?))
}
}
}

Expand All @@ -48,6 +85,15 @@ struct UdpSocket {
inner: udp::UdpSocketState,
}

impl UdpSocket {
fn new(sock: std::net::UdpSocket) -> io::Result<Self> {
Ralith marked this conversation as resolved.
Show resolved Hide resolved
Ok(Self {
inner: udp::UdpSocketState::new((&sock).into())?,
io: Async::new(sock)?,
})
}
}

impl AsyncUdpSocket for UdpSocket {
fn poll_send(&self, cx: &mut Context, transmits: &[udp::Transmit]) -> Poll<io::Result<usize>> {
loop {
Expand Down
Loading