From 1b7616be8dbebb18a882364c397115aeb17943b4 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Sat, 14 Sep 2024 15:09:08 +0300 Subject: [PATCH] timeout and threadpool settings --- conf.yml | 21 ++++++++------------- src/flowgate/config.rs | 13 +++++++++++-- src/flowgate/server.rs | 8 ++++---- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/conf.yml b/conf.yml index 6563d66..0de786a 100644 --- a/conf.yml +++ b/conf.yml @@ -1,17 +1,12 @@ http_host: localhost:80 # Http server host https_host: localhost:443 # Https server host +threadpool_size: 10 # Threadpool size (count of threads that accept requests) +connection_timeout: 10 # Read and write timeout of connections in seconds + sites: - # - domain: example.com # Domain with SSL - # host: localhost:8080 # Http server host - # ssl_cert: "/path/to/public/certificate.txt" # Ssl public certificate file - # ssl_key: "/path/to/private/key.txt" # Ssl private key file - # support_keep_alive: true # Does server supports keep-alive connections - - # - domain: sub.example.com # Domain with no SSL - # host: localhost:8081 # Http server host - # support_keep_alive: true # Does server supports keep-alive connections - - - domain: localhost - host: localhost:8080 - support_keep_alive: false + - domain: localhost # Site domain + host: localhost:8080 # Http server host + support_keep_alive: false # Does server supports keep-alive connections + # ssl_cert: "/path/to/public/certificate.txt" # Ssl public certificate file (optional) + # ssl_key: "/path/to/private/key.txt" # Ssl private key file (optional) diff --git a/src/flowgate/config.rs b/src/flowgate/config.rs index e1c5355..5594aa8 100644 --- a/src/flowgate/config.rs +++ b/src/flowgate/config.rs @@ -1,4 +1,4 @@ -use std::{fs, net::TcpStream, sync::Arc}; +use std::{fs, net::TcpStream, sync::Arc, time::Duration}; use serde_yml::Value; @@ -23,6 +23,8 @@ pub struct Config { pub sites: Arc>, pub http_host: String, pub https_host: String, + pub threadpool_size: usize, + pub connection_timeout: Duration } impl Config { @@ -33,6 +35,9 @@ impl Config { let http_host = doc["http_host"].as_str()?.to_string(); let https_host = doc["https_host"].as_str()?.to_string(); + let threadpool_size = doc["threadpool_size"].as_u64()? as usize; + let connection_timeout = Duration::from_secs(doc["connection_timeout"].as_u64()?); + let mut sites: Vec = Vec::new(); let sites_yaml = doc["sites"].as_sequence()?; @@ -60,10 +65,14 @@ impl Config { sites.push(site); } + let sites = Arc::new(sites); + Some(Config { - sites: Arc::new(sites), + sites, http_host, https_host, + threadpool_size, + connection_timeout }) } diff --git a/src/flowgate/server.rs b/src/flowgate/server.rs index d660413..14748c5 100644 --- a/src/flowgate/server.rs +++ b/src/flowgate/server.rs @@ -89,7 +89,7 @@ impl FlowgateServer { let cert = cert.build(); - let pool = ThreadPool::new(10); + let pool = ThreadPool::new(config.threadpool_size); info!("HTTPS server runned on {}", &config.https_host); @@ -100,9 +100,9 @@ impl FlowgateServer { move || { let Ok(stream) = stream else { return }; - - let Ok(_) = stream.set_write_timeout(Some(Duration::from_secs(10))) else { return }; - let Ok(_) = stream.set_read_timeout(Some(Duration::from_secs(10))) else { return }; + + let Ok(_) = stream.set_write_timeout(Some(config.connection_timeout)) else { return }; + let Ok(_) = stream.set_read_timeout(Some(config.connection_timeout)) else { return }; let Ok(addr) = stream.peer_addr() else { return };