-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move serve::Listener into its own module
- Loading branch information
Showing
2 changed files
with
108 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::{future::Future, time::Duration}; | ||
|
||
use tokio::{ | ||
io::{self, AsyncRead, AsyncWrite}, | ||
net::{TcpListener, TcpStream}, | ||
}; | ||
|
||
/// Types that can listen for connections. | ||
pub trait Listener: Send + 'static { | ||
/// The listener's IO type. | ||
type Io: AsyncRead + AsyncWrite + Unpin + Send + 'static; | ||
|
||
/// The listener's address type. | ||
type Addr: Send; | ||
|
||
/// Accept a new incoming connection to this listener. | ||
/// | ||
/// If the underlying accept call can return an error, this function must | ||
/// take care of logging and retrying. | ||
fn accept(&mut self) -> impl Future<Output = (Self::Io, Self::Addr)> + Send; | ||
|
||
/// Returns the local address that this listener is bound to. | ||
fn local_addr(&self) -> io::Result<Self::Addr>; | ||
} | ||
|
||
impl Listener for TcpListener { | ||
type Io = TcpStream; | ||
type Addr = std::net::SocketAddr; | ||
|
||
async fn accept(&mut self) -> (Self::Io, Self::Addr) { | ||
loop { | ||
match Self::accept(self).await { | ||
Ok(tup) => return tup, | ||
Err(e) => handle_accept_error(e).await, | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn local_addr(&self) -> io::Result<Self::Addr> { | ||
Self::local_addr(self) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
impl Listener for tokio::net::UnixListener { | ||
type Io = tokio::net::UnixStream; | ||
type Addr = tokio::net::unix::SocketAddr; | ||
|
||
async fn accept(&mut self) -> (Self::Io, Self::Addr) { | ||
loop { | ||
match Self::accept(self).await { | ||
Ok(tup) => return tup, | ||
Err(e) => handle_accept_error(e).await, | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn local_addr(&self) -> io::Result<Self::Addr> { | ||
Self::local_addr(self) | ||
} | ||
} | ||
|
||
async fn handle_accept_error(e: io::Error) { | ||
if is_connection_error(&e) { | ||
return; | ||
} | ||
|
||
// [From `hyper::Server` in 0.14](https://github.com/hyperium/hyper/blob/v0.14.27/src/server/tcp.rs#L186) | ||
// | ||
// > A possible scenario is that the process has hit the max open files | ||
// > allowed, and so trying to accept a new connection will fail with | ||
// > `EMFILE`. In some cases, it's preferable to just wait for some time, if | ||
// > the application will likely close some files (or connections), and try | ||
// > to accept the connection again. If this option is `true`, the error | ||
// > will be logged at the `error` level, since it is still a big deal, | ||
// > and then the listener will sleep for 1 second. | ||
// | ||
// hyper allowed customizing this but axum does not. | ||
error!("accept error: {e}"); | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
} | ||
|
||
fn is_connection_error(e: &io::Error) -> bool { | ||
matches!( | ||
e.kind(), | ||
io::ErrorKind::ConnectionRefused | ||
| io::ErrorKind::ConnectionAborted | ||
| io::ErrorKind::ConnectionReset | ||
) | ||
} |