hyperlane-plugin-timeout 23.3.1

A per-route and global request deadline budget plugin for the Hyperlane framework with X-Request-Timeout clamping, Context RequestDeadline propagation, timeout_future async helper, and 504 Gateway Timeout enforcement.
Documentation
use super::*;

/// Per-prefix timeout override entry.
#[derive(Clone, Debug, Eq, Getter, PartialEq)]
pub struct PrefixTimeout {
    /// Segment-bounded path prefix.
    pub(crate) prefix: String,
    /// Deadline budget in milliseconds (`0` = disabled for this prefix).
    #[get(type(copy))]
    pub(crate) timeout_ms: u64,
}

/// Active request deadline metadata stored in [`Context`] under [`ATTR_REQUEST_DEADLINE`].
#[derive(Clone, Copy, Debug, Eq, Getter, PartialEq)]
pub struct RequestDeadline {
    /// Monotonic instant when the request entered [`TimeoutRequestHook`].
    #[get(type(copy))]
    pub(crate) started_at: Instant,
    /// Monotonic instant when the deadline expires.
    #[get(type(copy))]
    pub(crate) deadline: Instant,
    /// Effective deadline budget in milliseconds.
    #[get(type(copy))]
    pub(crate) budget_ms: u64,
}

/// Configuration for [`TimeoutPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct TimeoutConfig {
    /// Global fallback timeout budget in milliseconds (`0` = disabled).
    #[get(type(copy))]
    pub(crate) default_timeout_ms: u64,
    /// Longest-prefix-first per-route timeout overrides.
    pub(crate) prefix_timeouts: Vec<PrefixTimeout>,
    /// Whether an inbound client timeout header may tighten (never extend) the server budget.
    #[get(type(copy))]
    pub(crate) honor_client_header: bool,
    /// Inbound client timeout header name (default `x-request-timeout`).
    pub(crate) client_header_name: String,
    /// Whether to emit `x-timeout-remaining-ms` on normal responses.
    #[get(type(copy))]
    pub(crate) expose_remaining_header: bool,
    /// HTTP status code applied when a deadline has expired on response (default `504`).
    #[get(type(copy))]
    pub(crate) timeout_status: HttpStatus,
    /// `Content-Type` applied on timeout responses.
    pub(crate) timeout_content_type: String,
    /// Body bytes applied on timeout responses.
    pub(crate) timeout_body: Vec<u8>,
}

/// Compiled request deadline manager backing [`TimeoutRequestHook`] and [`TimeoutResponseHook`].
#[derive(Clone, Debug)]
pub struct TimeoutPlugin {
    /// Retained configuration.
    pub(crate) config: TimeoutConfig,
    /// Normalized prefix timeout rules sorted longest-prefix first.
    pub(crate) prefix_rules: Box<[(String, u64)]>,
    /// Lowercased inbound timeout header name.
    pub(crate) client_header_lower: String,
}

/// Opt-in request middleware attaching a monotonic [`RequestDeadline`] to [`Context`].
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-112)]
pub struct TimeoutRequestHook;

/// Opt-in response middleware converting expired deadlines into `504 Gateway Timeout`.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::response_middleware(82)]
pub struct TimeoutResponseHook;