Skip to content

Commit

Permalink
fix(su): add max read memory #980
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceJuliano committed Aug 28, 2024
1 parent 17f5917 commit e93a4ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion servers/su/src/domain/clients/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,18 +1092,32 @@ mod bytestore {
&self,
ids: Vec<(String, Option<String>, String, String)>,
) -> Result<DashMap<(String, Option<String>, String, String), Vec<u8>>, String> {
let max_memory_usage = self.config.max_read_memory;
let binaries = Arc::new(DashMap::new());
let db = match self.db.read() {
Ok(r) => r,
Err(_) => return Err("Failed to acquire read lock".into()),
};

if let Some(ref db) = *db {
let mut total_memory_usage: usize = 0;

for id in ids {
let binaries = binaries.clone();

let key = ByteStore::create_key(&id.0, &id.1, &id.2, &id.3);
if let Ok(Some(value)) = db.get(&key) {
/*
This is added here because really large message lists
with large messages are filling up the machines memory
and freezing it.
*/
total_memory_usage += value.len();
if total_memory_usage > max_memory_usage {
return Err(format!(
"Memory usage exceeded the limit: {} bytes",
max_memory_usage
));
}
binaries.insert(id.clone(), value);
}
}
Expand Down
8 changes: 7 additions & 1 deletion servers/su/src/domain/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AoConfig {
pub db_write_connections: u32,
pub db_read_connections: u32,
pub enable_metrics: bool,
pub max_read_memory: usize,
}

impl AoConfig {
Expand Down Expand Up @@ -65,6 +66,10 @@ impl AoConfig {
Ok(val) => val == "true",
Err(_e) => false,
};
let max_read_memory = match env::var("MAX_READ_MEMORY") {
Ok(val) => val.parse().unwrap(),
Err(_e) => 1_073_741_824,
};
Ok(AoConfig {
database_url: env::var("DATABASE_URL")?,
database_read_url,
Expand All @@ -80,6 +85,7 @@ impl AoConfig {
db_write_connections,
db_read_connections,
enable_metrics,
max_read_memory,
})
}
}
Expand All @@ -91,4 +97,4 @@ impl Config for AoConfig {
fn scheduler_list_path(&self) -> String {
self.scheduler_list_path.clone()
}
}
}
Binary file modified servers/su/su
Binary file not shown.

0 comments on commit e93a4ec

Please sign in to comment.