Struct tonic::transport::Server[][src]

pub struct Server<L = Identity> { /* fields omitted */ }
This is supported on crate feature transport only.
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

Create a new server builder that can configure a Server.

This is supported on crate feature tls only.

Configure TLS for this server.

Set the concurrency limit applied to on requests inbound per connection.

Example

builder.concurrency_limit_per_connection(32);

Set a timeout on for all request handlers.

Example

builder.timeout(Duration::from_secs(30));

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.

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};

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_fn(auth_interceptor))
    .layer(interceptor_fn(some_other_interceptor))
    .into_inner();

Server::builder().layer(layer);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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