Skip to main content

ProxyMiddleware

Trait ProxyMiddleware 

Source
pub trait ProxyMiddleware: Send + Sync {
    // Required methods
    fn on_request<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        req: &'life1 mut ProxyRequest,
        ctx: &'life2 mut ConnectionContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn on_response<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        res: &'life1 mut ProxyResponse,
        ctx: &'life2 ConnectionContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn name(&self) -> &'static str;

    // Provided methods
    fn on_connect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 ConnectionContext,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 ConnectionContext,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_init<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn on_shutdown<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

The central extension trait for agent-proxy-rust.

Implementors can intercept and transform requests/responses flowing through the proxy. Execution order:

  • on_init: registration order (at startup)
  • on_request: registration order
  • on_response: reverse registration order
  • on_disconnect: reverse registration order
  • on_shutdown: reverse registration order

All methods have default no-op implementations except on_request,on_response, and name.

Required Methods§

Source

fn on_request<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, req: &'life1 mut ProxyRequest, ctx: &'life2 mut ConnectionContext, ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called before forwarding the request to upstream.

Middleware may modify the request body, headers, or context extensions. For example, the compress middleware reduces tool definition sizes, and the model-router middleware selects a channel and sets the upstream URL.

Source

fn on_response<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, res: &'life1 mut ProxyResponse, ctx: &'life2 ConnectionContext, ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called after receiving the response from upstream.

Middleware may modify the response body, headers, or context extensions. Called in reverse registration order for symmetry with on_request.

Source

fn name(&self) -> &'static str

Returns the unique name of this middleware.

Used for logging and debugging.

Provided Methods§

Source

fn on_connect<'life0, 'life1, 'async_trait>( &'life0 self, _ctx: &'life1 ConnectionContext, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a new connection is established. Runs in registration order.

Source

fn on_disconnect<'life0, 'life1, 'async_trait>( &'life0 self, _ctx: &'life1 ConnectionContext, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a connection is closed. Runs in reverse registration order.

Source

fn on_init<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Called once when the proxy starts. Use for opening DB pools, loading config, etc.

Source

fn on_shutdown<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), ProxyError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Called once when the proxy shuts down gracefully.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§