Skip to content

Commit eec507f

Browse files
committed
f Reuse dest_file_path as lock key
1 parent 5f76d8b commit eec507f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn path_to_windows_str<T: AsRef<OsStr>>(path: T) -> Vec<u16> {
3535
pub struct FilesystemStore {
3636
data_dir: PathBuf,
3737
tmp_file_counter: AtomicUsize,
38-
locks: Mutex<HashMap<(String, String), Arc<RwLock<()>>>>,
38+
locks: Mutex<HashMap<PathBuf, Arc<RwLock<()>>>>,
3939
}
4040

4141
impl FilesystemStore {
@@ -71,13 +71,13 @@ impl KVStore for FilesystemStore {
7171
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
7272
}
7373

74-
let mut outer_lock = self.locks.lock().unwrap();
75-
let lock_key = (namespace.to_string(), key.to_string());
76-
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
77-
7874
let mut dest_file_path = self.data_dir.clone();
7975
dest_file_path.push(namespace);
8076
dest_file_path.push(key);
77+
78+
let mut outer_lock = self.locks.lock().unwrap();
79+
let inner_lock_ref = Arc::clone(&outer_lock.entry(dest_file_path.clone()).or_default());
80+
8181
FilesystemReader::new(dest_file_path, inner_lock_ref)
8282
}
8383

@@ -97,15 +97,14 @@ impl KVStore for FilesystemStore {
9797
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
9898
}
9999

100-
let mut outer_lock = self.locks.lock().unwrap();
101-
let lock_key = (namespace.to_string(), key.to_string());
102-
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
103-
let _guard = inner_lock_ref.write().unwrap();
104-
105100
let mut dest_file_path = self.data_dir.clone();
106101
dest_file_path.push(namespace);
107102
dest_file_path.push(key);
108103

104+
let mut outer_lock = self.locks.lock().unwrap();
105+
let inner_lock_ref = Arc::clone(&outer_lock.entry(dest_file_path.clone()).or_default());
106+
let _guard = inner_lock_ref.write().unwrap();
107+
109108
let parent_directory = dest_file_path
110109
.parent()
111110
.ok_or_else(|| {
@@ -183,16 +182,15 @@ impl KVStore for FilesystemStore {
183182
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
184183
}
185184

186-
let mut outer_lock = self.locks.lock().unwrap();
187-
let lock_key = (namespace.to_string(), key.to_string());
188-
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key.clone()).or_default());
189-
190-
let _guard = inner_lock_ref.write().unwrap();
191-
192185
let mut dest_file_path = self.data_dir.clone();
193186
dest_file_path.push(namespace);
194187
dest_file_path.push(key);
195188

189+
let mut outer_lock = self.locks.lock().unwrap();
190+
let inner_lock_ref = Arc::clone(&outer_lock.entry(dest_file_path.clone()).or_default());
191+
192+
let _guard = inner_lock_ref.write().unwrap();
193+
196194
if !dest_file_path.is_file() {
197195
return Ok(());
198196
}
@@ -230,7 +228,7 @@ impl KVStore for FilesystemStore {
230228
// Note that this by itself is still leaky as lock entries will remain when more Readers/Writers are
231229
// around, but is preferable to doing nothing *or* something overly complex such as
232230
// implementing yet another RAII structure just for this pupose.
233-
outer_lock.remove(&lock_key);
231+
outer_lock.remove(&dest_file_path);
234232
}
235233

236234
// Garbage collect all lock entries that are not referenced anymore.

0 commit comments

Comments
 (0)