pub struct ServerConfig {
pub host: String,
pub port: u16,
pub workers: Option<usize>,
pub request_timeout: Option<Duration>,
pub max_body_size: usize,
pub max_connections: Option<usize>,
pub max_concurrent_requests: Option<usize>,
pub shutdown_timeout: Duration,
pub header_read_timeout: Duration,
pub tcp_nodelay: bool,
pub connect_info: bool,
}Expand description
Configuration for the HTTP server.
Fields are public for reading; the same-named builder methods consume and return the config for fluent construction:
use std::time::Duration;
use kerniq_core::ServerConfig;
let config = ServerConfig::new()
.host("0.0.0.0")
.port(3000)
.workers(4)
.request_timeout(Duration::from_secs(30))
.max_body_size(10 * 1024 * 1024);
assert_eq!(config.address(), "0.0.0.0:3000");
assert_eq!(config.workers, Some(4));Fields§
§host: StringInterface to bind to. Default: 127.0.0.1.
port: u16Port to bind to. 0 selects an ephemeral port. Default: 3000.
workers: Option<usize>Tokio worker threads used by run_blocking. Default: one per CPU core.
request_timeout: Option<Duration>Maximum time to produce a response. Exceeding it yields
408 REQUEST_TIMEOUT. Default: no timeout.
max_body_size: usizeMaximum request body size in bytes accepted by body extractors
(Json, String, Bytes, …). Larger bodies yield
413 PAYLOAD_TOO_LARGE. Default: 2 MiB.
max_connections: Option<usize>Maximum number of open TCP connections. Default: unlimited.
max_concurrent_requests: Option<usize>Maximum number of requests processed concurrently. Default: unlimited.
shutdown_timeout: DurationHow long graceful shutdown waits for in-flight requests and background tasks before giving up. Default: 30 seconds.
header_read_timeout: DurationMaximum time for a client to send the request headers (protects
against slow-header attacks). 0 disables it, which removes a
per-request timer (a few percent of throughput on trivial endpoints)
at the cost of that protection. Default: 30 seconds.
tcp_nodelay: boolDisable Nagle’s algorithm on accepted sockets. Default: true.
connect_info: boolMake the peer address available to handlers through
ConnectInfo<SocketAddr>. Costs one small allocation per request.
Default: true.
Implementations§
Source§impl ServerConfig
impl ServerConfig
Sourcepub fn workers(self, workers: usize) -> Self
pub fn workers(self, workers: usize) -> Self
Sets the number of Tokio worker threads used by run_blocking.
Sourcepub fn request_timeout(self, timeout: Duration) -> Self
pub fn request_timeout(self, timeout: Duration) -> Self
Sets the per-request timeout.
Sourcepub fn max_body_size(self, bytes: usize) -> Self
pub fn max_body_size(self, bytes: usize) -> Self
Sets the maximum request body size in bytes.
Sourcepub fn max_connections(self, max: usize) -> Self
pub fn max_connections(self, max: usize) -> Self
Limits the number of open TCP connections.
Sourcepub fn max_concurrent_requests(self, max: usize) -> Self
pub fn max_concurrent_requests(self, max: usize) -> Self
Limits the number of requests processed concurrently.
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> Self
pub fn shutdown_timeout(self, timeout: Duration) -> Self
Sets the graceful shutdown timeout.
Sourcepub fn header_read_timeout(self, timeout: Duration) -> Self
pub fn header_read_timeout(self, timeout: Duration) -> Self
Sets the request header read timeout (Duration::ZERO disables it).
Sourcepub fn tcp_nodelay(self, enabled: bool) -> Self
pub fn tcp_nodelay(self, enabled: bool) -> Self
Enables or disables TCP_NODELAY.
Sourcepub fn connect_info(self, enabled: bool) -> Self
pub fn connect_info(self, enabled: bool) -> Self
Enables or disables ConnectInfo<SocketAddr> for handlers.
Sourcepub fn from_env() -> Result<Self, ConfigError>
pub fn from_env() -> Result<Self, ConfigError>
Builds a configuration from defaults overridden by KERNIQ_*
environment variables. See ServerConfig::apply_env_vars.
Sourcepub fn apply_env(self) -> Result<Self, ConfigError>
pub fn apply_env(self) -> Result<Self, ConfigError>
Overrides values from the process environment. See
ServerConfig::apply_env_vars.
Sourcepub fn apply_env_vars<I, K, V>(self, vars: I) -> Result<Self, ConfigError>
pub fn apply_env_vars<I, K, V>(self, vars: I) -> Result<Self, ConfigError>
Overrides values from the given (name, value) pairs, using these
variables (durations are in seconds, sizes in bytes):
| Variable | Setting |
|---|---|
KERNIQ_HOST | host |
KERNIQ_PORT | port |
KERNIQ_WORKERS | workers |
KERNIQ_REQUEST_TIMEOUT | request_timeout |
KERNIQ_MAX_BODY_SIZE | max_body_size |
KERNIQ_MAX_CONNECTIONS | max_connections |
KERNIQ_MAX_CONCURRENT_REQUESTS | max_concurrent_requests |
KERNIQ_SHUTDOWN_TIMEOUT | shutdown_timeout |
KERNIQ_HEADER_READ_TIMEOUT | header_read_timeout |
KERNIQ_TCP_NODELAY | tcp_nodelay |
KERNIQ_CONNECT_INFO | connect_info |
Unrelated variables are ignored. The result is validated.
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
Available on crate feature config-file only.
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError>
config-file only.Loads a configuration from a TOML file. Missing keys use defaults; unknown keys are rejected. Durations are given in seconds.
host = "0.0.0.0"
port = 8080
request_timeout = 30
max_concurrent_requests = 1024Sourcepub fn from_toml_str(text: &str) -> Result<Self, ConfigError>
Available on crate feature config-file only.
pub fn from_toml_str(text: &str) -> Result<Self, ConfigError>
config-file only.Parses a configuration from a TOML string. See ServerConfig::from_file.
Sourcepub fn validate(&self) -> Result<(), ConfigError>
pub fn validate(&self) -> Result<(), ConfigError>
Checks that values are within range.