Struct tonic::transport::server::Server [−][src]
pub struct Server<L = Identity> { /* fields omitted */ }
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
Set the concurrency limit applied to on requests inbound per connection.
Example
builder.concurrency_limit_per_connection(32);
Sets the SETTINGS_INITIAL_WINDOW_SIZE
option for HTTP2
stream-level flow control.
Default is 65,535
Sets the max connection-level flow control for HTTP2
Default is 65,535
Sets the SETTINGS_MAX_CONCURRENT_STREAMS
option for HTTP2
connections.
Default is no limit (None
).
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
)
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.
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
)
Set the value of TCP_NODELAY
option for accepted connections. Enabled by default.
Sets the maximum frame size to use for HTTP2.
Passing None
will do nothing.
If not set, will default from underlying transport.
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
.
Intercept inbound headers and add a tracing::Span
to each response future.
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.
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,
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.
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 = Identity> !UnwindSafe for Server<L>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more