pub struct Server<L = Identity> { /* private fields */ }
Expand description
A default batteries included transport
server.
This is a wrapper around hyper::Server
and provides an easy builder
pattern style builder Server
. This builder exposes easy configuration parameters
for providing a fully featured http2 based gRPC server. This should provide
a very good out of the box http2 server for use with tonic but is also a
reference implementation that should be a good starting point for anyone
wanting to create a more complex and/or specific implementation.
Implementations
sourceimpl<L> Server<L>
impl<L> Server<L>
sourcepub fn tls_config(self, tls_config: ServerTlsConfig) -> Result<Self, Error>
pub fn tls_config(self, tls_config: ServerTlsConfig) -> Result<Self, Error>
Configure TLS for this server.
sourcepub fn concurrency_limit_per_connection(self, limit: usize) -> Self
pub fn concurrency_limit_per_connection(self, limit: usize) -> Self
Set the concurrency limit applied to on requests inbound per connection.
Example
builder.concurrency_limit_per_connection(32);
sourcepub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Self
pub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Self
Sets the SETTINGS_INITIAL_WINDOW_SIZE
option for HTTP2
stream-level flow control.
Default is 65,535
sourcepub fn initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> Self
pub fn initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> Self
Sets the max connection-level flow control for HTTP2
Default is 65,535
sourcepub fn max_concurrent_streams(self, max: impl Into<Option<u32>>) -> Self
pub fn max_concurrent_streams(self, max: impl Into<Option<u32>>) -> Self
Sets the SETTINGS_MAX_CONCURRENT_STREAMS
option for HTTP2
connections.
Default is no limit (None
).
sourcepub fn http2_keepalive_interval(
self,
http2_keepalive_interval: Option<Duration>
) -> Self
pub fn http2_keepalive_interval(
self,
http2_keepalive_interval: Option<Duration>
) -> Self
Set whether HTTP2 Ping frames are enabled on accepted connections.
If None
is specified, HTTP2 keepalive is disabled, otherwise the duration
specified will be the time interval between HTTP2 Ping frames.
The timeout for receiving an acknowledgement of the keepalive ping
can be set with Server::http2_keepalive_timeout
.
Default is no HTTP2 keepalive (None
)
sourcepub fn http2_keepalive_timeout(
self,
http2_keepalive_timeout: Option<Duration>
) -> Self
pub fn http2_keepalive_timeout(
self,
http2_keepalive_timeout: Option<Duration>
) -> Self
Sets a timeout for receiving an acknowledgement of the keepalive ping.
If the ping is not acknowledged within the timeout, the connection will be closed. Does nothing if http2_keep_alive_interval is disabled.
Default is 20 seconds.
sourcepub fn tcp_keepalive(self, tcp_keepalive: Option<Duration>) -> Self
pub fn tcp_keepalive(self, tcp_keepalive: Option<Duration>) -> Self
Set whether TCP keepalive messages are enabled on accepted connections.
If None
is specified, keepalive is disabled, otherwise the duration
specified will be the time to remain idle before sending TCP keepalive
probes.
Default is no keepalive (None
)
sourcepub fn tcp_nodelay(self, enabled: bool) -> Self
pub fn tcp_nodelay(self, enabled: bool) -> Self
Set the value of TCP_NODELAY
option for accepted connections. Enabled by default.
sourcepub fn max_frame_size(self, frame_size: impl Into<Option<u32>>) -> Self
pub fn max_frame_size(self, frame_size: impl Into<Option<u32>>) -> Self
Sets the maximum frame size to use for HTTP2.
Passing None
will do nothing.
If not set, will default from underlying transport.
sourcepub fn accept_http1(self, accept_http1: bool) -> Self
pub fn accept_http1(self, accept_http1: bool) -> Self
Allow this server to accept http1 requests.
Accepting http1 requests is only useful when developing grpc-web
enabled services. If this setting is set to true
but services are
not correctly configured to handle grpc-web requests, your server may
return confusing (but correct) protocol errors.
Default is false
.
sourcepub fn trace_fn<F>(self, f: F) -> Selfwhere
F: Fn(&Request<()>) -> Span + Send + Sync + 'static,
pub fn trace_fn<F>(self, f: F) -> Selfwhere
F: Fn(&Request<()>) -> Span + Send + Sync + 'static,
Intercept inbound headers and add a tracing::Span
to each response future.
sourcepub fn add_service<S>(&mut self, svc: S) -> Router<S, Unimplemented, L>where
S: Service<Request<Body>, Response = Response<BoxBody>> + NamedService + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send,
L: Clone,
pub fn add_service<S>(&mut self, svc: S) -> Router<S, Unimplemented, L>where
S: Service<Request<Body>, Response = Response<BoxBody>> + NamedService + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send,
L: Clone,
Create a router with the S
typed service as the first service.
This will clone the Server
builder and create a router that will
route around different services.
sourcepub fn add_optional_service<S>(
&mut self,
svc: Option<S>
) -> Router<Either<S, Unimplemented>, Unimplemented, L>where
S: Service<Request<Body>, Response = Response<BoxBody>> + NamedService + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send,
L: Clone,
pub fn add_optional_service<S>(
&mut self,
svc: Option<S>
) -> Router<Either<S, Unimplemented>, Unimplemented, L>where
S: Service<Request<Body>, Response = Response<BoxBody>> + NamedService + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<Box<dyn Error + Send + Sync>> + Send,
L: Clone,
Create a router with the optional S
typed service as the first service.
This will clone the Server
builder and create a router that will
route around different services.
Note
Even when the argument given is None
this will capture all requests to this service name.
As a result, one cannot use this to toggle between two identically named implementations.
sourcepub fn layer<NewLayer>(self, new_layer: NewLayer) -> Server<NewLayer>
pub fn layer<NewLayer>(self, new_layer: NewLayer) -> Server<NewLayer>
Set the Tower Layer
all services will be wrapped in.
This enables using middleware from the Tower ecosystem.
Example
use tower::timeout::TimeoutLayer;
use std::time::Duration;
builder.layer(TimeoutLayer::new(Duration::from_secs(30)));
Note that timeouts should be set using Server::timeout
. TimeoutLayer
is only used
here as an example.
You can build more complex layers using ServiceBuilder
. Those layers can include
interceptors:
use tower::ServiceBuilder;
use std::time::Duration;
use tonic::{Request, Status, service::interceptor};
fn auth_interceptor(request: Request<()>) -> Result<Request<()>, Status> {
if valid_credentials(&request) {
Ok(request)
} else {
Err(Status::unauthenticated("invalid credentials"))
}
}
fn valid_credentials(request: &Request<()>) -> bool {
// ...
}
fn some_other_interceptor(request: Request<()>) -> Result<Request<()>, Status> {
Ok(request)
}
let layer = ServiceBuilder::new()
.load_shed()
.timeout(Duration::from_secs(30))
.layer(interceptor(auth_interceptor))
.layer(interceptor(some_other_interceptor))
.into_inner();
Server::builder().layer(layer);
Trait Implementations
Auto Trait Implementations
impl<L = Identity> !RefUnwindSafe for Server<L>
impl<L> Send for Server<L>where
L: Send,
impl<L> Sync for Server<L>where
L: Sync,
impl<L> Unpin for Server<L>where
L: Unpin,
impl<L = Identity> !UnwindSafe for Server<L>
Blanket Implementations
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstablefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
impl<T> ToOwned for Twhere
T: Clone,
impl<T> ToOwned for Twhere
T: Clone,
type Owned = T
type Owned = T
fn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T>where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;