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
//! Filesystem-backed [`CoordinationLock`] (POSIX advisory `flock`, unix).
//!
//! One lock file per `key` under `root`. The flock call is blocking, so it is
//! offloaded via [`tokio::task::spawn_blocking`].
use std::{fs::OpenOptions, io, os::fd::AsRawFd, path::PathBuf, time::Duration};
use async_trait::async_trait;
use crate::traits::{CoordinationLock, LockError, LockGuard};
/// Filesystem-backed advisory lock. One lock file per `key` under `root`.
pub struct FileLock {
root: PathBuf,
}
impl FileLock {
#[must_use]
pub fn new(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() }
}
}
struct FileGuard {
file: Option<std::fs::File>,
path: PathBuf,
}
#[async_trait]
impl LockGuard for FileGuard {
async fn release(&mut self) {
if let Some(file) = self.file.take() {
// SAFETY: fd is valid and owned by `file`.
unsafe {
libc::flock(file.as_raw_fd(), libc::LOCK_UN);
}
let _ = std::fs::remove_file(&self.path);
}
}
}
impl Drop for FileGuard {
fn drop(&mut self) {
// Best-effort release if `release()` wasn't called explicitly.
if let Some(file) = self.file.take() {
unsafe {
libc::flock(file.as_raw_fd(), libc::LOCK_UN);
}
let _ = std::fs::remove_file(&self.path);
}
}
}
#[async_trait]
impl CoordinationLock for FileLock {
/// Acquire an exclusive `flock` on the lock file for `key` with a single
/// non-blocking `LOCK_EX | LOCK_NB` try.
///
/// # Semantics
///
/// - The lock lives until [`LockGuard::release`], the guard's `Drop`
/// best-effort fallback, or process exit (the kernel releases a `flock`
/// when the process or its file descriptor goes away).
/// - `lease` is **ignored** — there is no TTL and no automatic expiry.
///
/// On contention this returns [`LockError::Contended`] immediately; it
/// never queues or waits.
async fn acquire(&self, key: &str, _lease: Duration) -> Result<Box<dyn LockGuard>, LockError> {
let root = self.root.clone();
let path = root.join(sanitize(key));
let key_owned = key.to_owned();
let result = tokio::task::spawn_blocking(move || -> Result<FileGuard, LockError> {
std::fs::create_dir_all(&root)?;
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(&path)?;
let fd = file.as_raw_fd();
// LOCK_EX | LOCK_NB: non-blocking exclusive advisory lock.
// SAFETY: fd is a valid open file descriptor.
let r = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if r != 0 {
let err = io::Error::last_os_error();
// EAGAIN / EWOULDBLOCK ⇒ contended; anything else ⇒ io error.
if matches!(err.raw_os_error(), Some(libc::EAGAIN)) {
return Err(LockError::Contended(format!(
"flock on '{key_owned}' is held by another live process"
)));
}
return Err(LockError::Io(err));
}
Ok(FileGuard {
file: Some(file),
path,
})
})
.await
.map_err(|e| LockError::Io(io::Error::other(format!("blocking task failed: {e}"))))?;
match result {
Ok(g) => Ok(Box::new(g)),
Err(e) => Err(e),
}
}
}
fn sanitize(key: &str) -> String {
let mut out = String::with_capacity(key.len());
for c in key.chars() {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
out.push(c);
} else {
out.push('_');
}
}
if out.is_empty() {
out.push_str("default");
}
out.push_str(".lock");
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanitize_basic() {
assert_eq!(sanitize("hello"), "hello.lock");
assert_eq!(sanitize(""), "default.lock");
assert_eq!(sanitize("a/b c"), "a_b_c.lock");
}
}