use std::fs::{self, File};
use std::path::{Path, PathBuf};
use fs2::FileExt;
use crate::error::{GwError, Result};
pub struct PoolLock {
_file: File,
_path: PathBuf,
}
impl PoolLock {
pub fn acquire(pool_dir: &Path) -> Result<Self> {
fs::create_dir_all(pool_dir)?;
let lock_path = pool_dir.join("pool.lock");
let file = File::create(&lock_path)?;
file.try_lock_exclusive()
.map_err(|_| GwError::PoolLockTimeout)?;
Ok(Self {
_file: file,
_path: lock_path,
})
}
}