use std::fs;
use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use crate::error::{AppError, AppResult, ErrorKind};
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::with_source(ErrorKind::Internal, "concurrency semaphore closed", 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::with_source(
ErrorKind::Internal,
"cpu worker panicked during parse",
e,
)),
Err(e) => Err(AppError::with_source(
ErrorKind::Internal,
"cpu worker join failed",
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
}
fn free_ram_mib() -> Option<u64> {
#[cfg(target_os = "linux")]
{
let text = fs::read_to_string("/proc/meminfo").ok()?;
for line in text.lines() {
if let Some(rest) = line.strip_prefix("MemAvailable:") {
let kb: u64 = rest.split_whitespace().next()?.parse().ok()?;
return Some(kb / 1024);
}
}
None
}
#[cfg(not(target_os = "linux"))]
{
let _ = fs::metadata;
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[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::new(ErrorKind::Parse, "synthetic parse failure"))
})
.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);
}
}