pub struct Server { /* private fields */ }server only.Expand description
ConnectRPC server built on hyper.
Implementations§
Source§impl Server
impl Server
Sourcepub fn from_service(service: ConnectRpcService) -> Self
pub fn from_service(service: ConnectRpcService) -> Self
Create a new server from an existing ConnectRpcService.
Sourcepub fn with_tls(self, config: Arc<ServerConfig>) -> Self
Available on crate feature server-tls only.
pub fn with_tls(self, config: Arc<ServerConfig>) -> Self
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?;Sourcepub fn with_tls_handshake_timeout(self, timeout: Duration) -> Self
Available on crate feature server-tls only.
pub fn with_tls_handshake_timeout(self, timeout: Duration) -> Self
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.
Sourcepub fn with_http1_keep_alive(self, enabled: bool) -> Self
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.
Sourcepub fn with_deadline_policy(self, policy: DeadlinePolicy) -> Self
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.
Sourcepub fn with_limits(self, limits: Limits) -> Self
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.
Sourcepub fn with_compression(self, registry: CompressionRegistry) -> Self
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.
Sourcepub fn with_compression_policy(self, policy: CompressionPolicy) -> Self
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.
Sourcepub fn with_interceptor(self, interceptor: impl Interceptor) -> Self
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.
Sourcepub fn with_interceptor_arc(self, interceptor: Arc<dyn Interceptor>) -> Self
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.
Sourcepub async fn serve(
self,
addr: SocketAddr,
) -> Result<(), Box<dyn Error + Send + Sync>>
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.
Sourcepub fn from_listener(listener: TcpListener) -> BoundServer
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.
Sourcepub async fn bind(
addr: impl ToSocketAddrs,
) -> Result<BoundServer, Box<dyn Error + Send + Sync>>
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.