Struct glommio::net::UnixListener

source ·
pub struct UnixListener { /* private fields */ }
Expand description

A Unix socket server, listening for connections.

After creating a UnixListener by binding it to a socket address, it listens for incoming Unix connections. These can be accepted by calling accept or shared_accept, or by iterating over the Incoming iterator returned by incoming.

A good networking architecture within a thread-per-core model needs to take into account parallelism and spawn work into multiple executors. If everything happens inside the same Executor, then at most one thread is used. Sometimes this is what you want: you may want to dedicate a CPU entirely for networking, or even use specialized ports for each CPU of the application, but most likely it isn’t.

There are so far only one approach to load balancing possible with the UnixListener:

The socket will be closed when the value is dropped.

Implementations§

source§

impl UnixListener

source

pub fn bind<A: AsRef<Path>>(addr: A) -> Result<UnixListener, ()>

Creates a Unix listener bound to the specified address.

Binding with port number 0 will request an available port from the OS.

This method sets the ReusePort option in the bound socket, so it is designed to be called from multiple executors to achieve parallelism.

§Examples
use glommio::{net::UnixListener, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let _listener = UnixListener::bind("/tmp/named").unwrap();
});
source

pub async fn shared_accept(&self) -> Result<AcceptedUnixStream, ()>

Accepts a new incoming Unix connection and allows the result to be sent to a foreign executor

This is similar to accept, except it returns an AcceptedUnixStream instead of a UnixStream. AcceptedUnixStream implements Send, so it can be safely sent for processing over a shared channel to a different executor.

This is useful when the user wants to do her own load balancing across multiple executors instead of relying on the load balancing the OS would do with the ReusePort property of the bound socket.

§Examples
use glommio::{net::UnixListener, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let listener = UnixListener::bind("/tmp/named").unwrap();
    let _stream = listener.shared_accept().await.unwrap();
});
source

pub async fn accept(&self) -> Result<UnixStream, ()>

Accepts a new incoming Unix connection in this executor

This is similar to calling shared_accept and bind_to_executor in a single operation.

If this connection once accepted is to be handled by the same executor in which it was accepted, this version is preferred.

§Examples
use futures_lite::stream::StreamExt;
use glommio::{net::UnixListener, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let listener = UnixListener::bind("/tmp/named").unwrap();
    let _stream = listener.accept().await.unwrap();
});
source

pub fn incoming( &self ) -> impl Stream<Item = Result<UnixStream, ()>> + Unpin + '_

Creates a stream of incoming connections

§Examples
use futures_lite::stream::StreamExt;
use glommio::{net::UnixListener, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let listener = UnixListener::bind("/tmp/named").unwrap();
    let mut incoming = listener.incoming();
    while let Some(conn) = incoming.next().await {
        // handle
    }
});
source

pub fn local_addr(&self) -> Result<SocketAddr, ()>

Returns the socket address of the local half of this Unix connection.

§Examples
use glommio::{net::UnixListener, LocalExecutor};

let ex = LocalExecutor::default();
ex.run(async move {
    let listener = UnixListener::bind("/tmp/named").unwrap();
    println!("Listening on {:?}", listener.local_addr().unwrap());
});

Trait Implementations§

source§

impl Debug for UnixListener

source§

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

Formats the value using the given formatter. 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> 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<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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>,

§

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