git_policy_cli/wait_limit.rs
1//! What:
2//! How long a command waits for the commit service's own hold on the index, and the variable that sets it.
3//! Why:
4//! A hold that is alive but stuck would otherwise stall every later command with no output at all. The wait ends at a
5//! generous backstop (`CLI_GIT_LOCK_WAIT_SECONDS`, thirty minutes by default), read by the wrapper that waits and by the
6//! service that waits for a foreign `index.lock` before it lands a commit; it belongs in the environment of the unit that
7//! runs the service.
8//!
9//! In TS you'd write (pseudocode):
10//! ```ts
11//! const limitMs = positiveSeconds(process.env.CLI_GIT_LOCK_WAIT_SECONDS) ?? DEFAULT_LIVE_WAIT_MS;
12//! ```
13
14/// Positive-seconds parsing shared with the push deadline.
15use super::bounded_push::positive_seconds;
16/// Borrowed OS text of the environment value.
17use std::ffi::OsStr;
18/// Time spans.
19use std::time::Duration;
20
21/// Environment variable that sets, in whole seconds, how long a command waits for a lock held by a live process.
22pub const LIVE_WAIT_VARIABLE: &str = "CLI_GIT_LOCK_WAIT_SECONDS";
23
24/// How long a command waits for a live holder when the variable is absent or unusable:
25/// longer than any ordinary hook run, short enough that a stuck holder is noticed within the hour.
26pub const DEFAULT_LIVE_WAIT: Duration = Duration::from_secs(30 * 60);
27
28/// What:
29/// The wait limit for a value of the variable.
30/// Why:
31/// A missing, zero or malformed value falls back to the default,
32/// never to "no limit".
33///
34/// In TS you'd write (pseudocode):
35/// ```ts
36/// positiveSeconds(value) ?? DEFAULT_LIVE_WAIT_MS
37/// ```
38pub fn live_wait_limit_from(value: Option<&OsStr>) -> Duration {
39 let Some(text) = value else {
40 return DEFAULT_LIVE_WAIT;
41 };
42 return positive_seconds(LIVE_WAIT_VARIABLE, text).unwrap_or(DEFAULT_LIVE_WAIT);
43}
44
45/// The wait limit this process runs under: the variable in its environment, or the default.
46///
47/// In TS you'd write (pseudocode):
48/// ```ts
49/// liveWaitLimitMs(process.env.CLI_GIT_LOCK_WAIT_SECONDS)
50/// ```
51pub fn live_wait_limit() -> Duration {
52 return live_wait_limit_from(std::env::var_os(LIVE_WAIT_VARIABLE).as_deref());
53}
54
55/// Wait limit controls stay out of the release executable.
56#[cfg(test)]
57#[path = "wait_limit_tests.rs"]
58mod tests;