HttpServer

Struct HttpServer 

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

An HTTP server.

Serves a set of routes (HTTP or gRPC) over a connection-oriented listener, optionally with TLS.

§Routes

Routes are accumulated on the server itself: HTTP routes via add_routes, gRPC services via add_grpc_service. Both can be called as many times as needed, and both feed the same router, because a gRPC service is a route set like any other – one whose paths follow the gRPC naming convention. The final router is built once, when the server is converted into a supervisor.

The server will respond accordingly depending on whether or not at least one gRPC service was configured. For example, when an unknown gRPC service/operation is called, it will receive a gRPC-specific response indicating as such, rather than a generic HTTP “404 Not Found” response.

A caller that has already built the exact router it wants can hand it over with with_routes, which bypasses all of the above.

§Supervision

The server must be run under supervision, and can be converted to a Supervisor to do so. A number of child workers handle various aspects of the server and connection lifecycle:

  • A dedicated worker accepts new connections from the configured listener.
  • For TLS-enabled servers, an interstitial worker is spawned to handle the initial TLS handshake, which helps avoid head-of-line blocking when accepting subsequent connections due to the amount of time it can take to perform the TLS handshake.
  • Connections are driven on their own worker for isolation. - Background tasks (related to HTTP/2) may also be spawned as individual workers.

§Shutdown

The subtree carries its own shutdown budget, because a nested supervisor is deliberately exempt from its parent’s. See with_graceful_shutdown_timeout for what sets it and what the default is.

§Assertions

HttpServer can optionally assert particular information at runtime when the server identifier is set (see with_server_id):

  • the bound listen address (BoundListenAddress, with an identifier of http-server-<server ID>)

Implementations§

Source§

impl HttpServer

Source

pub fn from_listen_address(listen_address: ListenAddress) -> Self

Creates a server that will listen on the given address, with no routes attached.

Source

pub fn add_routes(self, routes: Router) -> Self

Adds HTTP routes to this server.

Can be called more than once, in which case the route sets are merged.

§Panics

Panics if routes defines the same path as an existing route on this server.

Source

pub fn add_grpc_service<S>(self, service: S) -> Self
where S: Service<Request<GrpcBody>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static, S::Response: IntoResponse, S::Future: Send + 'static,

Adds a gRPC service to this server.

The service’s routes are served from the same listener, and alongside the same HTTP routes, as everything else attached to this server.

Can be called more than once to attach several services.

Source

pub fn with_routes(self, routes: Router) -> Self

Serves the given router, ignoring any routes otherwise attached to this server.

Any existing routes, whether HTTP or gRPC, will be ignored entirely.

Source

pub fn with_http2_config(self, config: Http2Config) -> Self

Sets the HTTP/2 settings for the server.

Defaults to Http2Config::default(), which enables neither keepalive nor a connection age limit.

Source

pub fn with_http2_only(self) -> Self

Restricts the server to HTTP/2.

By default, the protocol is detected per connection: a client that opens with the HTTP/2 connection preface is served over HTTP/2, and anything else is served over HTTP/1.1. Restricting the server to HTTP/2 skips that detection, so an HTTP/1.1 client is rejected at the protocol level rather than being routed and answered.

This is worth setting on an endpoint that only ever serves gRPC, where an HTTP/1.1 request is a client error worth surfacing as one. Leave it off for any endpoint that also serves REST-ful routes.

Defaults to accepting both HTTP/1.1 and HTTP/2.

Source

pub fn with_server_id(self, id: impl Into<MetaString>) -> Self

Sets the server identifier to use when asserting any facts for this server.

The identifier also distinguishes this server from any other running under the same supervisor: it becomes part of the name the subtree reports, so that logs and per-worker task metrics can be attributed to a specific endpoint. Set it on any server that shares a supervisor with another, even when nothing consumes the assertions.

If no identifier is set, no assertions will be made at runtime, and the subtree reports a bare http_server.

Source

pub fn with_graceful_shutdown_timeout(self, timeout: Duration) -> Self

Sets how long an individual connection is given to drain during shutdown.

When shutdown is signalled, a connection stops accepting new requests and finishes what is already in flight. This bounds that: a connection that hasn’t finished by then gives up and closes, logging a warning, so one wedged peer can’t hold the subtree open.

It also sets the subtree’s shutdown budget, to this value plus a small amount of slack. That matters because a nested supervisor is exempt from its parent’s budget, so without one of its own nothing would bound the subtree at all. Connections bound themselves, so the budget only comes into play for one that ignores its own deadline.

Defaults to 30 seconds, matching the default shutdown timeout a topology gives its components. Lower it for an endpoint that should be abandoned quickly; raise it for one serving long-running requests that are worth waiting for.

Source

pub fn with_tls_config(self, config: ServerConfig) -> Self

Sets the TLS configuration for the server.

This enables TLS, after which the server only accepts connections that are encrypted with TLS.

Defaults to TLS being disabled.

Source

pub fn with_worker_pool(self, handle: Handle) -> Self

Runs this server’s tasks on the given runtime.

Every task the server runs is placed here: accepting connections, TLS handshakes, serving connections, and the futures hyper hands to the server’s executor. Only the subtree’s own supervisor loop stays on the runtime it was spawned on, since that is where children are registered rather than run.

Use this to keep the server off the runtime that its owner runs on – a topology component’s server belongs on the shared worker pool rather than on the runtime driving the topology, since request handling and TLS handshake crypto are both compute-heavy enough to add scheduling latency to everything else there.

Defaults to running on whichever runtime the subtree was spawned on.

Source

pub fn into_supervisor(self) -> Supervisor

Converts this server into a supervisor.

The supervisor is configured to drive an accept loop on the configured listen address, and any resulting connections will be spawned and handled on the supervisor.

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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Track for T

Source§

fn track_resources(self, token: ResourceGroupToken) -> Tracked<Self>

Instruments this type by attaching the given resource group token, returning a Tracked wrapper. Read more
Source§

fn in_current_resource_group(self) -> Tracked<Self>

Instruments this type by attaching the current resource group, returning a Tracked wrapper. Read more
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