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

Make serve generic over the listener and IO types #2941

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jplatte
Copy link
Member

@jplatte jplatte commented Sep 27, 2024

Supersedes #2479.

@jplatte jplatte changed the base branch from main to jplatte/msrv175 September 28, 2024 16:38
@jplatte jplatte force-pushed the jplatte/generic-serve branch 2 times, most recently from 53c870c to 72871dc Compare September 28, 2024 17:16
@jplatte jplatte added this to the 0.8 milestone Sep 28, 2024
Base automatically changed from jplatte/msrv175 to main September 28, 2024 21:21
Copy link

@daheige daheige left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

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

This looks good to me.

The underlying theme of the comments seems to be what do we want this to eventually look like and whether to move some functionality in serve into the Listener instances so I guess that's the main thing I'd want your opinions on.

Comment on lines +496 to +499
async fn accept<L>(listener: &mut L) -> Option<(L::Io, L::Addr)>
where
L: Listener,
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

How much TCP-specific is this method? I'm especially concerned with the logic regarding sleeps. Different implementations could use different set of ignorable errors than what we have in is_connection_error and then this could add needless sleeps and error logs.

I think this logic should be moved up to the impl Listener block and not be used for all IOs. Or again defer to a more specific axum-provided Listener or mapping function (this is similar to the other comment).

Comment on lines +416 to +417
let tcp_stream: &tokio::net::TcpStream = <dyn std::any::Any>::downcast_ref(&io)
.expect("internal error: tcp_nodelay used with the wrong type of listener");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd rather we avoid special-casing like this.

I assume that the end game would be either a) to have these configurations on a specific Listener (like axum::TcpListener) which would accept a connection and set this and possibly other configurations or b) have something like ListenerExt::map_io -> impl Listener which could prepare each TcpStream (or any other Listener::Io) with additional connection options.

Both of these are also possible from user code so I think even if we didn't want to create either right now, we could just drop the first class nodelay support.

How do you see the end goal regarding this?

type Addr = std::net::SocketAddr;

#[inline]
async fn accept(&mut self) -> io::Result<(Self::Io, Self::Addr)> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Currently, we ignore these errors. Or more precisely, we ignore "connection errors" and log and sleep on all other errors.

I think we could just have a return type of (Self::Io, Self::Addr) here and let the underlying method do whatever it wants to handle transient accept errors, log them, sleep, panic, or anything else.

Or we could let it return a Result but then I think those errors should be fatal and we'd just propagate them. And in that case I'd also consider whether to have an associated type for the error type instead of forcing std::io::Error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants