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
impl MultiListener
Sourcepub async fn bind<I: IntoIterator<Item = A>, A: ToDualAddr>(
addresses: I,
) -> Result<Self, Error>
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 implementToDualAddr
§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)
Sourcepub async fn accept(&self) -> Result<(DualStream, DualAddr), Error>
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 clientDualAddr: 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.