Skip to main content

BackgroundServer

Struct BackgroundServer 

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

A managed, background HTTP server returned by Server::serve_managed.

The server starts as soon as serve_managed is called. Use this handle to:

  • addr — query the current bind address
  • rebind — gracefully migrate to a new address
  • mechanism — add a route live, no restart
  • stop — shut down and await the task

§Routes are preserved across rebind

All routes registered before serve_managed, plus any added via mechanism, are automatically carried over when rebind restarts the server.

§Ownership

Dropping a BackgroundServer without calling stop leaves the background task running until the Tokio runtime exits.

§Example

let mut server = Server::default();
server.mechanism(
    toolkit_zero::socket::server::ServerMechanism::get("/ping")
        .onconnect(|| async { reply!(json => Pong { ok: true }) })
);
let mut bg = server.serve_managed(([127, 0, 0, 1], 8080));
assert_eq!(bg.addr().port(), 8080);

bg.rebind(([127, 0, 0, 1], 9090)).await;
assert_eq!(bg.addr().port(), 9090);

bg.stop().await;

Implementations§

Source§

impl BackgroundServer

Source

pub fn addr(&self) -> SocketAddr

Returns the address the server is currently bound to.

Source

pub async fn stop(self)

Shuts the server down gracefully and awaits the background task.

In-flight requests complete before the server stops.

Source

pub async fn rebind(&mut self, addr: impl Into<SocketAddr>)

Migrates the server to addr with zero route loss:

  1. Sends a graceful shutdown signal to the current instance.
  2. Waits for all in-flight requests to complete.
  3. Spawns a fresh server task on the new address with the same routes.

After this method returns, addr reflects the new address and the server is accepting connections.

§Example
let mut bg = server.serve_managed(([127, 0, 0, 1], 8080));

bg.rebind(([127, 0, 0, 1], 9090)).await;
assert_eq!(bg.addr().port(), 9090);

bg.stop().await;
Source

pub async fn mechanism(&mut self, mech: SocketType) -> &mut Self

Registers a new route on the running server without any restart.

Because routes are stored in an Arc<RwLock<Vec<SocketType>>> shared between this handle and the server’s dispatch loop, writing through the lock makes the new route visible to the next incoming request immediately — no TCP port gap, no in-flight request interruption.

Returns &mut Self for chaining.

§Example
use toolkit_zero::socket::server::{Server, ServerMechanism, reply};
use serde::Serialize;

#[derive(Serialize)] struct Pong   { ok:  bool   }
#[derive(Serialize)] struct Status { msg: String }

let mut server = Server::default();
server.mechanism(
    ServerMechanism::get("/ping")
        .onconnect(|| async { reply!(json => Pong { ok: true }) })
);

let mut bg = server.serve_managed(([127, 0, 0, 1], 8080));

bg.mechanism(
    ServerMechanism::get("/status")
        .onconnect(|| async { reply!(json => Status { msg: "enabled".into() }) })
).await;

// /status is now live alongside /ping — no restart.
bg.stop().await;

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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