MultiListener

Struct MultiListener 

Source
pub struct MultiListener {
    pub listeners: Vec<DualListener>,
}
Expand description

A listener that can accept connections on multiple underlying listeners simultaneously.

This struct allows you to bind to multiple addresses (TCP and/or Unix Domain Sockets) and accept connections from any of them. When multiple listeners are ready to accept connections, there is no guarantee which one will be selected first.

§Implementation Details

Internally, this uses futures::future::select_all to wait on all listeners simultaneously, ensuring efficient polling of all underlying listeners.

§Examples

use axum::{Router, routing::get};
use axum_listener::multi::MultiListener;

let router = Router::new().route("/", get(|| async { "Hello, World!" }));

// Bind to multiple TCP addresses
let addresses = ["127.0.0.1:8080", "127.0.0.1:8081"];
let listener = MultiListener::bind(addresses).await.unwrap();
axum::serve(listener, router).await.unwrap();
use axum_listener::multi::MultiListener;

// Mix TCP and Unix Domain Socket addresses (on Unix systems)
let addresses = ["localhost:8080", "unix:/tmp/app.sock"];
let listener = MultiListener::bind(addresses).await.unwrap();

Fields§

§listeners: Vec<DualListener>

The underlying listeners that this multi-listener manages

Implementations§

Source§

impl MultiListener

Source

pub async fn bind<I: IntoIterator<Item = A>, A: ToDualAddr>( addresses: I, ) -> Result<Self, Error>

Creates a new MultiListener bound to multiple addresses.

This method attempts to bind to all provided addresses simultaneously. If any of the bindings fail, the entire operation fails and returns an error. All addresses must be successfully bound for this method to succeed.

§Arguments
  • addresses - An iterable collection of addresses that implement ToDualAddr
§Returns

Returns a MultiListener bound to all specified addresses, or an error if any binding fails.

§Examples
use axum_listener::multi::MultiListener;

// Bind to multiple TCP ports
let tcp_addresses = ["127.0.0.1:8080", "127.0.0.1:8081", "127.0.0.1:8082"];
let listener = MultiListener::bind(tcp_addresses).await.unwrap();
use axum_listener::multi::MultiListener;

// Mix TCP and Unix Domain Socket addresses
let mixed_addresses = ["localhost:8080", "unix:/tmp/app.sock"];
let listener = MultiListener::bind(mixed_addresses).await.unwrap();
§Errors

This method can fail if:

  • Any address format is invalid
  • Any address is already in use
  • Permission is denied for any requested address
  • Unix Domain Sockets are not supported on the current platform
  • The provided iterator is empty (no addresses to bind to)
Source

pub async fn accept(&self) -> Result<(DualStream, DualAddr), Error>

Accepts a new incoming connection from any of the underlying listeners.

This method waits for a connection to be established on any of the bound listeners and returns the first one that becomes available. The selection is non-deterministic when multiple listeners are ready simultaneously.

§Returns

Returns a tuple containing:

  • DualStream: The stream for communicating with the client
  • DualAddr: The address of the connected client
§Examples
use axum_listener::multi::MultiListener;

let addresses = ["127.0.0.1:8080", "127.0.0.1:8081"];
let listener = MultiListener::bind(addresses).await.unwrap();

// Accept a connection from any of the bound addresses
let (stream, addr) = listener.accept().await.unwrap();
println!("Accepted connection from: {:?}", addr);
§Errors

This method can fail if there’s an I/O error while accepting a connection from any of the underlying listeners.

Trait Implementations§

Source§

impl Listener for MultiListener

Source§

type Io = DualStream

The listener’s IO type.
Source§

type Addr = MultiAddr

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§

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

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