Struct teaclave_rpc::transport::Server
source · [−]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<Server<L>, Error>
pub fn tls_config(self, tls_config: ServerTlsConfig) -> Result<Server<L>, Error>
Configure TLS for this server.
sourcepub fn concurrency_limit_per_connection(self, limit: usize) -> Server<L>
pub fn concurrency_limit_per_connection(self, limit: usize) -> Server<L>
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>>) -> Server<L>
pub fn initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> Server<L>
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>>
) -> Server<L>
pub fn initial_connection_window_size(
self,
sz: impl Into<Option<u32>>
) -> Server<L>
Sets the max connection-level flow control for HTTP2
Default is 65,535
sourcepub fn max_concurrent_streams(self, max: impl Into<Option<u32>>) -> Server<L>
pub fn max_concurrent_streams(self, max: impl Into<Option<u32>>) -> Server<L>
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>
) -> Server<L>
pub fn http2_keepalive_interval(
self,
http2_keepalive_interval: Option<Duration>
) -> Server<L>
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>
) -> Server<L>
pub fn http2_keepalive_timeout(
self,
http2_keepalive_timeout: Option<Duration>
) -> Server<L>
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>) -> Server<L>
pub fn http2_adaptive_window(self, enabled: Option<bool>) -> Server<L>
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 tcp_keepalive(self, tcp_keepalive: Option<Duration>) -> Server<L>
pub fn tcp_keepalive(self, tcp_keepalive: Option<Duration>) -> Server<L>
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) -> Server<L>
pub fn tcp_nodelay(self, enabled: bool) -> Server<L>
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>>) -> Server<L>
pub fn max_frame_size(self, frame_size: impl Into<Option<u32>>) -> Server<L>
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) -> Server<L>
pub fn accept_http1(self, accept_http1: bool) -> Server<L>
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) -> Server<L>where
F: 'static + Fn(&Request<()>) -> Span + Send + Sync,
pub fn trace_fn<F>(self, f: F) -> Server<L>where
F: 'static + Fn(&Request<()>) -> Span + Send + Sync,
Intercept inbound headers and add a [tracing::Span
] to each response future.
sourcepub fn add_service<S>(&mut self, svc: S) -> Router<L>where
S: 'static + Service<Request<Body>, Response = Response<UnsyncBoxBody<Bytes, Status>>, Error = Infallible> + NamedService + Clone + Send,
<S as Service<Request<Body>>>::Future: 'static + Send,
L: Clone,
pub fn add_service<S>(&mut self, svc: S) -> Router<L>where
S: 'static + Service<Request<Body>, Response = Response<UnsyncBoxBody<Bytes, Status>>, Error = Infallible> + NamedService + Clone + Send,
<S as Service<Request<Body>>>::Future: 'static + 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<L>where
S: 'static + Service<Request<Body>, Response = Response<UnsyncBoxBody<Bytes, Status>>, Error = Infallible> + NamedService + Clone + Send,
<S as Service<Request<Body>>>::Future: 'static + Send,
L: Clone,
pub fn add_optional_service<S>(&mut self, svc: Option<S>) -> Router<L>where
S: 'static + Service<Request<Body>, Response = Response<UnsyncBoxBody<Bytes, Status>>, Error = Infallible> + NamedService + Clone + Send,
<S as Service<Request<Body>>>::Future: 'static + 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<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 = 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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn 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>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
sourcefn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request