use anyhow::Context;
use once_cell::sync::Lazy;
use smol::lock::{Semaphore, SemaphoreGuardArc};
use smol_timeout::TimeoutExt;
use std::{sync::Arc, time::Duration};
#[cfg(target_os = "macos")]
static FD_SEMAPHORE: Lazy<Arc<Semaphore>> = Lazy::new(|| Arc::new(Semaphore::new(64)));
#[cfg(not(target_os = "macos"))]
static FD_SEMAPHORE: Lazy<Arc<Semaphore>> = Lazy::new(|| Arc::new(Semaphore::new(512)));
pub(crate) async fn acquire_fd() -> anyhow::Result<SemaphoreGuardArc> {
FD_SEMAPHORE
.acquire_arc()
.timeout(Duration::from_millis(300))
.await
.context("could not acquire")
}