mod blocking;
#[cfg(feature = "async")]
mod non_blocking;
pub use blocking::*;
#[cfg(feature = "async")]
pub use non_blocking::*;
use std::time::{Duration, Instant, SystemTime};
use uuid::Uuid;
use crate::{thread_id_to_u64};
struct LocalLockState {
lock_count: u32,
lock_value: String,
last_renew_time: Instant,
}
#[derive(Debug, Clone)]
pub struct LockInfo {
pub name: String,
pub value: String,
pub thread_id: u64,
pub lease_time: Duration,
pub acquired_at: SystemTime,
pub expire_time: SystemTime,
}
impl LockInfo {
pub fn new(name: String, lease_time: Duration) -> Self {
let value = Uuid::new_v4().to_string();
let thread_id = thread_id_to_u64();
let acquired_at = SystemTime::now();
let expire_time = acquired_at + lease_time;
Self {
name,
value,
thread_id,
lease_time,
acquired_at,
expire_time,
}
}
pub fn is_expired(&self) -> bool {
SystemTime::now() >= self.expire_time
}
pub fn remaining_time(&self) -> Duration {
self.expire_time
.duration_since(SystemTime::now())
.unwrap_or(Duration::from_secs(0))
}
}
struct RedLockLocalState {
lock_value: Option<String>,
acquired_at: Option<Instant>,
acquired_nodes: Vec<usize>, }