Skip to main content

xmtp_configuration/common/
api.rs

1//! Backend connection values.
2
3pub const LOCALHOST: &str = "http://localhost";
4/// Maximum bytes in one gRPC message, in either direction. The client's
5/// transport refuses a larger message, and the backend refuses a request or
6/// response budget above it.
7pub const MAX_TRANSPORT_BYTES: usize = 25 * 1024 * 1024;
8
9/// The `google.rpc.ErrorInfo` reason on a rate-limit rejection (RATE-040).
10pub const RATE_LIMITED_REASON: &str = "RATE_LIMITED";
11/// The `google.rpc.ErrorInfo` domain on a rate-limit rejection (RATE-040).
12pub const RATE_LIMITED_DOMAIN: &str = "xmtp.org";
13
14/// Local backend URL when no worktree environment is loaded.
15pub const BACKEND_TEST_URL_DEFAULT: &str = "http://localhost:5050";
16/// Local backend proxy URL when no worktree environment is loaded.
17pub const BACKEND_TEST_TOXIC_URL_DEFAULT: &str = "http://localhost:6010";
18
19/// Local backend URL for client tests.
20///
21/// Each worktree publishes the stack on its own ports, so the address comes
22/// from the environment. `dev/worktree-env` writes it and the `just` recipes
23/// export it. The constant above is the fallback for a bare `cargo test`.
24///
25/// Native reads the variable at run time. Wasm has no `std::env`, so the value
26/// is baked in at build time instead; build through `just wasm test` so the
27/// worktree's environment is loaded.
28pub fn backend_test_url() -> String {
29    #[cfg(not(target_arch = "wasm32"))]
30    {
31        std::env::var("XMTP_BACKEND_URL").unwrap_or_else(|_| BACKEND_TEST_URL_DEFAULT.to_string())
32    }
33    #[cfg(target_arch = "wasm32")]
34    {
35        option_env!("XMTP_BACKEND_URL")
36            .unwrap_or(BACKEND_TEST_URL_DEFAULT)
37            .to_string()
38    }
39}
40
41/// Local backend proxy URL for fault tests. See [`backend_test_url`].
42pub fn backend_test_toxic_url() -> String {
43    #[cfg(not(target_arch = "wasm32"))]
44    {
45        std::env::var("XMTP_BACKEND_TOXIC_URL")
46            .unwrap_or_else(|_| BACKEND_TEST_TOXIC_URL_DEFAULT.to_string())
47    }
48    #[cfg(target_arch = "wasm32")]
49    {
50        option_env!("XMTP_BACKEND_TOXIC_URL")
51            .unwrap_or(BACKEND_TEST_TOXIC_URL_DEFAULT)
52            .to_string()
53    }
54}
55
56/// Temporary test URL names. Remove with the Task 9 binding update.
57pub struct GrpcUrls;
58impl GrpcUrls {
59    pub fn node() -> String {
60        backend_test_url()
61    }
62    pub fn gateway() -> String {
63        backend_test_url()
64    }
65}
66
67/// Temporary fault-test URL name. Remove with the Task 9 binding update.
68pub struct GrpcUrlsToxic;
69impl GrpcUrlsToxic {
70    pub fn node() -> String {
71        backend_test_toxic_url()
72    }
73}
74
75/// Maximum consecutive credential rejections and callback failures.
76///
77/// This value must stay above the failures one caller request can produce, or
78/// a single API call locks the client out. `Retry::default()` makes 6 attempts
79/// (5 retries), and the auth middleware replays each attempt once with a fresh
80/// credential, so one call can count up to 12 failures. The limit is above
81/// that, so only repeated calls reach the lockout. Raise it together with the
82/// retry budget; `retry_budget_cannot_reach_the_auth_lockout` checks the
83/// relation.
84pub const MAX_CONSECUTIVE_AUTH_FAILURES: u32 = 13;
85/// Time before one authentication probe is allowed after lockout.
86pub const AUTH_LOCKOUT_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(60);