Skip to main content

Dispatcher

Trait Dispatcher 

Source
pub trait Dispatcher:
    Send
    + Sync
    + 'static {
    // Required method
    fn dispatch(
        &self,
        req: Request<Incoming>,
    ) -> Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, !>>> + Send + '_>>;

    // Provided methods
    fn request_handler(&self) -> Option<&Arc<RequestHandler>> { ... }
    fn protocol_binding(&self) -> Option<&'static str> { ... }
    fn close_upgraded(
        &self,
        _within: Duration,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> { ... }
}
Expand description

Trait for types that can dispatch HTTP requests to an A2A handler.

Implemented by JsonRpcDispatcher, RestDispatcher, CombinedDispatcher and, with the axum feature, the axum router (RestRouter, ADR 0052); Server serves any of them.

Required Methods§

Source

fn dispatch( &self, req: Request<Incoming>, ) -> Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, !>>> + Send + '_>>

Dispatches an HTTP request and returns a response.

Provided Methods§

Source

fn request_handler(&self) -> Option<&Arc<RequestHandler>>

The handler whose tasks this dispatcher starts, if it has one.

Server::serve_with_shutdown uses it to end in-flight work before draining connections: it calls RequestHandler::finish_in_flight as soon as the listener closes, so executors can cancel what they delegated and every open stream ends with a terminal event, and only then waits for the sockets.

What the default costs. It returns None, and a dispatcher that keeps it gets the socket-only shutdown serve_with_shutdown had until 2026-09-22: the drain waits on streams whose tasks nobody has cancelled, for up to ServeConfig::drain_timeout, and whatever those tasks delegated is left running when the process exits. Override it in any dispatcher that wraps a RequestHandler; JsonRpcDispatcher and RestDispatcher do.

Source

fn protocol_binding(&self) -> Option<&'static str>

The protocolBinding this dispatcher serves, as an agent card names it ("JSONRPC", "HTTP+JSON"), if it serves exactly one.

Server uses it with request_handler to warn, when it starts, that the handler’s agent card does not declare the binding this listener serves (spec §5.2: “Agents MUST declare all supported protocols in their AgentCard”). The default, None, is not checked.

Source

fn close_upgraded( &self, _within: Duration, ) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>

Closes the connections this dispatcher upgraded to WebSocket (ADR 0055), which the HTTP server no longer sees once hyper has handed them over: each stops reading, lets its requests finish and sends a Close frame. true when all of them ended within within. Server::serve_with_shutdown calls it in its drain. The default upgrades nothing.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<D> Dispatcher for Arc<D>
where D: Dispatcher,

A shared dispatcher is a dispatcher: Arc<D> forwards all three methods, so code that keeps an Arc<JsonRpcDispatcher> to hand clones to other tasks can give it to Server as it is. The provided methods are forwarded too, not left at their defaults: a wrapper that kept the defaults would lose the shutdown that cancels in-flight work first (Dispatcher::request_handler) and the one that closes upgraded connections (Dispatcher::close_upgraded).

Source§

fn dispatch( &self, req: Request<Incoming>, ) -> Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, !>>> + Send + '_>>

Source§

fn request_handler(&self) -> Option<&Arc<RequestHandler>>

Source§

fn protocol_binding(&self) -> Option<&'static str>

Source§

fn close_upgraded( &self, within: Duration, ) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>

Implementors§

Source§

impl Dispatcher for A2aRouter

The axum router as a Dispatcher, so it runs on Server under a ServeConfig (audit G-09, ADR 0052): the header-read and idle timeouts, the connection cap, the loopback Host check, the production refusal and the shutdown that ends the tasks before it drains the connections. Under plain axum::serve it had none of them.

Source§

impl Dispatcher for CombinedDispatcher

Source§

impl Dispatcher for JsonRpcDispatcher

Source§

impl Dispatcher for RestDispatcher

Source§

impl<D> Dispatcher for Hsts<D>
where D: Dispatcher,