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 new() -> ServerConfig
pub fn new() -> ServerConfig
Creates a configuration with default values.
Sourcepub fn host(self, host: impl Into<String>) -> ServerConfig
pub fn host(self, host: impl Into<String>) -> ServerConfig
Sets the interface to bind to.
Sourcepub fn port(self, port: u16) -> ServerConfig
pub fn port(self, port: u16) -> ServerConfig
Sets the port to bind to.
Sourcepub fn workers(self, workers: usize) -> ServerConfig
pub fn workers(self, workers: usize) -> ServerConfig
Sets the number of Tokio worker threads used by run_blocking.
Sourcepub fn request_timeout(self, timeout: Duration) -> ServerConfig
pub fn request_timeout(self, timeout: Duration) -> ServerConfig
Sets the per-request timeout.
Sourcepub fn max_body_size(self, bytes: usize) -> ServerConfig
pub fn max_body_size(self, bytes: usize) -> ServerConfig
Sets the maximum request body size in bytes.
Sourcepub fn max_connections(self, max: usize) -> ServerConfig
pub fn max_connections(self, max: usize) -> ServerConfig
Limits the number of open TCP connections.
Sourcepub fn max_concurrent_requests(self, max: usize) -> ServerConfig
pub fn max_concurrent_requests(self, max: usize) -> ServerConfig
Limits the number of requests processed concurrently.
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> ServerConfig
pub fn shutdown_timeout(self, timeout: Duration) -> ServerConfig
Sets the graceful shutdown timeout.
Sourcepub fn header_read_timeout(self, timeout: Duration) -> ServerConfig
pub fn header_read_timeout(self, timeout: Duration) -> ServerConfig
Sets the request header read timeout (Duration::ZERO disables it).
Sourcepub fn tcp_nodelay(self, enabled: bool) -> ServerConfig
pub fn tcp_nodelay(self, enabled: bool) -> ServerConfig
Enables or disables TCP_NODELAY.
Sourcepub fn connect_info(self, enabled: bool) -> ServerConfig
pub fn connect_info(self, enabled: bool) -> ServerConfig
Enables or disables ConnectInfo<SocketAddr> for handlers.
Sourcepub fn from_env() -> Result<ServerConfig, ConfigError>
pub fn from_env() -> Result<ServerConfig, ConfigError>
Builds a configuration from defaults overridden by KERNIQ_*
environment variables. See ServerConfig::apply_env_vars.
Sourcepub fn apply_env(self) -> Result<ServerConfig, ConfigError>
pub fn apply_env(self) -> Result<ServerConfig, ConfigError>
Overrides values from the process environment. See
ServerConfig::apply_env_vars.
Sourcepub fn apply_env_vars<I, K, V>(
self,
vars: I,
) -> Result<ServerConfig, ConfigError>
pub fn apply_env_vars<I, K, V>( self, vars: I, ) -> Result<ServerConfig, 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<ServerConfig, ConfigError>
Available on crate feature config-file only.
pub fn from_file(path: impl AsRef<Path>) -> Result<ServerConfig, 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<ServerConfig, ConfigError>
Available on crate feature config-file only.
pub fn from_toml_str(text: &str) -> Result<ServerConfig, 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.
Trait Implementations§
Source§impl Clone for ServerConfig
impl Clone for ServerConfig
Source§fn clone(&self) -> ServerConfig
fn clone(&self) -> ServerConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ServerConfig
impl Debug for ServerConfig
Source§impl Default for ServerConfig
impl Default for ServerConfig
Source§fn default() -> ServerConfig
fn default() -> ServerConfig
Source§impl<'de> Deserialize<'de> for ServerConfigwhere
ServerConfig: Default,
impl<'de> Deserialize<'de> for ServerConfigwhere
ServerConfig: Default,
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ServerConfig, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ServerConfig, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
impl Eq for ServerConfig
Source§impl From<&ServerConfig> for ServeOptions
impl From<&ServerConfig> for ServeOptions
Source§fn from(config: &ServerConfig) -> ServeOptions
fn from(config: &ServerConfig) -> ServeOptions
Source§impl PartialEq for ServerConfig
impl PartialEq for ServerConfig
Source§impl Serialize for ServerConfig
impl Serialize for ServerConfig
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for ServerConfig
Auto Trait Implementations§
impl Forget for ServerConfig
impl Freeze for ServerConfig
impl RefUnwindSafe for ServerConfig
impl Send for ServerConfig
impl Sync for ServerConfig
impl Unpin for ServerConfig
impl UnsafeUnpin for ServerConfig
impl UnwindSafe for ServerConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
compression-br or compression-deflate or compression-gzip or compression-zstd only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.x-request-id as the header name. Read more