Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Available on crate feature server only.
Expand description

ConnectRPC server built on hyper.

Implementations§

Source§

impl Server

Source

pub fn new(router: Router) -> Self

Create a new server with the given router.

Source

pub fn from_service(service: ConnectRpcService) -> Self

Create a new server from an existing ConnectRpcService.

Source

pub fn with_tls(self, config: Arc<ServerConfig>) -> Self

Available on crate feature server-tls only.

Enable TLS with the given rustls server configuration.

The configuration controls all TLS behavior including certificate selection, client authentication, and protocol versions. For dynamic certificate rotation, use a rustls::server::ResolvesServerCert implementation in the config.

§Example
use std::sync::Arc;

let tls_config = Arc::new(rustls::ServerConfig::builder()
    .with_no_client_auth()
    .with_single_cert(certs, key)?);

Server::new(router)
    .with_tls(tls_config)
    .serve(addr).await?;
Source

pub fn with_tls_handshake_timeout(self, timeout: Duration) -> Self

Available on crate feature server-tls only.

Set the TLS handshake timeout.

Defaults to DEFAULT_TLS_HANDSHAKE_TIMEOUT (10 seconds). A client that connects via TCP but does not complete the TLS handshake within this duration is disconnected.

Source

pub fn with_http1_keep_alive(self, enabled: bool) -> Self

Enable or disable HTTP/1.1 keep-alive (default: enabled).

When disabled, the server sends Connection: close and handles only one request per TCP connection. This avoids stale-connection races where the server closes an idle connection at the same time the client sends a new request on it.

HTTP/2 multiplexing is unaffected.

Source

pub fn with_deadline_policy(self, policy: DeadlinePolicy) -> Self

Configure server-side moderation of client-asserted RPC deadlines.

Delegates to ConnectRpcService::with_deadline_policy. The default DeadlinePolicy::new is a no-op: client timeout headers are honored verbatim and streaming bodies are unbounded once the handler returns. Set a policy to clamp, supply a default, or enforce on streams. See DeadlinePolicy.

Source

pub fn with_limits(self, limits: Limits) -> Self

Configure request limits on the underlying service.

Delegates to ConnectRpcService::with_limits. See Limits for available options. Replaces any previously configured limits.

Source

pub fn with_compression(self, registry: CompressionRegistry) -> Self

Configure the compression registry on the underlying service.

Delegates to ConnectRpcService::with_compression. The CompressionRegistry determines which compression algorithms are available for request decompression and response compression. Replaces any previously configured registry.

Source

pub fn with_compression_policy(self, policy: CompressionPolicy) -> Self

Configure the compression policy on the underlying service.

Delegates to ConnectRpcService::with_compression_policy. The CompressionPolicy controls when compression is applied (e.g. minimum message size). Replaces any previously configured policy.

Source

pub fn with_interceptor(self, interceptor: impl Interceptor) -> Self

Append an Interceptor to the chain on the underlying service.

Delegates to ConnectRpcService::with_interceptor. Interceptors run after envelope decoding, decompression, and protocol header parsing, and before the handler — they see the parsed request, not the wire bytes. The first interceptor registered runs outermost: first on the way in, last on the way out. To share one interceptor instance across several Servers, use with_interceptor_arc.

Source

pub fn with_interceptor_arc(self, interceptor: Arc<dyn Interceptor>) -> Self

Append an already-Arc’d Interceptor to the chain on the underlying service.

Delegates to ConnectRpcService::with_interceptor_arc. Same ordering and semantics as with_interceptor; use this when one interceptor instance is shared across multiple services or Servers.

Source

pub fn router(&self) -> &Router

Get a reference to the underlying router.

Source

pub async fn serve( self, addr: SocketAddr, ) -> Result<(), Box<dyn Error + Send + Sync>>

Bind and serve on the given address.

This runs forever until the process is killed. For graceful shutdown, use Server::bind + BoundServer::serve_with_graceful_shutdown.

Source

pub fn from_listener(listener: TcpListener) -> BoundServer

Bind to the given address and return a BoundServer.

Accepts anything implementing tokio::net::ToSocketAddrs:

  • "127.0.0.1:8080" — IPv4 loopback (safest default for dev)
  • "[::1]:8080" — IPv6 loopback
  • "0.0.0.0:8080" — all IPv4 interfaces (only for trusted networks)
  • "[::]:8080" — all IPv6 interfaces (on Linux, also accepts IPv4 via IPv4-mapped addresses by default)
  • "localhost:8080" — resolves via DNS/hosts (may yield v4, v6, or both)

Wrap a pre-bound TcpListener.

Use this instead of Server::bind when you need to configure socket options before binding — e.g. IPV6_V6ONLY=false for dual-stack listening, SO_REUSEPORT for multi-process accept, or binding to a listener inherited from a parent process.

Source

pub async fn bind( addr: impl ToSocketAddrs, ) -> Result<BoundServer, Box<dyn Error + Send + Sync>>

When multiple addresses are returned (e.g. localhost resolving to both ::1 and 127.0.0.1), the first that successfully binds is used.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more