use distributed_lock_core::{error::LockResult, traits::LockHandle};
use mongodb::{Collection, bson::doc};
use tokio::sync::watch;
use crate::document::MongoLockDocument;
pub struct MongoLockHandle {
pub(crate) collection: Collection<MongoLockDocument>,
pub(crate) name: String,
pub(crate) lock_id: String,
pub(crate) lost_token: watch::Receiver<bool>,
}
impl LockHandle for MongoLockHandle {
fn lost_token(&self) -> &watch::Receiver<bool> {
&self.lost_token
}
async fn release(self) -> LockResult<()> {
let filter = doc! {
"_id": &self.name,
"lockId": &self.lock_id
};
self.collection
.delete_one(filter)
.await
.map_err(|e| distributed_lock_core::error::LockError::Connection(Box::new(e)))?;
Ok(())
}
}