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§
source§impl<L> Server<L>
impl<L> Server<L>
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 http2_adaptive_window(self, enabled: Option<bool>) -> Self
pub fn http2_adaptive_window(self, enabled: Option<bool>) -> Self
Sets whether to use an adaptive flow control. Defaults to false. Enabling this will override the limits set in http2_initial_stream_window_size and http2_initial_connection_window_size.
sourcepub fn http2_max_pending_accept_reset_streams(self, max: Option<usize>) -> Self
pub fn http2_max_pending_accept_reset_streams(self, max: Option<usize>) -> Self
Configures the maximum number of pending reset streams allowed before a GOAWAY will be sent.
This will default to whatever the default in h2 is. As of v0.3.17, it is 20.
See https://github.com/hyperium/hyper/issues/2877 for more information.
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) -> Self
pub fn trace_fn<F>(self, f: F) -> Self
Intercept inbound headers and add a tracing::Span
to each response future.
sourcepub fn add_service<S>(&mut self, svc: S) -> Router<L>
pub fn add_service<S>(&mut self, svc: S) -> Router<L>
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<L>
pub fn add_optional_service<S>(&mut self, svc: Option<S>) -> Router<L>
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 add_routes(&mut self, routes: Routes) -> Router<L>where
L: Clone,
pub fn add_routes(&mut self, routes: Routes) -> Router<L>where
L: Clone,
Create a router with given Routes
.
This will clone the Server
builder and create a router that will
route around different services that were already added to the provided routes
.
sourcepub fn layer<NewLayer>(self, new_layer: NewLayer) -> Server<Stack<NewLayer, L>>
pub fn layer<NewLayer>(self, new_layer: NewLayer) -> Server<Stack<NewLayer, L>>
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> Freeze for Server<L>where
L: Freeze,
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§
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request