use std::{
sync::{
OnceLock,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
static IS_CN_DONE: OnceLock<bool> = OnceLock::new();
static IS_CN_PROBING: AtomicBool = AtomicBool::new(false);
pub async fn is_cn() -> bool {
let user_region = std::env::var("LONGBRIDGE_REGION")
.ok()
.or_else(|| std::env::var("LONGPORT_REGION").ok());
if let Some(region) = user_region {
return region.eq_ignore_ascii_case("CN");
}
if let Some(&cached) = IS_CN_DONE.get() {
return cached;
}
if IS_CN_PROBING
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
let result = reqwest::Client::new()
.get("https://geotest.lbkrs.com")
.timeout(Duration::from_secs(5))
.send()
.await
.is_ok_and(|resp| resp.status().is_success());
let _ = IS_CN_DONE.set(result);
result
} else {
IS_CN_DONE.get().copied().unwrap_or(false)
}
}