1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! User-facing [`Lock`] handle.
use std::sync::{Arc, Mutex};
use std::time::Duration;
use crate::{LockError, LockStore};
/// Named lock bound to one owner token and a [`LockStore`].
pub struct Lock {
resource: String,
token: String,
store: Arc<dyn LockStore>,
ttl: Option<Duration>,
auto_release: bool,
acquired: Mutex<bool>,
}
impl Lock {
pub(crate) fn new(
resource: String,
token: String,
store: Arc<dyn LockStore>,
ttl: Option<Duration>,
auto_release: bool,
) -> Self {
Self {
resource,
token,
store,
ttl,
auto_release,
acquired: Mutex::new(false),
}
}
/// Resource name.
#[must_use]
pub fn resource(&self) -> &str {
&self.resource
}
/// Owner token.
#[must_use]
pub fn token(&self) -> &str {
&self.token
}
/// Default TTL used on acquire / refresh when callers pass `None`.
#[must_use]
pub const fn ttl(&self) -> Option<Duration> {
self.ttl
}
/// Tries to acquire without blocking.
///
/// Returns `Ok(true)` when acquired (or already held by this token), `Ok(false)` on conflict.
///
/// # Errors
///
/// Propagates store failures other than conflict.
///
/// # Panics
///
/// Panics when the internal acquired flag mutex is poisoned.
pub fn acquire(&self) -> Result<bool, LockError> {
match self.store.save(&self.resource, &self.token, self.ttl) {
Ok(()) => {
*self
.acquired
.lock()
.expect("serenade-lock acquired flag poisoned") = true;
Ok(true)
}
Err(LockError::Conflict { .. }) => Ok(false),
Err(err) => Err(err),
}
}
/// Releases ownership when held by this token.
///
/// # Errors
///
/// Propagates store failures.
///
/// # Panics
///
/// Panics when the internal acquired flag mutex is poisoned.
pub fn release(&self) -> Result<(), LockError> {
self.store.delete(&self.resource, &self.token)?;
*self
.acquired
.lock()
.expect("serenade-lock acquired flag poisoned") = false;
Ok(())
}
/// Extends TTL. `ttl: None` uses the lock default TTL (which may also be `None` = forever).
///
/// # Errors
///
/// Returns [`LockError::NotHeld`] when this token does not own the resource.
///
/// # Panics
///
/// Panics when the internal acquired flag mutex is poisoned.
pub fn refresh(&self, ttl: Option<Duration>) -> Result<(), LockError> {
let ttl = ttl.or(self.ttl);
self.store
.put_off_expiration(&self.resource, &self.token, ttl)?;
*self
.acquired
.lock()
.expect("serenade-lock acquired flag poisoned") = true;
Ok(())
}
/// Store-backed ownership check.
///
/// # Errors
///
/// Propagates store failures.
pub fn is_acquired(&self) -> Result<bool, LockError> {
self.store.exists(&self.resource, &self.token)
}
}
impl Drop for Lock {
fn drop(&mut self) {
if !self.auto_release {
return;
}
let mut acquired = self
.acquired
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if !*acquired {
return;
}
let _ = self.store.delete(&self.resource, &self.token);
*acquired = false;
}
}