use std::future::Future;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use tokio::sync::watch;
use tracing::{info, warn};
use super::{DhtEngine, DhtEngineState, DhtEngineStats};
impl DhtEngine {
pub(super) async fn spawn_bootstrap(self: &Arc<Self>) {
if self.context.shutdown_requested.load(Ordering::Acquire) {
return;
}
let context = Arc::clone(&self.context);
let task_queue = Arc::clone(&self.task_queue);
let mut shutdown_rx = self.shutdown_tx.subscribe();
let limit = context.config.bootstrap_timeout;
self.register_background_task(async move {
let bootstrap = async {
if tokio::time::timeout(limit, context.bootstrap(&task_queue))
.await
.is_err()
&& !context.shutdown_requested.load(Ordering::Acquire)
{
warn!(
timeout = ?limit,
"DHT bootstrap timed out; continuing without entry-point nodes"
);
context.inner.write().await.state = DhtEngineState::Running;
let _ = context.state_updates.send(DhtEngineState::Running);
}
};
tokio::select! {
_ = bootstrap => {}
_ = shutdown_rx.changed() => {}
}
})
.await;
}
pub async fn state(&self) -> DhtEngineState {
let inner = self.context.inner.read().await;
if self.context.shutdown_requested.load(Ordering::Acquire) {
DhtEngineState::ShuttingDown
} else {
inner.state
}
}
pub fn subscribe_state(&self) -> watch::Receiver<DhtEngineState> {
self.context.state_updates.subscribe()
}
pub async fn wait_until_ready(&self, timeout: Duration) -> std::io::Result<()> {
let mut updates = self.context.state_updates.subscribe();
let wait = async {
loop {
match self.state().await {
DhtEngineState::Running => return Ok(()),
DhtEngineState::ShuttingDown | DhtEngineState::Stopped => {
return Err(std::io::Error::new(
std::io::ErrorKind::Interrupted,
"DHT engine is not running",
));
}
DhtEngineState::Bootstrapping => updates.changed().await.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Interrupted,
"DHT engine state updates are closed",
)
})?,
}
}
};
tokio::time::timeout(timeout, wait).await.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::TimedOut,
"DHT bootstrap did not become ready before the timeout",
)
})?
}
pub fn shutdown(&self) {
let first_shutdown = !self.context.shutdown_requested.swap(true, Ordering::AcqRel);
if first_shutdown {
let _ = self.shutdown_tx.send(true);
if let Ok(mut inner) = self.context.inner.try_write() {
inner.state = DhtEngineState::ShuttingDown;
let _ = self
.context
.state_updates
.send(DhtEngineState::ShuttingDown);
}
info!("DHT shutdown signal sent");
}
}
pub async fn shutdown_async(&self) {
self.shutdown();
self.task_queue.shutdown().await;
let mut join_set = self.background_tasks.lock().await;
let wait_for_tasks = async { while join_set.join_next().await.is_some() {} };
if tokio::time::timeout(Duration::from_millis(100), wait_for_tasks)
.await
.is_err()
{
join_set.abort_all();
while join_set.join_next().await.is_some() {}
}
self.context.inner.write().await.state = DhtEngineState::ShuttingDown;
let _ = self
.context
.state_updates
.send(DhtEngineState::ShuttingDown);
if self.context.config.dht_file_path.is_some()
&& let Err(error) = self.context.save_state().await
{
warn!("DHT shutdown save failed: {error}");
}
info!("DHT engine shutdown complete");
}
pub async fn stats(&self) -> DhtEngineStats {
let inner = self.context.inner.read().await;
let routing_table = self.context.task_context.routing_table.read().await;
let state = if self.context.shutdown_requested.load(Ordering::Acquire) {
DhtEngineState::ShuttingDown
} else {
inner.state
};
let peer_storage = self.context.peer_storage.stats();
DhtEngineStats {
total_nodes: routing_table.total_node_count(),
good_nodes: routing_table.good_node_count(),
pending_transactions: self.context.task_context.tracker.pending_count(),
questionable_nodes: routing_table.questionable_node_count(),
bad_nodes: routing_table.bad_node_count(),
cached_nodes: routing_table
.get_all_buckets()
.iter()
.map(|bucket| bucket.cached_nodes().len())
.sum(),
bucket_count: routing_table.num_buckets(),
peer_info_hashes: peer_storage.info_hashes,
stored_peers: peer_storage.peers,
peer_storage_evictions: peer_storage.evictions,
max_peer_info_hashes: peer_storage.max_info_hashes,
persistence_enabled: self.context.config.dht_file_path.is_some(),
persistence_max_age_secs: self.context.config.persistence_max_age.as_secs(),
cleanup_interval_secs: self.context.config.cleanup_interval.as_secs(),
save_interval_secs: self.context.config.save_interval.as_secs(),
state,
}
}
pub(in crate::bittorrent::dht) async fn register_background_task<F>(&self, task: F)
where
F: Future<Output = ()> + Send + 'static,
{
let mut background_tasks = self.background_tasks.lock().await;
if !self.context.shutdown_requested.load(Ordering::Acquire) {
background_tasks.spawn(task);
}
}
}
impl Drop for DhtEngine {
fn drop(&mut self) {
self.task_queue.cancel();
self.background_tasks.get_mut().abort_all();
}
}