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

Add generic trait to combine UnixListener and TcpListener #4385

Merged
merged 17 commits into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-util/src/lib.rs
Expand Up @@ -31,6 +31,7 @@ cfg_codec! {

cfg_net! {
pub mod udp;
pub mod net;
}

cfg_compat! {
Expand Down
104 changes: 104 additions & 0 deletions tokio-util/src/net/mod.rs
@@ -0,0 +1,104 @@
//! TCP/UDP/Unix helpers for tokio.

use crate::either::Either;
use std::future::Future;
use std::io::Result;
use std::pin::Pin;
use std::task::{Context, Poll};

#[cfg(unix)]
pub mod unix;

/// A trait for a listener: `TcpListener` and `UnixListener`.
pub trait Listener: Send + Unpin {
cecton marked this conversation as resolved.
Show resolved Hide resolved
/// The stream's type of this listener.
type Io: tokio::io::AsyncRead + tokio::io::AsyncWrite;
/// The socket address type of this listener.
type Addr;

/// Polls to accept a new incoming connection to this listener.
fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should take a pinned reference to self.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replicated the signature of the function from tokio's TCP listener. Are you sure you want to make it different?

Copy link
Contributor

@Darksonn Darksonn Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, making this take &self will make it difficult to implement this trait for anything else than the two listener types in Tokio itself. This is why I suggested changing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fe231ec


/// Accepts a new incoming connection from this listener.
fn accept(&self) -> ListenerAcceptFut<'_, Self>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to accept a &mut self here. Calling accept twice in parallel will result in one of them breaking due to lost wakeups.

Suggested change
/// Polls to accept a new incoming connection to this listener.
fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>>;
/// Accepts a new incoming connection from this listener.
fn accept(&self) -> ListenerAcceptFut<'_, Self>
/// Polls to accept a new incoming connection to this listener.
fn poll_accept(&mut self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>>;
/// Accepts a new incoming connection from this listener.
fn accept(&mut self) -> ListenerAcceptFut<'_, Self>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make the API different from tokio's TCP listener and Unix listener. If they don't use &mut self there, why would I need it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accept fn in Tokio will not have the same lost wakeup issue as this one does because it isn't implemented by calling poll_accept on itself. However, we cant really replicate what Tokio does with a trait. The &mut self avoids the footgun.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see!! Thanks for the explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fe231ec

where
Self: Sized + Unpin,
{
ListenerAcceptFut::new(self)
}

/// Returns the local address that this listener is bound to.
fn local_addr(&self) -> Result<Self::Addr>;
}

impl Listener for tokio::net::TcpListener {
type Io = tokio::net::TcpStream;
type Addr = std::net::SocketAddr;

fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>> {
Self::poll_accept(self, cx)
}

fn local_addr(&self) -> Result<Self::Addr> {
self.local_addr().map(Into::into)
}
}

/// Future for accepting a new connection from a listener.
#[derive(Debug)]
pub struct ListenerAcceptFut<'a, L> {
cecton marked this conversation as resolved.
Show resolved Hide resolved
listener: &'a L,
}

impl<'a, L> ListenerAcceptFut<'a, L>
where
L: Listener,
{
fn new(listener: &'a L) -> Self {
Self { listener }
}
}

impl<'a, L> Future for ListenerAcceptFut<'a, L>
where
L: Listener,
{
type Output = Result<(L::Io, L::Addr)>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.listener.poll_accept(cx)
}
}

impl<L, R> Listener for Either<L, R>
where
L: Listener,
R: Listener,
{
type Io = Either<<L as Listener>::Io, <R as Listener>::Io>;
type Addr = Either<<L as Listener>::Addr, <R as Listener>::Addr>;

fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>> {
match self {
Either::Left(listener) => listener
.poll_accept(cx)
.map(|res| res.map(|(stream, addr)| (Either::Left(stream), Either::Left(addr)))),
Either::Right(listener) => listener
.poll_accept(cx)
.map(|res| res.map(|(stream, addr)| (Either::Right(stream), Either::Right(addr)))),
}
}

fn local_addr(&self) -> Result<Self::Addr> {
match self {
Either::Left(listener) => {
let addr = listener.local_addr()?;
Ok(Either::Left(addr))
}
Either::Right(listener) => {
let addr = listener.local_addr()?;
Ok(Either::Right(addr))
}
}
}
}
18 changes: 18 additions & 0 deletions tokio-util/src/net/unix/mod.rs
@@ -0,0 +1,18 @@
//! Unix domain socket helpers.

use super::Listener;
use std::io::Result;
use std::task::{Context, Poll};

impl Listener for tokio::net::UnixListener {
type Io = tokio::net::UnixStream;
type Addr = tokio::net::unix::SocketAddr;

fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>> {
Self::poll_accept(self, cx)
}

fn local_addr(&self) -> Result<Self::Addr> {
self.local_addr().map(Into::into)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put this in the other folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will force me to add #[cfg(unix)].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I don't mind whether it is separate or together then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll keep it then but I don't mind either way either