#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
use crate::cacheable::Cacheable;
#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
use crate::punnu::pool::PunnuInner;
#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
use std::sync::Arc;
#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
use std::sync::Weak;
#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
use tokio::sync::Notify;
#[cfg(any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
))]
pub(crate) fn spawn_sweep<T: Cacheable>(
weak: Weak<PunnuInner<T>>,
interval: std::time::Duration,
sweep_initialised: Arc<Notify>,
) {
let Some(inner_for_exec) = weak.upgrade() else {
return;
};
let executor = inner_for_exec.executor.clone();
drop(inner_for_exec);
let exec_for_loop = executor.clone();
executor.spawn(Box::pin(async move {
sweep_initialised.notify_one();
loop {
exec_for_loop.sleep(interval).await;
let Some(inner) = weak.upgrade() else { break };
let now = inner.executor.now();
let expired_ids = {
let snapshot = inner.l1.load_full();
snapshot
.entries
.iter()
.filter(|(_, entry)| entry.is_expired_at(now))
.map(|(id, _)| id.clone())
.collect::<Vec<_>>()
};
#[cfg(all(
test,
any(
all(feature = "runtime-tokio", not(target_arch = "wasm32")),
all(feature = "runtime-wasm", target_arch = "wasm32"),
)
))]
inner
.pause_next_sweep_after_candidates_for_test(!expired_ids.is_empty())
.await;
if expired_ids.is_empty() {
continue;
}
if !inner.sweep_expired(expired_ids) {
break;
}
}
}));
}