pub struct Server<A, P, S, R, E> { /* private fields */ }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:
Ais the type that can accept connections. This must implementAccept.Pis the protocol to use for serving connections. This must implementProtocol.Sis 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).Ris the request type for the service.Eis 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.
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.
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> 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<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_request<B2>(self) -> Server<A, P, S, B2, E>
pub fn with_request<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<A, P, S, B, E> Server<A, P, S, B, E>where
A: Accept,
impl<A, P, S, B, E> Server<A, P, S, B, E>where
A: Accept,
Sourcepub fn with_tls(
self,
config: Arc<ServerConfig>,
) -> Server<TlsAcceptor<A>, P, S, B, E>
pub fn with_tls( self, config: Arc<ServerConfig>, ) -> Server<TlsAcceptor<A>, P, S, B, E>
Add TLS to this server
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 map_service<F, U>(self, f: F) -> Server<A, P, U, B, E>where
F: FnOnce(S) -> U,
pub fn map_service<F, U>(self, f: F) -> Server<A, P, U, B, E>where
F: FnOnce(S) -> U,
Map the service to a new value.
Source§impl Server<(), (), (), (), ()>
impl Server<(), (), (), (), ()>
Sourcepub fn builder<R>() -> Server<NeedsAcceptor, NeedsProtocol, NeedsService, R, NeedsExecutor>
pub fn builder<R>() -> Server<NeedsAcceptor, NeedsProtocol, NeedsService, R, NeedsExecutor>
Create a new server builder.
Source§impl<A, P, S, R, E> Server<A, P, S, R, E>
impl<A, P, S, R, E> Server<A, P, S, R, E>
Sourcepub fn new(
acceptor: A,
protocol: P,
make_service: S,
executor: E,
) -> Server<A, P, S, R, E>
pub fn new( acceptor: A, protocol: P, make_service: S, executor: E, ) -> Server<A, P, S, R, E>
Create a new server with the given tower::make::MakeService and Accept, and Protocol.
Sourcepub fn with_graceful_shutdown<F>(
self,
signal: F,
) -> GracefulShutdown<A, P, S, R, E, F> ⓘwhere
S: MakeServiceRef<<A as Accept>::Connection, R>,
P: Protocol<<S as MakeServiceRef<<A as Accept>::Connection, R>>::Service, <A as Accept>::Connection, R>,
A: Accept + Unpin,
F: Future<Output = ()> + Send + 'static,
E: ServerExecutor<P, S, A, R>,
pub fn with_graceful_shutdown<F>(
self,
signal: F,
) -> GracefulShutdown<A, P, S, R, E, F> ⓘwhere
S: MakeServiceRef<<A as Accept>::Connection, R>,
P: Protocol<<S as MakeServiceRef<<A as Accept>::Connection, R>>::Service, <A as Accept>::Connection, R>,
A: Accept + Unpin,
F: Future<Output = ()> + Send + 'static,
E: ServerExecutor<P, S, A, R>,
Shutdown the server gracefully when the given future resolves.
Trait Implementations§
Source§impl<A, P, S, R, E> IntoFuture for Server<A, P, S, R, E>where
S: MakeServiceRef<<A as Accept>::Connection, R>,
P: Protocol<<S as MakeServiceRef<<A as Accept>::Connection, R>>::Service, <A as Accept>::Connection, R>,
A: Accept + Unpin,
E: ServerExecutor<P, S, A, R>,
impl<A, P, S, R, E> IntoFuture for Server<A, P, S, R, E>where
S: MakeServiceRef<<A as Accept>::Connection, R>,
P: Protocol<<S as MakeServiceRef<<A as Accept>::Connection, R>>::Service, <A as Accept>::Connection, R>,
A: Accept + Unpin,
E: ServerExecutor<P, S, A, R>,
Source§type IntoFuture = Serving<A, P, S, R, E>
type IntoFuture = Serving<A, P, S, R, E>
Source§fn into_future(self) -> <Server<A, P, S, R, E> as IntoFuture>::IntoFuture
fn into_future(self) -> <Server<A, P, S, R, E> as IntoFuture>::IntoFuture
Source§impl<P, S, B, E> ServerAcceptorExt<P, S, B, E> for Server<NeedsAcceptor, P, S, B, E>
impl<P, S, B, E> ServerAcceptorExt<P, S, B, E> for Server<NeedsAcceptor, P, S, B, E>
Source§fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>
fn with_incoming<I>(self, incoming: I) -> Server<Acceptor, P, S, B, E>
Source§async fn with_bind(
self,
addr: &SocketAddr,
) -> Result<Server<Acceptor, P, S, B, E>, Error>
async fn with_bind( self, addr: &SocketAddr, ) -> Result<Server<Acceptor, P, S, B, E>, Error>
Source§async fn with_listener(
self,
listener: TcpListener,
) -> Server<Acceptor, P, S, B, E>
async fn with_listener( self, listener: TcpListener, ) -> Server<Acceptor, P, S, B, E>
Source§impl<A, P, S, B, E> ServerConnectionInfoExt<A, P, S, B, E> for Server<A, P, S, B, E>
impl<A, P, S, B, E> ServerConnectionInfoExt<A, P, S, B, E> for Server<A, P, S, B, E>
Source§fn with_connection_info(
self,
) -> Server<A, P, MakeServiceConnectionInfoService<S>, B, E>
fn with_connection_info( self, ) -> Server<A, P, MakeServiceConnectionInfoService<S>, B, E>
Source§fn with_tls_connection_info(
self,
) -> Server<A, P, TlsConnectionInfoService<S>, B, E>
fn with_tls_connection_info( self, ) -> Server<A, P, TlsConnectionInfoService<S>, B, E>
Source§impl<A, S, B, E> ServerProtocolExt<A, S, B, E> for Server<A, NeedsProtocol, S, B, E>
impl<A, S, B, E> ServerProtocolExt<A, S, B, E> for Server<A, NeedsProtocol, S, B, E>
Source§fn with_auto_http(self) -> Server<A, Builder, S, B, E>
fn with_auto_http(self) -> Server<A, Builder, S, B, E>
Source§fn with_http1(self) -> Server<A, Http1Builder, S, B, E>
fn with_http1(self) -> Server<A, Http1Builder, S, B, E>
Source§fn with_http2(self) -> Server<A, Http2Builder<TokioExecutor>, S, B, E>
fn with_http2(self) -> Server<A, Http2Builder<TokioExecutor>, S, B, E>
Auto Trait Implementations§
impl<A, P, S, R, E> Freeze for Server<A, P, S, R, E>
impl<A, P, S, R, E> RefUnwindSafe for Server<A, P, S, R, E>
impl<A, P, S, R, E> Send for Server<A, P, S, R, E>
impl<A, P, S, R, E> Sync for Server<A, P, S, R, E>
impl<A, P, S, R, E> Unpin for Server<A, P, S, R, E>
impl<A, P, S, R, E> UnwindSafe for Server<A, P, S, R, 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,
FirstAddrResolver. Read more