pub struct ConnectionLifetimeLimits { /* private fields */ }Expand description
Per-connection limits applied by serve, used to bound the lifetime of
individual connections.
Closing connections after a bounded lifetime pressures clients to establish
new connections, which is useful behind a load balancer (e.g. a Kubernetes
Service) that round-robins new connections across the current set of
backends: without rotation, a client’s connection pool keeps sending work to
whichever backends it first connected to, even after the pool has scaled up.
It also bounds the worst case when a client’s connection pool has no
rotation of its own.
The only limit currently available is MaxConnectionAge.
§Example
use std::time::Duration;
use axum::{Router, routing::get, serve::{ConnectionLifetimeLimits, MaxConnectionAge}};
let router = Router::new().route("/", get(|| async { "Hello, World!" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
let limits = ConnectionLifetimeLimits::new().max_connection_age(
MaxConnectionAge::new(Duration::from_secs(10 * 60))
// Random per-connection jitter added to the age, to avoid
// synchronized reconnect storms when many connections were
// established at once.
.jitter(Duration::from_secs(60))
// Hard cap on how long to wait for in-flight work after the age
// limit fires before forcibly closing.
.grace(Duration::from_secs(30)),
);
axum::serve(listener, router)
.connection_lifetime_limits(limits)
.await;Implementations§
Source§impl ConnectionLifetimeLimits
impl ConnectionLifetimeLimits
Sourcepub fn new() -> ConnectionLifetimeLimits
pub fn new() -> ConnectionLifetimeLimits
Create a new ConnectionLifetimeLimits with no limits set.
Sourcepub fn max_connection_age(
self,
age: MaxConnectionAge,
) -> ConnectionLifetimeLimits
pub fn max_connection_age( self, age: MaxConnectionAge, ) -> ConnectionLifetimeLimits
Cap how long a connection keeps accepting new requests.
See MaxConnectionAge for what the limit does and how to configure
its jitter and grace period.
Connection age is unbounded by default.
§Upgraded connections
This limit does not apply to connections that have been upgraded, e.g.
to a WebSocket or via CONNECT. Once an HTTP/1 connection is upgraded,
hyper hands the socket off to the upgrade handler and serve stops
tracking it, so neither the age limit nor the grace period affect it.
An HTTP/2 upgrade stays a stream on the tracked connection and is
treated like any other in-flight stream.
Trait Implementations§
Source§impl Clone for ConnectionLifetimeLimits
impl Clone for ConnectionLifetimeLimits
Source§fn clone(&self) -> ConnectionLifetimeLimits
fn clone(&self) -> ConnectionLifetimeLimits
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more