Skip to main content

lexe_common/constants/
timeout.rs

1//! Timeout constants, organized into nested modules named after the API trait,
2//! service, or struct whose timeouts it holds.
3//!
4//! `const_assert!`s are used to enforce our timeout hierarchies; e.g. each
5//! timeout along a request chain must be shorter than the one enclosing it.
6//!
7//! Principles for tuning:
8//!
9//! - When a timeout increase is needed: prefer to raise the timeout for a
10//!   specific server or client method that needs it, rather than the global
11//!   default.
12//! - Be careful when decreasing timeouts baked into enclave programs - if it
13//!   cannot be overridden via CLI, there is no way to backtrack if the decrease
14//!   was too aggressive. Wire through a CLI arg before decreasing the timeout.
15
16use std::time::Duration;
17
18use lexe_std::const_assert;
19
20/// Defaults for Lexe API servers.
21pub mod server {
22    use super::*;
23
24    /// The grace period passed to `axum_server::Handle::graceful_shutdown`
25    /// during which new connections are refused and we wait for existing
26    /// connections to terminate before initiating a hard shutdown.
27    pub const SHUTDOWN_GRACE_PERIOD: Duration = Duration::from_secs(3);
28
29    /// The maximum time we'll wait for a server to complete shutdown.
30    pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
31
32    /// The default maximum time a server can spend handling a request.
33    pub const DEFAULT_HANDLER_TIMEOUT: Duration = Duration::from_secs(25);
34
35    const_assert!(SHUTDOWN_TIMEOUT.as_secs() > SHUTDOWN_GRACE_PERIOD.as_secs());
36}
37
38/// Defaults for Lexe `RestClient`s.
39pub mod client {
40    use super::*;
41
42    /// The default request timeout for Lexe `RestClient`s.
43    pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
44
45    const_assert!(
46        DEFAULT_TIMEOUT.as_secs() > server::DEFAULT_HANDLER_TIMEOUT.as_secs()
47    );
48}
49
50/// Timeouts for `UserNode`s.
51pub mod usernode {
52    use super::*;
53
54    /// Default sync timeout for user nodes (BDK/LDK sync).
55    //
56    // (2026-07-24): 30s -> 15s. The const was created at 30s despite 15s being
57    // the documented default; prod doesn't override it.
58    pub const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(15);
59
60    /// The amount of time user node tasks have to finish after a graceful
61    /// shutdown signal is received before the task is forced to exit.
62    pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(25);
63
64    // The meganode's `run_user` handler (bounded by the default
65    // `server::DEFAULT_HANDLER_TIMEOUT`) blocks on the usernode's boot sync
66    // before responding.
67    const_assert!(
68        DEFAULT_SYNC_TIMEOUT.as_secs()
69            < server::DEFAULT_HANDLER_TIMEOUT.as_secs()
70    );
71    const_assert!(
72        SHUTDOWN_TIMEOUT.as_secs() > server::SHUTDOWN_TIMEOUT.as_secs()
73    );
74}
75
76/// Timeouts for in-enclave `UserRunner`.
77pub mod userrunner {
78    use super::*;
79
80    /// The amount of time the user runner has to finish after a graceful
81    /// shutdown signal is received before the program is forced to exit.
82    pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(27);
83
84    const_assert!(
85        SHUTDOWN_TIMEOUT.as_secs() > usernode::SHUTDOWN_TIMEOUT.as_secs()
86    );
87}
88
89/// Timeouts for `UserNodeProvisionApi` (User -> Node).
90pub mod user_node_provision_api {
91    use super::*;
92
93    /// `NodeClient`'s request timeout for provisioning. Generous because the
94    /// provision handler does the Google Drive OAuth exchange and GVFS setup.
95    pub const CLIENT_TIMEOUT: Duration = Duration::from_secs(30);
96
97    const_assert!(
98        CLIENT_TIMEOUT.as_secs() > server::DEFAULT_HANDLER_TIMEOUT.as_secs()
99    );
100}
101
102/// Timeouts for `UserNodeRunApi` (User -> Node).
103pub mod user_node_run_api {
104    use super::*;
105
106    /// Handling timeout for the node's user-facing server. Generous because
107    /// `[preflight_]pay_invoice` may compute `max_flow`, which takes ~30s at
108    /// 10 iterations and ~50s at 17 iterations.
109    /// See `compute_max_flow_to_recipient` for more details.
110    pub const SERVER_HANDLER_TIMEOUT: Duration = Duration::from_secs(60);
111
112    /// Timeouts for endpoints which may compute `max_flow`.
113    pub mod max_flow {
114        use super::*;
115
116        /// Client timeout for endpoints which may compute `max_flow`.
117        pub const CLIENT_TIMEOUT: Duration = Duration::from_secs(62);
118
119        const_assert!(
120            CLIENT_TIMEOUT.as_secs()
121                > user_node_run_api::SERVER_HANDLER_TIMEOUT.as_secs()
122        );
123    }
124}