use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Debug, Clone, thiserror::Error)]
#[error("query busy for session {0}")]
pub struct ErrQueryBusy(pub String);
#[derive(Clone, Default)]
pub struct QueryGate {
inflight: Arc<Mutex<HashMap<String, bool>>>,
}
impl QueryGate {
pub fn new() -> Self {
Self::default()
}
pub async fn acquire(&self, session_id: &str) -> Result<(), ErrQueryBusy> {
let mut map = self.inflight.lock().await;
if map.get(session_id).copied().unwrap_or(false) {
return Err(ErrQueryBusy(session_id.to_string()));
}
map.insert(session_id.to_string(), true);
Ok(())
}
pub async fn cancel(&self, _session_id: &str) {}
pub async fn release(&self, session_id: &str) {
self.inflight.lock().await.remove(session_id);
}
}