pub struct Server { /* private fields */ }Expand description
A bound listener that has not started accepting yet.
Binding is separated from serving so the caller can learn the address —
which matters when binding port 0 — without racing the accept loop.
Implementations§
Source§impl Server
impl Server
Sourcepub fn with_tls(self, config: ServerConfig) -> Server
pub fn with_tls(self, config: ServerConfig) -> Server
Serves HTTPS: every connection is a TLS handshake first, with
config’s certificate. Default: plaintext.
The listener then speaks TLS only; a plaintext client fails the
handshake and is closed, with no fallback. When config names no ALPN
protocols, h2 and http/1.1 are offered, the two this server
speaks. The handshake runs on the connection’s own task, so a slow
one never holds up the accept loop, and it must finish within
ServeConfig::header_read_timeout
when that is set; the idle timeout applies underneath it as to any
other bytes. A connection whose handshake fails is closed and counted
as errored.
config is used as built, client authentication included, so its
crypto provider is the one it was built with. For a certificate and
key in PEM files, with_tls_pem builds one.
When the client presents a certificate, each of the connection’s
requests carries it as a PeerCertificate
extension, which the dispatchers pass to interceptors as
CallContext::peer_certificate;
MutualTlsAuthInterceptor
names the caller from it. Until 2026-10-18 the dispatchers did not see
it.
Sourcepub fn with_tls_pem(
self,
cert_chain: &[u8],
key: &[u8],
) -> Result<Server, Error>
pub fn with_tls_pem( self, cert_chain: &[u8], key: &[u8], ) -> Result<Server, Error>
with_tls with a server certificate chain (leaf
first) and its private key (PKCS#8, PKCS#1 or SEC1), both PEM.
The configuration is rustls’ safe defaults (TLS 1.2 and 1.3) on the
ring provider, chosen explicitly so that a binary linking more than
one provider cannot panic here. No client certificate is asked for.
§Errors
std::io::ErrorKind::InvalidInput when cert_chain holds no
certificate, either PEM does not parse, or the key is not the
certificate’s.
Source§impl Server
impl Server
Sourcepub async fn bind(addr: impl ToSocketAddrs) -> Result<Server, Error>
pub async fn bind(addr: impl ToSocketAddrs) -> Result<Server, Error>
Binds a listener without accepting anything yet.
§Errors
Returns std::io::Error if the address cannot be bound.
Sourcepub fn from_listener(listener: TcpListener) -> Server
pub fn from_listener(listener: TcpListener) -> Server
Wraps a listener the caller has already bound, without accepting anything yet.
For a socket bound outside this crate: one inherited from systemd
socket activation or handed over by a supervisor for a restart without
downtime, one built with options bind does not set
(SO_REUSEPORT, IPV6_V6ONLY, a custom backlog), or one bound before
the process drops privileges. The gRPC and WebSocket dispatchers’
serve_with_shutdown take a listener the same way.
A std::net::TcpListener must be switched to non-blocking before it
is converted:
use a2a_protocol_server::serve::Server;
fn wrap(inherited: std::net::TcpListener) -> std::io::Result<Server> {
inherited.set_nonblocking(true)?;
let listener = tokio::net::TcpListener::from_std(inherited)?;
Ok(Server::from_listener(listener))
}Sourcepub fn with_config(self, config: ServeConfig) -> Server
pub fn with_config(self, config: ServeConfig) -> Server
Applies limits to this server.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
The address actually bound, which is the only way to learn the port when
binding to 0.
§Errors
Returns std::io::Error if the socket cannot report its address.
Sourcepub async fn serve_with_shutdown(
self,
dispatcher: impl Dispatcher,
shutdown: impl Future<Output = ()> + Send,
) -> ServeReport
pub async fn serve_with_shutdown( self, dispatcher: impl Dispatcher, shutdown: impl Future<Output = ()> + Send, ) -> ServeReport
Accepts until shutdown resolves, then ends the work in flight and
drains.
The order is what makes it graceful:
- Stop accepting. The listener is dropped.
- End in-flight tasks. When the dispatcher has a
request_handler,RequestHandler::finish_in_flightfirst lets tasks finish on their own for up toServeConfig::completion_grace, then fires every remaining task’s cancellation token and waits up toServeConfig::task_gracefor the executors to act on it: cancel what they delegated, and end their tasks with a terminal event that reaches every open stream. - Drain connections, for up to
ServeConfig::drain_timeout. Streams whose tasks have ended close on their own, so this is usually quick.
It used to drain first. An open SSE stream is a connection that does not close until its task ends, so the drain waited out its whole timeout on tasks nobody had cancelled, and a delegating executor’s downstream work outlived the process.
Returns once both phases have finished or run out of time — never
before, which is the whole point of it existing. Call
RequestHandler::shutdown after it
to run the executor’s cleanup hook.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Server
impl !RefUnwindSafe for Server
impl !UnwindSafe for Server
impl Forget for Server
impl Move for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl UnsafeUnpin for Server
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].