Skip to main content

Server

Struct Server 

Source
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

Source

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.

Source

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

Source

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.

Source

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))
}
Source

pub fn with_config(self, config: ServeConfig) -> Server

Applies limits to this server.

Source

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.

Source

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:

  1. Stop accepting. The listener is dropped.
  2. End in-flight tasks. When the dispatcher has a request_handler, RequestHandler::finish_in_flight first lets tasks finish on their own for up to ServeConfig::completion_grace, then fires every remaining task’s cancellation token and waits up to ServeConfig::task_grace for the executors to act on it: cancel what they delegated, and end their tasks with a terminal event that reaches every open stream.
  3. 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§

Source§

impl Debug for Server

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

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§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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