distributed_lock_redis/redlock/
timeouts.rs

1//! RedLock timeout calculations.
2
3use std::time::Duration;
4
5use distributed_lock_core::timeout::TimeoutValue;
6
7/// Timeout configuration for RedLock algorithm.
8///
9/// Calculates acquire timeout based on expiry and minimum validity time.
10#[derive(Debug, Clone)]
11pub struct RedLockTimeouts {
12    /// Lock expiry time (TTL set on Redis keys).
13    pub expiry: Duration,
14    /// Minimum validity time required after acquisition.
15    ///
16    /// This accounts for clock drift in multi-server scenarios.
17    pub min_validity: Duration,
18}
19
20impl RedLockTimeouts {
21    /// Creates a new timeout configuration.
22    pub fn new(expiry: Duration, min_validity: Duration) -> Self {
23        Self {
24            expiry,
25            min_validity,
26        }
27    }
28
29    /// Calculates the acquire timeout.
30    ///
31    /// This is the maximum time we can spend acquiring the lock while still
32    /// ensuring at least `min_validity` time remains on the lock after acquisition.
33    pub fn acquire_timeout(&self) -> TimeoutValue {
34        if self.expiry > self.min_validity {
35            TimeoutValue::from(Some(self.expiry - self.min_validity))
36        } else {
37            TimeoutValue::ZERO
38        }
39    }
40}