Struct Server

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

Serve an app over HTTP.

Implementations§

Source§

impl<State> Server<State>
where State: Send + Sync + 'static,

Source

pub fn new(app: App<State>) -> Self

Creates a new server for the provided app.

Source

pub fn listen<A: ToSocketAddrs>( self, address: A, ) -> impl Future<Output = Result<ExitCode, Box<dyn Error + Send + Sync>>>

Starts the server and listens for incoming connections at the provided address. Returns a future that resolves when the server is shutdown.

§Errors

If the server fails to bind to the provided address or is unable to finish serving all of the inflight connections within the specified shutdown timeout when a shutdown signal is received.

§Exit Codes

An ExitCode::SUCCESS can be viewed as a confirmation that every request was served before exiting the accept loop.

An ExitCode::FAILURE is an indicator that an unrecoverable error occured which requires that the server be restarted in order to function as intended.

If you are running your Via application as a daemon with a process supervisor such as upstart or systemd, you can use the exit code to determine whether or not the process should restart.

If you are running your Via application in a cluster behind a load balancer you can use the exit code to properly configure node replacement and / or decommissioning logic.

When high availability is mission-critical, and you are scaling your Via application both horizontally and vertically using a combination of the aforementioned deployment strategies, we recommend configuring a temporal threshold for the number of restarts caused by an ExitCode::FAILURE. If the threshold is exceeded the cluster should immutably replace the node and the process supervisor should not make further attempts to restart the process.

This approach significantly reduces the impact of environmental entropy on your application’s availability while preventing conflicts between the process supervisor of an individual node and the replacement and decommissioning logic of the cluster.

Source

pub fn shutdown_timeout(self, timeout: u64) -> Self

Set the amount of time in seconds that the server will wait for inflight connections to complete before shutting down. The default value is 30 seconds.

Source

pub fn max_request_size(self, limit: usize) -> Self

Source

pub fn max_connections(self, limit: usize) -> Self

Sets the maximum number of concurrent connections that the server can accept. The default value is 256.

We suggest not setting this value unless you know what you are doing and have a good reason to do so. If you are unsure, it is best to leave this value at the default and scale horizontally.

If you do set this value, we suggest doing so by profiling the stack size of your application when it’s under load and incrementally increasing this value until you find a balance between performance and worry-free programming. In other words, the closer this value is to the limit based on your application’s stack consumption and the stack size of your server, the more careful you will need to be when allocating values on the stack (i.e dereferencing a heap pointer). Otherwise, you may encounter a stack overflow. In addition to the stack size, you should also consider not setting this value higher than the number of available file descriptors (or ulimit -n) on POSIX systems.

Auto Trait Implementations§

§

impl<T> !Freeze for Server<T>

§

impl<T> !RefUnwindSafe for Server<T>

§

impl<T> Send for Server<T>
where T: Send,

§

impl<T> Sync for Server<T>
where T: Sync,

§

impl<T> Unpin for Server<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Server<T>

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 = 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.