Struct glommio::net::UnixListener[][src]

pub struct UnixListener { /* fields omitted */ }
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

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();
});

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();
});

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();
});

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
    }
});

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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