Skip to content

Commit 1929328

Browse files
LilithSilveralexjg
authored andcommitted
limit number of file descriptors opened by FileSystemStorage
Problem: when loading a large number of documents it was possible for the file system storage backend to exhaust the available file descriptors. Solution: limit the number of files opened at once to (somewhat arbitrarily) 500.
1 parent c505792 commit 1929328

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎samod/src/storage/filesystem.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ pub mod tokio {
2020
use std::{
2121
collections::HashMap,
2222
path::{Path, PathBuf},
23+
sync::Arc,
2324
};
2425

26+
use tokio::sync::Semaphore;
27+
2528
use crate::storage::Storage;
2629

2730
use super::key_to_path;
@@ -32,12 +35,14 @@ pub mod tokio {
3235
#[derive(Clone, Debug)]
3336
pub struct FilesystemStorage {
3437
data_dir: PathBuf,
38+
read_limit: Arc<Semaphore>,
3539
}
3640

3741
impl FilesystemStorage {
3842
pub fn new<P: AsRef<Path>>(data_dir: P) -> Self {
3943
Self {
4044
data_dir: data_dir.as_ref().to_path_buf(),
45+
read_limit: Arc::new(Semaphore::new(500)),
4146
}
4247
}
4348

@@ -80,6 +85,7 @@ pub mod tokio {
8085
// Now walk the directory, recursively
8186
let mut to_visit = vec![(path, prefix)];
8287
while let Some((next_path, key_prefix)) = to_visit.pop() {
88+
let permit = self.read_limit.acquire().await;
8389
let Ok(mut entries) = tokio::fs::read_dir(&next_path).await else {
8490
continue;
8591
};
@@ -109,6 +115,7 @@ pub mod tokio {
109115
result.insert(next_key_prefix, data);
110116
}
111117
}
118+
drop(permit);
112119
}
113120
result
114121
}

0 commit comments

Comments
 (0)