use std::fs;
use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use crate::error::{AppError, AppResult, ErrorDetail, InternalOp};
pub const RAM_PER_TASK_MIB: u64 = 48;
pub const MAX_AUTO_CONCURRENCY: usize = 32;
pub const MAX_EXPLICIT_CONCURRENCY: usize = 256;
#[derive(Debug, Clone)]
pub struct ConcurrencyBudget {
max: usize,
semaphore: Arc<Semaphore>,
}
impl ConcurrencyBudget {
pub fn new(max: usize) -> Self {
let max = max.max(1);
Self {
max,
semaphore: Arc::new(Semaphore::new(max)),
}
}
pub fn from_configured(configured: u32) -> Self {
Self::new(resolve_max_concurrency(configured))
}
pub fn max(&self) -> usize {
self.max
}
pub fn available_permits(&self) -> usize {
self.semaphore.available_permits()
}
pub fn semaphore(&self) -> Arc<Semaphore> {
Arc::clone(&self.semaphore)
}
pub async fn acquire_owned(&self) -> AppResult<OwnedSemaphorePermit> {
self.semaphore.clone().acquire_owned().await.map_err(|e| {
AppError::of_with_source(
ErrorDetail::Internal {
op: InternalOp::SemaphoreClosed,
},
e,
)
})
}
pub async fn run_cpu_bound<F, T>(&self, f: F) -> AppResult<T>
where
F: FnOnce() -> AppResult<T> + Send + 'static,
T: Send + 'static,
{
let permit = self.acquire_owned().await?;
let join = tokio::task::spawn_blocking(move || {
let _permit = permit;
f()
});
match join.await {
Ok(inner) => inner,
Err(e) if e.is_cancelled() => Err(AppError::interrupted()),
Err(e) if e.is_panic() => Err(AppError::of_with_source(
ErrorDetail::Internal {
op: InternalOp::WorkerPanic,
},
e,
)),
Err(e) => Err(AppError::of_with_source(
ErrorDetail::Internal {
op: InternalOp::WorkerJoin,
},
e,
)),
}
}
}
pub fn resolve_max_concurrency(configured: u32) -> usize {
if configured > 0 {
return (configured as usize).clamp(1, MAX_EXPLICIT_CONCURRENCY);
}
auto_max_concurrency()
}
pub fn auto_max_concurrency() -> usize {
let cpus = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(2)
.max(1);
let ram_based = match free_ram_mib() {
Some(free) => {
let usable = free / 2; ((usable / RAM_PER_TASK_MIB).max(1) as usize).max(1)
}
None => cpus,
};
cpus.min(ram_based).clamp(1, MAX_AUTO_CONCURRENCY)
}
pub fn runtime_worker_threads() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(2)
.clamp(1, MAX_AUTO_CONCURRENCY)
}
pub fn max_blocking_threads() -> usize {
MAX_EXPLICIT_CONCURRENCY
}
const LINUX_MEMINFO_PATH: &str = "/proc/meminfo";
const MEM_AVAILABLE_PREFIX: &str = "MemAvailable:";
const KIB_PER_MIB: u64 = 1024;
fn free_ram_mib() -> Option<u64> {
if !cfg!(target_os = "linux") {
return None;
}
let text = fs::read_to_string(LINUX_MEMINFO_PATH).ok()?;
parse_mem_available_mib(&text)
}
fn parse_mem_available_mib(text: &str) -> Option<u64> {
for line in text.lines() {
if let Some(rest) = line.strip_prefix(MEM_AVAILABLE_PREFIX) {
let kib: u64 = rest.split_whitespace().next()?.parse().ok()?;
return Some(kib / KIB_PER_MIB);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::ErrorKind;
const MEMINFO_SAMPLE: &str = "MemTotal: 16341234 kB\nMemFree: 1234567 kB\nMemAvailable: 8388608 kB\nBuffers: 123456 kB\n";
#[test]
fn parses_mem_available_into_mib() {
assert_eq!(parse_mem_available_mib(MEMINFO_SAMPLE), Some(8192));
}
#[test]
fn missing_field_yields_none() {
let text = "MemTotal: 16341234 kB\nMemFree: 1234567 kB\n";
assert_eq!(parse_mem_available_mib(text), None);
}
#[test]
fn non_numeric_value_yields_none() {
assert_eq!(
parse_mem_available_mib("MemAvailable: not-a-number kB\n"),
None
);
}
#[test]
fn truncated_line_without_value_yields_none() {
assert_eq!(parse_mem_available_mib("MemAvailable:\n"), None);
}
#[test]
fn empty_input_yields_none() {
assert_eq!(parse_mem_available_mib(""), None);
}
#[test]
fn free_ram_is_none_or_positive_on_every_platform() {
if let Some(mib) = free_ram_mib() {
assert!(mib > 0, "MemAvailable reported {mib} MiB");
}
}
#[test]
fn configured_override_respected() {
assert_eq!(resolve_max_concurrency(4), 4);
assert_eq!(resolve_max_concurrency(1), 1);
}
#[test]
fn auto_is_at_least_one() {
assert!(auto_max_concurrency() >= 1);
assert!(auto_max_concurrency() <= MAX_AUTO_CONCURRENCY);
}
#[test]
fn runtime_and_blocking_pool_caps_are_bounded() {
let w = runtime_worker_threads();
assert!((1..=MAX_AUTO_CONCURRENCY).contains(&w));
assert_eq!(max_blocking_threads(), MAX_EXPLICIT_CONCURRENCY);
}
#[test]
fn budget_reports_max_and_available() {
let b = ConcurrencyBudget::new(3);
assert_eq!(b.max(), 3);
assert_eq!(b.available_permits(), 3);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn run_cpu_bound_returns_value_and_releases_permit() {
let b = ConcurrencyBudget::new(2);
let out = b.run_cpu_bound(|| Ok::<_, AppError>(41 + 1)).await.unwrap();
assert_eq!(out, 42);
assert_eq!(b.available_permits(), 2);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn run_cpu_bound_propagates_closure_error() {
let b = ConcurrencyBudget::new(1);
let err = b
.run_cpu_bound(|| -> AppResult<()> {
Err(AppError::of(ErrorDetail::Internal {
op: InternalOp::SyntheticParseFailure,
}))
})
.await
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::Parse);
assert_eq!(b.available_permits(), 1);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn peak_concurrency_never_exceeds_max() {
use std::sync::atomic::{AtomicUsize, Ordering};
let b = ConcurrencyBudget::new(2);
let peak = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let mut handles = Vec::new();
for _ in 0..8 {
let b = b.clone();
let peak = Arc::clone(&peak);
let current = Arc::clone(¤t);
handles.push(tokio::spawn(async move {
b.run_cpu_bound(move || {
let now = current.fetch_add(1, Ordering::SeqCst) + 1;
peak.fetch_max(now, Ordering::SeqCst);
std::thread::sleep(std::time::Duration::from_millis(15));
current.fetch_sub(1, Ordering::SeqCst);
Ok::<_, AppError>(())
})
.await
}));
}
for h in handles {
h.await.unwrap().unwrap();
}
assert!(
peak.load(Ordering::SeqCst) <= 2,
"peak {} exceeded budget 2",
peak.load(Ordering::SeqCst)
);
assert_eq!(b.available_permits(), 2);
}
}