pub struct Server<A, P, S, B, E> { /* private fields */ }
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 implementAccept
.P
is the protocol to use for serving connections. This must implementProtocol
.S
is the “make service” which generates a service to handle each connection. This must implementMakeServiceRef
, 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>
impl<P, S, B, E> Server<NeedsAcceptor, P, S, B, E>
Sourcepub fn with_acceptor<A>(self, acceptor: A) -> Server<A, P, S, B, E>where
A: Accept,
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.
Sourcepub fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>
Available on crate feature stream
only.
pub fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>
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
.
Sourcepub async fn with_bind(
self,
addr: &SocketAddr,
) -> Result<Server<Acceptor, P, S, B, E>, Error>
Available on crate feature stream
only.
pub async fn with_bind( self, addr: &SocketAddr, ) -> Result<Server<Acceptor, P, S, B, E>, Error>
stream
only.Bind to the provided address and use it as the acceptor.
Sourcepub async fn with_listener(
self,
listener: TcpListener,
) -> Server<Acceptor, P, S, B, E>
Available on crate feature stream
only.
pub async fn with_listener( self, listener: TcpListener, ) -> Server<Acceptor, P, S, B, E>
stream
only.Use the provided listener as the acceptor.
Source§impl<A, S, B, E> Server<A, NeedsProtocol, S, B, E>
impl<A, S, B, E> Server<A, NeedsProtocol, S, B, E>
Sourcepub fn with_protocol<P>(self, protocol: P) -> Server<A, P, S, B, E>
pub fn with_protocol<P>(self, protocol: P) -> Server<A, P, S, B, E>
Set the protocol to use for incoming connections.
Sourcepub fn with_auto_http(self) -> Server<A, Builder, S, B, E>
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.
Sourcepub fn with_http1(self) -> Server<A, Builder, S, B, E>
pub fn with_http1(self) -> Server<A, Builder, S, B, E>
Use HTTP/1.1 for all incoming connections.
Sourcepub fn with_http2(self) -> Server<A, Builder<TokioExecutor>, S, B, E>
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>
impl<A, P, B, E> Server<A, P, NeedsService, B, E>
Sourcepub fn with_make_service<S>(self, make_service: S) -> Server<A, P, S, B, E>
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.
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>
impl<A, P, S, B, E> Server<A, P, S, B, E>
Sourcepub fn with_connection_info(
self,
) -> Server<A, P, MakeServiceConnectionInfoService<S>, B, E>
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.
Sourcepub fn with_tls_connection_info(
self,
) -> Server<A, P, TlsConnectionInfoService<S>, B, E>
Available on crate feature tls
only.
pub fn with_tls_connection_info( self, ) -> Server<A, P, TlsConnectionInfoService<S>, B, E>
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<A, P, S, B> Server<A, P, S, B, NeedsExecutor>
impl<A, P, S, B> Server<A, P, S, B, NeedsExecutor>
Sourcepub fn with_executor<E>(self, executor: E) -> Server<A, P, S, B, E>
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.
Sourcepub fn with_tokio(self) -> Server<A, P, S, B, TokioExecutor>
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 Server<(), (), (), (), ()>
impl Server<(), (), (), (), ()>
Sourcepub fn builder<B>() -> Server<NeedsAcceptor, NeedsProtocol, NeedsService, B, NeedsExecutor>
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>
impl<A, P, S, B, E> Server<A, P, S, B, E>
Sourcepub fn new(acceptor: A, protocol: P, make_service: S, executor: E) -> Self
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.
Sourcepub fn with_graceful_shutdown<F>(
self,
signal: F,
) -> GracefulShutdown<A, P, S, B, E, F> ⓘ
pub fn with_graceful_shutdown<F>( self, signal: F, ) -> GracefulShutdown<A, P, S, B, E, F> ⓘ
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>
impl<S, B> Server<Acceptor, AutoBuilder<TokioExecutor>, S, B, TokioExecutor>
Sourcepub async fn bind<A: ToSocketAddrs>(addr: A, make_service: S) -> Result<Self>
Available on crate feature stream
only.
pub async fn bind<A: ToSocketAddrs>(addr: A, make_service: S) -> Result<Self>
stream
only.Bind a new server to the given address.
Trait Implementations§
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>,
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>
type IntoFuture = Serving<A, P, S, B, E>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Auto Trait Implementations§
impl<A, P, S, B, E> Freeze for Server<A, P, S, B, E>
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>
impl<A, P, S, B, E> Sync for Server<A, P, S, B, E>
impl<A, P, S, B, E> Unpin for Server<A, P, S, B, E>
impl<A, P, S, B, E> UnwindSafe for Server<A, P, S, B, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> FirstAddrExt for R
impl<R> FirstAddrExt for R
Source§fn first_addr(self) -> FirstAddrResolver<Self>where
Self: Sized,
fn first_addr(self) -> FirstAddrResolver<Self>where
Self: Sized,
client
only.FirstAddrResolver
. Read more