-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 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
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(); | ||
} |
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,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 | ||
} | ||
} |
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