Skip to main content

hyperlane_plugin_timeout/
struct.rs

1use super::*;
2
3/// Per-prefix timeout override entry.
4#[derive(Clone, Debug, Eq, Getter, PartialEq)]
5pub struct PrefixTimeout {
6    /// Segment-bounded path prefix.
7    pub(crate) prefix: String,
8    /// Deadline budget in milliseconds (`0` = disabled for this prefix).
9    #[get(type(copy))]
10    pub(crate) timeout_ms: u64,
11}
12
13/// Active request deadline metadata stored in [`Context`] under [`ATTR_REQUEST_DEADLINE`].
14#[derive(Clone, Copy, Debug, Eq, Getter, PartialEq)]
15pub struct RequestDeadline {
16    /// Monotonic instant when the request entered [`TimeoutRequestHook`].
17    #[get(type(copy))]
18    pub(crate) started_at: Instant,
19    /// Monotonic instant when the deadline expires.
20    #[get(type(copy))]
21    pub(crate) deadline: Instant,
22    /// Effective deadline budget in milliseconds.
23    #[get(type(copy))]
24    pub(crate) budget_ms: u64,
25}
26
27/// Configuration for [`TimeoutPlugin`].
28#[derive(Clone, Data, Debug, Eq, PartialEq)]
29pub struct TimeoutConfig {
30    /// Global fallback timeout budget in milliseconds (`0` = disabled).
31    #[get(type(copy))]
32    pub(crate) default_timeout_ms: u64,
33    /// Longest-prefix-first per-route timeout overrides.
34    pub(crate) prefix_timeouts: Vec<PrefixTimeout>,
35    /// Whether an inbound client timeout header may tighten (never extend) the server budget.
36    #[get(type(copy))]
37    pub(crate) honor_client_header: bool,
38    /// Inbound client timeout header name (default `x-request-timeout`).
39    pub(crate) client_header_name: String,
40    /// Whether to emit `x-timeout-remaining-ms` on normal responses.
41    #[get(type(copy))]
42    pub(crate) expose_remaining_header: bool,
43    /// HTTP status code applied when a deadline has expired on response (default `504`).
44    #[get(type(copy))]
45    pub(crate) timeout_status: HttpStatus,
46    /// `Content-Type` applied on timeout responses.
47    pub(crate) timeout_content_type: String,
48    /// Body bytes applied on timeout responses.
49    pub(crate) timeout_body: Vec<u8>,
50}
51
52/// Compiled request deadline manager backing [`TimeoutRequestHook`] and [`TimeoutResponseHook`].
53#[derive(Clone, Debug)]
54pub struct TimeoutPlugin {
55    /// Retained configuration.
56    pub(crate) config: TimeoutConfig,
57    /// Normalized prefix timeout rules sorted longest-prefix first.
58    pub(crate) prefix_rules: Box<[(String, u64)]>,
59    /// Lowercased inbound timeout header name.
60    pub(crate) client_header_lower: String,
61}
62
63/// Opt-in request middleware attaching a monotonic [`RequestDeadline`] to [`Context`].
64#[derive(Clone, Copy, Debug, Default)]
65#[hyperlane::request_middleware(-112)]
66pub struct TimeoutRequestHook;
67
68/// Opt-in response middleware converting expired deadlines into `504 Gateway Timeout`.
69#[derive(Clone, Copy, Debug, Default)]
70#[hyperlane::response_middleware(82)]
71pub struct TimeoutResponseHook;