Skip to main content

serenade_lock/
store.rs

1//! [`LockStore`] persistence contract.
2
3use std::time::Duration;
4
5use crate::LockError;
6
7/// Low-level lock persistence (Symfony `PersistingStoreInterface` analogue).
8pub trait LockStore: Send + Sync {
9    /// Persists ownership for `resource` under `token`.
10    ///
11    /// When `ttl` is `Some`, ownership expires after that duration. `None` means no expiry.
12    /// Same-token re-save refreshes ownership. Another token yields [`LockError::Conflict`].
13    ///
14    /// # Errors
15    ///
16    /// Returns [`LockError`] on invalid resource, conflict, or store failure.
17    fn save(&self, resource: &str, token: &str, ttl: Option<Duration>) -> Result<(), LockError>;
18
19    /// Drops ownership when `token` matches (idempotent when missing or expired).
20    ///
21    /// # Errors
22    ///
23    /// Returns [`LockError`] on store failure.
24    fn delete(&self, resource: &str, token: &str) -> Result<(), LockError>;
25
26    /// True when `token` currently owns `resource` and the entry has not expired.
27    ///
28    /// # Errors
29    ///
30    /// Returns [`LockError`] on store failure.
31    fn exists(&self, resource: &str, token: &str) -> Result<bool, LockError>;
32
33    /// Extends TTL for a held lock. `ttl: None` clears expiry (hold forever).
34    ///
35    /// # Errors
36    ///
37    /// Returns [`LockError::NotHeld`] when this token does not own the resource.
38    fn put_off_expiration(
39        &self,
40        resource: &str,
41        token: &str,
42        ttl: Option<Duration>,
43    ) -> Result<(), LockError>;
44}