use anyhow::Context;
use std::{
sync::{Arc, Weak},
time::Duration,
};
use tracing::debug;
use tor_chanmgr::ChanMgr;
use tor_rtcompat::Runtime;
pub(crate) struct ChannelHouseKeepingTask<R: Runtime> {
mgr: Weak<ChanMgr<R>>,
}
impl<R: Runtime> ChannelHouseKeepingTask<R> {
const START_TICK_TIME: Duration = Duration::from_secs(180);
pub(crate) fn new(mgr: &Arc<ChanMgr<R>>) -> Self {
Self {
mgr: Arc::downgrade(mgr),
}
}
#[allow(clippy::unused_async)] async fn run(&mut self) -> anyhow::Result<Duration> {
let mgr = Weak::upgrade(&self.mgr).context("Channel manager is gone")?;
let next_expiry = mgr.expire_channels();
Ok(next_expiry)
}
pub(crate) async fn start(&mut self) -> anyhow::Result<void::Void> {
let mut next_tick_in = Self::START_TICK_TIME;
debug!("Channel housekeeping task starting.");
loop {
tokio::time::sleep(next_tick_in).await;
next_tick_in = self
.run()
.await
.context("Shutting down channel housekeeping task")?;
}
}
}