Skip to main content

ConnectionLifetimeLimits

Struct ConnectionLifetimeLimits 

Source
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

Source

pub fn new() -> ConnectionLifetimeLimits

Create a new ConnectionLifetimeLimits with no limits set.

Source

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

Source§

fn clone(&self) -> ConnectionLifetimeLimits

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConnectionLifetimeLimits

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for ConnectionLifetimeLimits

Source§

fn default() -> ConnectionLifetimeLimits

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more