use std::sync::Arc;
use std::time::Duration;
use crate::{Lock, LockError, LockStore, generate_lock_token, validate_resource};
pub const DEFAULT_LOCK_TTL: Duration = Duration::from_secs(300);
#[derive(Clone)]
pub struct LockFactory {
store: Arc<dyn LockStore>,
}
impl LockFactory {
#[must_use]
pub fn new(store: Arc<dyn LockStore>) -> Self {
Self { store }
}
pub fn create_lock(
&self,
resource: impl Into<String>,
ttl: Option<Duration>,
auto_release: bool,
) -> Result<Lock, LockError> {
let ttl = Some(ttl.unwrap_or(DEFAULT_LOCK_TTL));
self.build(resource, ttl, auto_release)
}
pub fn create_lock_forever(
&self,
resource: impl Into<String>,
auto_release: bool,
) -> Result<Lock, LockError> {
self.build(resource, None, auto_release)
}
fn build(
&self,
resource: impl Into<String>,
ttl: Option<Duration>,
auto_release: bool,
) -> Result<Lock, LockError> {
let resource = resource.into();
validate_resource(&resource)?;
let token = generate_lock_token()?;
Ok(Lock::new(
resource,
token,
Arc::clone(&self.store),
ttl,
auto_release,
))
}
}