Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Expand description

A running server. Dropping it stops it (with a drain), so keep it and call Server::wait_for_stop to run until a stop is requested.

Implementations§

Source§

impl Server

Source

pub fn open_door(&self) -> Result<(), NetError>

Starts accepting on every listener; a second call is a no-op.

§Errors

ShuttingDown while draining or after shutdown; ForkedChild.

Source

pub fn close_door(&self)

Stops accepting within one loop iteration; listening sockets stay open. Idempotent.

Source

pub fn door(&self) -> DoorState

Whether the server is accepting.

Source

pub fn drain(&self, deadline: Instant) -> DrainReport

Closes the door, tells sessions to finish, closes idle connections at once, waits for the rest until deadline, then kills them with DrainDeadline.

Source

pub fn shutdown(self, deadline: Instant) -> DrainReport

Drains, then closes the listening sockets and joins every thread.

Source

pub fn wait_for_stop(self) -> DrainReport

Blocks until a stop is requested (ServerHandle::request_stop, or the control line moves to Draining or Stopping), then shuts down.

Source

pub fn adopt( &self, conn: TcpConnection, spec: AdoptSpec, ) -> Result<ConnectionId, NetError>

Registers a dialed connection exactly like an accepted one on the listener named in spec: a new id, the caps, Gate::admit with AdmitStage::Adopt, that listener’s limits, the server’s handler or sessions, its kill handle, and its events. For back-channels and outbound connections a server should run and revoke like its own. A gate the connection had from set_gate is closed (SessionClosed) first.

§Errors

Refused { cause } (caps, the gate, Busy; the connection was reset), InvalidConfig { field: "listener" } (no listener with that name), InvalidConfig { field: "connection" } (a TLS connection, or bytes read ahead), ShuttingDown, ForkedChild, Closed.

use net_io::{AdoptSpec, AllowAll, ListenerSpec, ServerBuilder};
let server = ServerBuilder::new()
    .listener(ListenerSpec::tcp("127.0.0.1:7000").name("main"))
    .handler(|conn| { conn.send_all(b"hello, upstream\n")?; Ok(()) })
    .gate(AllowAll)
    .build()?;
let upstream = net_io::connect("10.0.0.9:9000")?;
let id = server.adopt(upstream, AdoptSpec::new("main"))?;
Source

pub fn release_listeners(&self) -> Result<Vec<ListenerHandle>, NetError>

Owned duplicates of every listening socket, for a hand-off to the next process (send_listeners, Unix). This server keeps accepting meanwhile, so the new process can start before this one stops (decision D-042); a later shutdown closes only this server’s copies, and a Unix socket file stays in place for the new process.

§Errors

Os when a socket cannot be duplicated, ForkedChild.

Source

pub fn add_listener( &mut self, spec: ListenerSpec, ) -> Result<ListenerId, NetError>

Binds one more listener on the running server and starts its accept thread. Its door is closed until the next Server::open_door (which opens every listener’s door); the same checks as build apply. The id is new: ids of removed listeners are never reused.

§Errors

As build for the listener; InvalidConfig { field: "listener" } past 64 listeners, ShuttingDown, ForkedChild.

use net_io::{AllowAll, ListenerSpec, ServerBuilder};
let mut server = ServerBuilder::new()
    .listener(ListenerSpec::tcp("127.0.0.1:8080"))
    .handler(|conn| { conn.send_all(b"hi\n")?; Ok(()) })
    .gate(AllowAll)
    .build()?;
server.open_door()?;
let added = server.add_listener(ListenerSpec::tcp("127.0.0.1:8081").name("extra"))?;
server.open_door()?; // opens the new listener's door
Source

pub fn remove_listener( &mut self, id: ListenerId, deadline: Instant, ) -> Result<DrainReport, NetError>

Closes one listener’s socket at once (its connections keep running), then drains them: idle ones close now, the rest may finish until deadline and are then killed (DrainDeadline). Other listeners and their connections are not touched.

§Errors

InvalidConfig { field: "listener", reason: Missing } for an unknown id, ForkedChild.

Source

pub fn handle(&self) -> ServerHandle

A cloneable handle for other threads.

Source

pub fn stats(&self) -> Stats

Counters summed over the workers.

Source

pub fn listeners(&self) -> &[ListenerInfo]

Facts about each listener.

Trait Implementations§

Source§

impl Debug for Server

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Server

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.