-
Notifications
You must be signed in to change notification settings - Fork 66
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
Give more control on connection tasks #29
Comments
Related (but orthogonal) to this great enhancement proposal, I see by default the TCP keepalive is disabled in
To alleviate the issue, one can enable TCP keepalive with code like: use axum::{routing::get, Router};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum_server::bind(addr)
.addr_incoming_config(
AddrIncomingConfig::default()
.tcp_keepalive(Some(Duration::from_secs(60)))
.build(),
).serve(app.into_make_service())
.await
.unwrap();
} So far testing on a host that experienced this problem within a day or two shows very promising behavior. |
Furthermore, currently the only configurable property for TCP keepalive in axum_server for incoming/accepted connections is the Specifically, even though all three parameters are supported by the socket2 crate (used by Opportunities for improvement on both |
Here is a PR for If it was accepted, I would then post the corresponding PR to make this configurable at the |
Per empirical evidence enabling TCP keepalive with |
FYI hyperium/hyper#2991 was accepted. |
It seems the Relates to: https://github.com/programatik29/axum-server/pulls |
Yes, but it can be manually implemented like this: use axum_server::accept::Accept;
use log::error;
use socket2::{SockRef, TcpKeepalive};
use std::{marker::PhantomData, time::Duration};
use tokio::net::TcpStream;
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CustomAcceptor<S, I>(I, PhantomData<S>);
impl<S, I> CustomAcceptor<S, I> {
pub(crate) fn new(inner: I) -> Self {
Self(inner, PhantomData)
}
}
impl<S, I: Accept<TcpStream, S>> axum_server::accept::Accept<TcpStream, S>
for CustomAcceptor<S, I>
{
type Stream = <I as Accept<TcpStream, S>>::Stream;
type Service = <I as Accept<TcpStream, S>>::Service;
type Future = I::Future;
fn accept(&self, stream: TcpStream, service: S) -> I::Future {
if stream.set_nodelay(true).is_err() {
error!("failed to set TCP nodelay");
}
if SockRef::from(&stream)
.set_tcp_keepalive(
&TcpKeepalive::new()
.with_time(Duration::from_secs(10))
.with_interval(Duration::from_secs(10))
.with_retries(2),
)
.is_err()
{
error!("failed to set TCP keepalive");
}
self.0.accept(stream, service)
}
}
fn main() {
let http_server = axum_server::bind{_rustls}(...)
.map(CustomAcceptor::new)
.serve(...);
} |
Currently there is no way to shut a connection down except signaling a global shutdown.
Having this ability can be useful to detect slow clients and prevent some attacks.
The text was updated successfully, but these errors were encountered: