Struct Server

Source
pub struct Server<A, P, S, B, E> { /* private fields */ }
Available on crate feature server only.
Expand description

A server that can accept connections, and run each connection using a tower::Service.

To use the server, call .await on it. This will start the server and serve until the future is cancelled or the acceptor encounters an error. To cancel the server, drop the future.

The server also supports graceful shutdown. Provide a future which will resolve when the server should shut down to Server::with_graceful_shutdown to enable this behavior. In graceful shutdown mode, individual connections will have an opportunity to finish processing before the server stops.

The generic parameters can be a bit tricky. They are as follows:

  • A is the type that can accept connections. This must implement Accept.
  • P is the protocol to use for serving connections. This must implement Protocol.
  • S is the “make service” which generates a service to handle each connection. This must implement MakeServiceRef, and will be passed a reference to the connection stream (to facilitate connection information).
  • B is the body type for the service.
  • E is the executor to use for running the server. This is used to spawn the connection futures.

Implementations§

Source§

impl<P, S, B, E> Server<NeedsAcceptor, P, S, B, E>

Source

pub fn with_acceptor<A>(self, acceptor: A) -> Server<A, P, S, B, E>
where A: Accept,

Set the acceptor to use for incoming connections.

Source

pub fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>

Available on crate feature stream only.

Use an incoming stream of connections as the acceptor.

This is a convenience method that constructs an Acceptor from the provided stream of connections. It works with tokio::net::TcpListener, tokio::net::UnixListener.

Source

pub async fn with_bind( self, addr: &SocketAddr, ) -> Result<Server<Acceptor, P, S, B, E>, Error>

Available on crate feature stream only.

Bind to the provided address and use it as the acceptor.

Source

pub async fn with_listener( self, listener: TcpListener, ) -> Server<Acceptor, P, S, B, E>

Available on crate feature stream only.

Use the provided listener as the acceptor.

Source§

impl<A, S, B, E> Server<A, NeedsProtocol, S, B, E>

Source

pub fn with_protocol<P>(self, protocol: P) -> Server<A, P, S, B, E>

Set the protocol to use for incoming connections.

Source

pub fn with_auto_http(self) -> Server<A, Builder, S, B, E>

Use a protocol that automatically detects and selects between HTTP/1.1 and HTTP/2, by looking for the HTTP/2 header in the initial bytes of the connection.

Source

pub fn with_http1(self) -> Server<A, Builder, S, B, E>

Use HTTP/1.1 for all incoming connections.

Source

pub fn with_http2(self) -> Server<A, Builder<TokioExecutor>, S, B, E>

Use HTTP/2 for all incoming connections.

Source§

impl<A, P, B, E> Server<A, P, NeedsService, B, E>

Source

pub fn with_make_service<S>(self, make_service: S) -> Server<A, P, S, B, E>

Set the make service to use for handling incoming connections.

A MakeService is a factory for creating Service instances. It is used to create a new Service for each incoming connection.

If you have a service that is Clone, you can use with_shared_service to wrap it in a Shared and avoid constructing a new make service.

Source

pub fn with_shared_service<S>(self, service: S) -> Server<A, P, Shared<S>, B, E>

Wrap a Clone service in a Shared to use as a make service.

Source§

impl<A, P, S, B, E> Server<A, P, S, B, E>
where S: MakeServiceRef<A::Conn, B>, A: Accept,

Source

pub fn with_connection_info( self, ) -> Server<A, P, MakeServiceConnectionInfoService<S>, B, E>

Wrap the make service in a service that provides connection information.

This will make crate::info::ConnectionInfo<A> available in the request extensions for each request handled by the generated service.

Source

pub fn with_tls_connection_info( self, ) -> Server<A, P, TlsConnectionInfoService<S>, B, E>

Available on crate feature tls only.

Wrap the make service in a service that provides TLS connection information.

This will make crate::info::TlsConnectionInfo available in the request extensions for each request handled by the generated service.

Source§

impl<P, S, B, E> Server<Acceptor, P, S, B, E>

Source

pub fn with_tls<C>(self, config: C) -> Server<Acceptor, P, S, B, E>
where C: Into<Arc<ServerConfig>>,

Available on crate features tls and stream only.

Use the provided rustls::ServerConfig to configure TLS for incoming connections.

Source§

impl<A, P, S, B> Server<A, P, S, B, NeedsExecutor>

Source

pub fn with_executor<E>(self, executor: E) -> Server<A, P, S, B, E>

Set the executor for this server

The executor is used to drive connections to completion asynchronously.

Source

pub fn with_tokio(self) -> Server<A, P, S, B, TokioExecutor>

Use a tokio multi-threaded excutor via tokio::task::spawn

This executor is a suitable default, but does require Send and ’static bounds in some places to allow futures to be moved between threads.

Con

Source§

impl<A, P, S, B, E> Server<A, P, S, B, E>

Source

pub fn with_body<B2>(self) -> Server<A, P, S, B2, E>

Set the body to use for handling requests.

Usually this method can be called with inferred types.

Source§

impl Server<(), (), (), (), ()>

Source

pub fn builder<B>() -> Server<NeedsAcceptor, NeedsProtocol, NeedsService, B, NeedsExecutor>

Create a new server builder.

To build a simple server, you can use the with_shared_service method:

use hyperdriver::stream::duplex;
use hyperdriver::Server;
use hyperdriver::Body;
use tower::service_fn;

#[derive(Debug)]
struct MyError;

impl std::fmt::Display for MyError {
   fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("MyError")
  }
}

impl std::error::Error for MyError {}

let (_, incoming) = duplex::pair();
let server = Server::builder()
    .with_acceptor(incoming)
    .with_shared_service(service_fn(|req: http::Request<Body>| async move {
       Ok::<_, MyError>(http::Response::new(Body::empty()))
   }))
   .with_auto_http()
   .with_tokio();

server.await.unwrap();
Source§

impl<A, P, S, B, E> Server<A, P, S, B, E>

Source

pub fn new(acceptor: A, protocol: P, make_service: S, executor: E) -> Self

Create a new server with the given MakeService and Acceptor, and a custom Protocol.

The default protocol is AutoBuilder, which can serve both HTTP/1 and HTTP/2 connections, and will automatically detect the protocol used by the client.

Source

pub fn with_graceful_shutdown<F>( self, signal: F, ) -> GracefulShutdown<A, P, S, B, E, F>
where S: MakeServiceRef<A::Conn, B>, P: Protocol<S::Service, A::Conn, B>, A: Accept + Unpin, B: Body, F: Future<Output = ()> + Send + 'static, E: ServerExecutor<P, S, A, B>,

Shutdown the server gracefully when the given future resolves.

§Example
use hyperdriver::stream::duplex;
use hyperdriver::Server;
use hyperdriver::Body;
use tower::service_fn;

let (_, incoming) = duplex::pair();
let server = Server::builder()
    .with_acceptor(incoming)
    .with_shared_service(service_fn(|req: http::Request<Body>| async move {
       Ok::<_, MyError>(http::Response::new(Body::empty()))
   }))
   .with_auto_http()
   .with_tokio()
   .with_graceful_shutdown(async { let _ = tokio::signal::ctrl_c().await; }).await.unwrap();
Source§

impl<S, B> Server<Acceptor, AutoBuilder<TokioExecutor>, S, B, TokioExecutor>

Source

pub async fn bind<A: ToSocketAddrs>(addr: A, make_service: S) -> Result<Self>

Available on crate feature stream only.

Bind a new server to the given address.

Trait Implementations§

Source§

impl<A, P, S, B, E> Debug for Server<A, P, S, B, E>

Source§

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

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

impl<A, P, S, B, E> IntoFuture for Server<A, P, S, B, E>
where S: MakeServiceRef<A::Conn, B>, P: Protocol<S::Service, A::Conn, B>, A: Accept + Unpin, B: Body, E: ServerExecutor<P, S, A, B>,

Source§

type IntoFuture = Serving<A, P, S, B, E>

Which kind of future are we turning this into?
Source§

type Output = Result<(), ServerError>

The output that the future will produce on completion.
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

Auto Trait Implementations§

§

impl<A, P, S, B, E> Freeze for Server<A, P, S, B, E>
where A: Freeze, P: Freeze, S: Freeze, E: Freeze,

§

impl<A, P, S, B, E> RefUnwindSafe for Server<A, P, S, B, E>

§

impl<A, P, S, B, E> Send for Server<A, P, S, B, E>
where A: Send, P: Send, S: Send, E: Send,

§

impl<A, P, S, B, E> Sync for Server<A, P, S, B, E>
where A: Sync, P: Sync, S: Sync, E: Sync,

§

impl<A, P, S, B, E> Unpin for Server<A, P, S, B, E>
where A: Unpin, P: Unpin, S: Unpin, E: Unpin,

§

impl<A, P, S, B, E> UnwindSafe for Server<A, P, S, B, E>

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<R> FirstAddrExt for R

Source§

fn first_addr(self) -> FirstAddrResolver<Self>
where Self: Sized,

Available on crate feature client only.
Convert this resolver into a FirstAddrResolver. 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, 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<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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,