use athena_gateway::{
GatewayFetchRowsResult, GatewayFetchSingleflight, GatewayFetchSingleflightRole,
GatewayInFlightFetch,
};
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::time::Duration;
use crate::config_validation::runtime_env_settings;
static FETCH_SINGLEFLIGHT: Lazy<GatewayFetchSingleflight> = Lazy::new(|| {
let timeout_ms = runtime_env_settings()
.fetch_singleflight_wait_timeout_ms
.max(1);
GatewayFetchSingleflight::new(Duration::from_millis(timeout_ms))
});
pub(crate) type InFlightFetch = GatewayInFlightFetch;
pub(crate) type SingleflightRole = GatewayFetchSingleflightRole;
pub(crate) async fn acquire_fetch_singleflight(cache_key: &str) -> SingleflightRole {
FETCH_SINGLEFLIGHT.acquire(cache_key).await
}
pub(crate) async fn publish_fetch_singleflight_result(
cache_key: &str,
flight: Arc<InFlightFetch>,
result: GatewayFetchRowsResult,
) {
FETCH_SINGLEFLIGHT
.publish_result(cache_key, flight, result)
.await;
}
pub(crate) async fn wait_for_fetch_singleflight_result(
flight: Arc<InFlightFetch>,
) -> Option<GatewayFetchRowsResult> {
FETCH_SINGLEFLIGHT.wait_for_result(flight).await
}