add keep-alive timeout

add more default settings

add basic config example
This commit is contained in:
xfy
2024-04-28 09:29:49 +08:00
parent 7cd8606d27
commit 8ecd35afaf
5 changed files with 40 additions and 11 deletions

View File

@ -1,10 +1,6 @@
[[host]]
ip = "0.0.0.0"
port = 4000
# index file name and format
index = ["index.html"]
# http keep alive timeout
keep-alive-time = 75
[[host.route]]
# route path: GET /
location = "/"

14
config.example_full.toml Normal file
View File

@ -0,0 +1,14 @@
[[host]]
ip = "0.0.0.0"
port = 4000
# index file name and format
index = ["index.html"]
# http keep alive timeout
keep-alive = 75
# http process max timeout
process-timeout = 15
[[host.route]]
# route path: GET /
location = "/"
# static file path
root = "./html"

View File

@ -1,4 +1,7 @@
use crate::{consts::keep_alive_timeoutd_efault, error::Result};
use crate::{
consts::{host_index, keep_alive_timeoutd_efault, process_timeout},
error::Result,
};
use std::{collections::BTreeMap, fs};
use serde::Deserialize;
@ -21,10 +24,14 @@ pub struct SettingHost {
#[serde(skip_deserializing, skip_serializing)]
pub route_map: BTreeMap<String, SettingRoute>,
/// Index files format
#[serde(default = "host_index")]
pub index: Vec<String>,
/// HTTP keep-alive timeout
#[serde(default = "keep_alive_timeoutd_efault")]
pub keep_alive: u16,
// http process max timeout
#[serde(default = "process_timeout")]
pub process_timeout: u16,
}
#[derive(Deserialize, Clone, Debug)]

View File

@ -6,7 +6,17 @@ pub const OS: &str = env::consts::OS;
pub const ARCH: &str = env::consts::ARCH;
// config defaults
pub const HOST_INDEX: [&str; 1] = ["index.html"];
pub fn host_index() -> Vec<String> {
HOST_INDEX.map(|h| h.to_string()).to_vec()
}
pub const KEEP_ALIVE_TIMEOUTD_EFAULT: u16 = 75;
pub fn keep_alive_timeoutd_efault() -> u16 {
KEEP_ALIVE_TIMEOUTD_EFAULT
}
pub const PROCESS_TIMEOUT: u16 = 75;
pub fn process_timeout() -> u16 {
PROCESS_TIMEOUT
}

View File

@ -31,11 +31,6 @@ use crate::config::SettingHost;
impl SettingHost {
pub fn mk_server(&'static self) -> impl Future<Output = anyhow::Result<()>> + 'static {
// Use a 5 second timeout for incoming connections to the server.
// If a request is in progress when the 5 second timeout elapses,
// use a 2 second timeout for processing the final request and graceful shutdown.
let connection_timeouts = [Duration::from_secs(5), Duration::from_secs(2)];
let addr = format!("{}:{}", self.ip, self.port);
#[allow(unreachable_code)]
async move {
@ -46,6 +41,13 @@ impl SettingHost {
info!("accept from {}", &addr);
let io = TokioIo::new(stream);
// Use keep_alive in config for incoming connections to the server.
// use process_timeout in config for processing the final request and graceful shutdown.
let connection_timeouts = [
Duration::from_secs(self.keep_alive.into()),
Duration::from_secs(self.process_timeout.into()),
];
let service = move |req| async move {
let start_time = time::Instant::now();
let res = handle_connection(req, self).await;
@ -127,7 +129,7 @@ async fn handle_connection(
let headers = response.headers_mut().ok_or(InternalServerError(anyhow!(
"build response failed, cannot get headser"
)))?;
headers.insert("Content-Type", "application/html".parse()?);
headers.insert("Content-Type", "text/html".parse()?);
// http method handle
let res = match *req_method {