mirror of
https://github.com/rustfs/rustfs.git
synced 2026-01-17 01:30:33 +00:00
@@ -11,7 +11,7 @@ use crate::{
|
||||
};
|
||||
use common::error::Result;
|
||||
|
||||
pub type RWLockerImpl = Box<dyn RWLocker + Send>;
|
||||
pub type RWLockerImpl = Box<dyn RWLocker + Send + Sync>;
|
||||
|
||||
#[async_trait]
|
||||
pub trait RWLocker {
|
||||
@@ -93,22 +93,33 @@ impl NsLockMap {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WrapperLocker(pub Arc<RwLock<RWLockerImpl>>);
|
||||
|
||||
impl Drop for WrapperLocker {
|
||||
fn drop(&mut self) {
|
||||
let inner = self.0.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = inner.write().await.un_lock().await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_nslock(
|
||||
ns: Arc<RwLock<NsLockMap>>,
|
||||
owner: String,
|
||||
volume: String,
|
||||
paths: Vec<String>,
|
||||
lockers: Vec<LockApi>,
|
||||
) -> RWLockerImpl {
|
||||
) -> WrapperLocker {
|
||||
if ns.read().await.is_dist_erasure {
|
||||
let names = paths
|
||||
.iter()
|
||||
.map(|path| Path::new(&volume).join(path).to_str().unwrap().to_string())
|
||||
.collect();
|
||||
return Box::new(DistLockInstance::new(owner, names, lockers));
|
||||
return WrapperLocker(Arc::new(RwLock::new(Box::new(DistLockInstance::new(owner, names, lockers)))));
|
||||
}
|
||||
|
||||
Box::new(LocalLockInstance::new(ns, volume, paths))
|
||||
WrapperLocker(Arc::new(RwLock::new(Box::new(LocalLockInstance::new(ns, volume, paths)))))
|
||||
}
|
||||
|
||||
struct DistLockInstance {
|
||||
@@ -258,7 +269,7 @@ mod test {
|
||||
#[tokio::test]
|
||||
async fn test_local_instance() -> Result<()> {
|
||||
let ns_lock_map = Arc::new(RwLock::new(NsLockMap::default()));
|
||||
let mut ns = new_nslock(
|
||||
let ns = new_nslock(
|
||||
Arc::clone(&ns_lock_map),
|
||||
"local".to_string(),
|
||||
"test".to_string(),
|
||||
@@ -267,7 +278,7 @@ mod test {
|
||||
)
|
||||
.await;
|
||||
|
||||
let result = ns
|
||||
let result = ns.0.write().await
|
||||
.get_lock(&Options {
|
||||
timeout: Duration::from_secs(5),
|
||||
retry_interval: Duration::from_secs(1),
|
||||
|
||||
@@ -50,7 +50,7 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
||||
let url = url::Url::parse("http://127.0.0.1:9000/data")?;
|
||||
let locker = new_lock_api(false, Some(url));
|
||||
let ns_mutex = Arc::new(RwLock::new(NsLockMap::new(true)));
|
||||
let mut ns = new_nslock(
|
||||
let ns = new_nslock(
|
||||
Arc::clone(&ns_mutex),
|
||||
"local".to_string(),
|
||||
"dandan".to_string(),
|
||||
@@ -59,7 +59,7 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
ns.get_lock(&Options {
|
||||
ns.0.write().await.get_lock(&Options {
|
||||
timeout: Duration::from_secs(5),
|
||||
retry_interval: Duration::from_secs(1),
|
||||
})
|
||||
@@ -68,6 +68,6 @@ async fn test_lock_unlock_ns_lock() -> Result<(), Box<dyn Error>> {
|
||||
true
|
||||
);
|
||||
|
||||
ns.un_lock().await.unwrap();
|
||||
ns.0.write().await.un_lock().await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user