#![allow(clippy::expect_used)]
#![allow(clippy::unwrap_used)]
use std::collections::HashSet;
use std::sync::{
Mutex,
atomic::{AtomicU64, Ordering},
};
use std::time::Duration;
static ORCH_PROCESSING_DELAY_MS: AtomicU64 = AtomicU64::new(0);
static ORCH_DELAY_INSTANCES: Mutex<Option<HashSet<String>>> = Mutex::new(None);
pub fn set_orch_processing_delay(delay: Duration, instance_prefix: Option<&str>) {
ORCH_PROCESSING_DELAY_MS.store(delay.as_millis() as u64, Ordering::SeqCst);
if let Some(prefix) = instance_prefix {
let mut guard = ORCH_DELAY_INSTANCES.lock().unwrap();
let set = guard.get_or_insert_with(HashSet::new);
set.insert(prefix.to_string());
}
}
pub fn get_orch_processing_delay(instance: &str) -> Option<Duration> {
let ms = ORCH_PROCESSING_DELAY_MS.load(Ordering::SeqCst);
if ms == 0 {
return None;
}
let guard = ORCH_DELAY_INSTANCES.lock().unwrap();
if let Some(prefixes) = guard.as_ref()
&& !prefixes.is_empty()
&& !prefixes.iter().any(|p| instance.starts_with(p))
{
return None;
}
Some(Duration::from_millis(ms))
}
pub fn clear_orch_processing_delay() {
ORCH_PROCESSING_DELAY_MS.store(0, Ordering::SeqCst);
let mut guard = ORCH_DELAY_INSTANCES.lock().unwrap();
*guard = None;
}