-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement optional support for axum 0.8's Listener trait
Doing this is required due to how the orphan rule works in Rust Signed-off-by: kanpov <karpovanton729@gmail.com>
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
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,45 @@ | ||
#[cfg(feature = "axum08")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "axum08")))] | ||
impl axum08::serve::Listener for crate::VsockListener { | ||
type Io = crate::VsockStream; | ||
|
||
type Addr = vsock::VsockAddr; | ||
|
||
async fn accept(&mut self) -> (Self::Io, Self::Addr) { | ||
loop { | ||
match std::future::poll_fn(|cx| self.poll_accept(cx)).await { | ||
Ok(tuple) => return tuple, | ||
Err(err) => handle_accept_error(err).await, | ||
} | ||
} | ||
} | ||
|
||
fn local_addr(&self) -> std::io::Result<Self::Addr> { | ||
self.local_addr() | ||
} | ||
} | ||
|
||
#[cfg(feature = "axum08")] | ||
async fn handle_accept_error(err: std::io::Error) { | ||
if matches!( | ||
err.kind(), | ||
std::io::ErrorKind::ConnectionRefused | ||
| std::io::ErrorKind::ConnectionAborted | ||
| std::io::ErrorKind::ConnectionReset | ||
) { | ||
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. | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
#![cfg_attr(docsrs, feature(doc_cfg))] | ||
|
||
mod axum_support; | ||
mod listener; | ||
mod split; | ||
mod stream; | ||
|