Skip to main content

MaxConnListener

Struct MaxConnListener 

Source
pub struct MaxConnListener<L> { /* private fields */ }
Expand description

Wrap any axum::serve::Listener with a hard cap on concurrent connections.

axum::serve exposes no connection cap of its own, so the limit lives here as a listener adapter. Each accepted connection holds one permit for its whole lifetime; the permit releases when the connection IO is dropped. When the permits are exhausted, accept waits before pulling the next connection off the OS queue, which back-pressures the accept loop instead of unbounded-buffering new sockets. The permit semaphore is never closed, so a drain does not revoke the permits of connections that are still finishing.

This wrapper changes the listener’s accepted IO type, which moves it out of axum’s built-in Connected<_> for SocketAddr impl (that impl is bound to the concrete TcpListener, and coherence forbids re-adding one for the foreign SocketAddr). A site that still needs the peer address serves into_make_service_with_connect_info::<CappedPeerAddr>() and reads ConnectInfo<CappedPeerAddr>, so it keeps both the accept cap and connect info.

Implementations§

Source§

impl<L> MaxConnListener<L>

Source

pub fn new(inner: L, max_connections: usize) -> Self

Cap inner at max_connections simultaneously accepted connections.

usize::MAX is a valid “effectively uncapped” sentinel: the count is clamped to Semaphore::MAX_PERMITS so a large value is a no-op cap rather than a startup panic.

Trait Implementations§

Source§

impl<L> Listener for MaxConnListener<L>
where L: Listener, L::Io: Unpin,

Source§

type Io = PermittedIo<<L as Listener>::Io>

The listener’s IO type.
Source§

type Addr = <L as Listener>::Addr

The listener’s address type.
Source§

async fn accept(&mut self) -> (Self::Io, Self::Addr)

Accept a new incoming connection to this listener. Read more
Source§

fn local_addr(&self) -> Result<Self::Addr>

Returns the local address that this listener is bound to.

Auto Trait Implementations§

§

impl<L> !RefUnwindSafe for MaxConnListener<L>

§

impl<L> !UnwindSafe for MaxConnListener<L>

§

impl<L> Freeze for MaxConnListener<L>
where L: Freeze,

§

impl<L> Send for MaxConnListener<L>
where L: Send,

§

impl<L> Sync for MaxConnListener<L>
where L: Sync,

§

impl<L> Unpin for MaxConnListener<L>
where L: Unpin,

§

impl<L> UnsafeUnpin for MaxConnListener<L>
where L: UnsafeUnpin,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

impl<T> Instrument for T

Source§

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

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

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<L> ListenerExt for L
where L: Listener,

Source§

fn tap_io<F>(self, tap_fn: F) -> TapIo<Self, F>
where F: FnMut(&mut Self::Io) + Send + 'static,

Run a mutable closure on every accepted Io. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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