use std::sync::Arc;
use tokio::sync::Semaphore;
pub struct WorkerPool {
semaphore: Arc<Semaphore>,
}
impl WorkerPool {
#[must_use]
pub fn new(max_concurrent: usize) -> Self {
Self {
semaphore: Arc::new(Semaphore::new(max_concurrent.max(1))),
}
}
#[must_use]
pub fn from_env() -> Self {
let max = std::env::var("PHOTON_HANDLER_POOL_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(64);
Self::new(max)
}
pub async fn acquire(&self) -> tokio::sync::OwnedSemaphorePermit {
self.semaphore
.clone()
.acquire_owned()
.await
.expect("semaphore closed")
}
}