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 AddrIncomingConfig #38

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/configure_addr_incoming.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Run with `cargo run --example configure_http` command.
//!
//! To connect through browser, navigate to "http://localhost:3000" url.

use axum::{routing::get, Router};
use axum_server::AddrIncomingConfig;
use std::net::SocketAddr;
use std::time::Duration;

#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));

let config = AddrIncomingConfig::new()
.tcp_nodelay(true)
.tcp_sleep_on_accept_errors(true)
.tcp_keepalive(Some(Duration::from_secs(32)))
.build();

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum_server::bind(addr)
.addr_incoming_config(config)
.serve(app.into_make_service())
.await
.unwrap();
}
55 changes: 55 additions & 0 deletions src/addr_incoming_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::time::Duration;

/// A configuration for [`AddrIncoming`].
#[derive(Debug, Clone)]
pub struct AddrIncomingConfig {
pub(crate) tcp_sleep_on_accept_errors: bool,
pub(crate) tcp_keepalive: Option<Duration>,
pub(crate) tcp_nodelay: bool,
}

impl Default for AddrIncomingConfig {
fn default() -> Self {
Self::new()
}
}

impl AddrIncomingConfig {
/// Creates a default [`AddrIncoming`] config.
pub fn new() -> AddrIncomingConfig {
Self {
tcp_sleep_on_accept_errors: true,
tcp_keepalive: None,
tcp_nodelay: false,
}
}

/// Builds the config, creating an owned version of it.
pub fn build(&mut self) -> Self {
self.clone()
}

/// Set whether to sleep on accept errors, to avoid exhausting file descriptor limits.
///
/// Default is `true`.
pub fn tcp_sleep_on_accept_errors(&mut self, val: bool) -> &mut Self {
self.tcp_sleep_on_accept_errors = val;
self
}

/// Set how often to send TCP keepalive probes.
///
/// Default is `false`.
pub fn tcp_keepalive(&mut self, val: Option<Duration>) -> &mut Self {
self.tcp_keepalive = val;
self
}

/// Set the value of `TCP_NODELAY` option for accepted connections.
///
/// Default is `false`.
pub fn tcp_nodelay(&mut self, val: bool) -> &mut Self {
self.tcp_nodelay = val;
self
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg))]

mod addr_incoming_config;
mod handle;
mod http_config;
mod notify_once;
Expand All @@ -96,6 +97,7 @@ pub mod accept;
pub mod service;

pub use self::{
addr_incoming_config::AddrIncomingConfig,
handle::Handle,
http_config::HttpConfig,
server::{bind, Server},
Expand Down
14 changes: 14 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::addr_incoming_config::AddrIncomingConfig;
use crate::{
accept::{Accept, DefaultAcceptor},
handle::Handle,
Expand Down Expand Up @@ -25,6 +26,7 @@ use tokio::{
pub struct Server<A = DefaultAcceptor> {
acceptor: A,
addr: SocketAddr,
addr_incoming_conf: AddrIncomingConfig,
handle: Handle,
http_conf: HttpConfig,
}
Expand All @@ -43,6 +45,7 @@ impl Server {
Self {
acceptor,
addr,
addr_incoming_conf: AddrIncomingConfig::default(),
handle,
http_conf: HttpConfig::default(),
}
Expand All @@ -55,6 +58,7 @@ impl<A> Server<A> {
Server {
acceptor,
addr: self.addr,
addr_incoming_conf: self.addr_incoming_conf,
handle: self.handle,
http_conf: self.http_conf,
}
Expand All @@ -72,6 +76,12 @@ impl<A> Server<A> {
self
}

/// Overwrite addr incoming configuration.
pub fn addr_incoming_config(mut self, config: AddrIncomingConfig) -> Self {
self.addr_incoming_conf = config;
self
}

/// Serve provided [`MakeService`].
///
/// # Errors
Expand All @@ -93,11 +103,15 @@ impl<A> Server<A> {
A::Future: Send,
{
let acceptor = self.acceptor;
let addr_incoming_conf = self.addr_incoming_conf;
let handle = self.handle;
let http_conf = self.http_conf;

let listener = TcpListener::bind(self.addr).await?;
let mut incoming = AddrIncoming::from_listener(listener).map_err(io_other)?;
incoming.set_sleep_on_errors(addr_incoming_conf.tcp_sleep_on_accept_errors);
incoming.set_keepalive(addr_incoming_conf.tcp_keepalive);
incoming.set_nodelay(addr_incoming_conf.tcp_nodelay);

handle.notify_listening(incoming.local_addr());

Expand Down