use std::fmt;
use super::executor::DhtTaskExecutor;
use super::{BoxedDhtTask, DEFAULT_NUM_CONCURRENT};
pub struct DhtTaskQueue {
periodic_executor_1: DhtTaskExecutor,
periodic_executor_2: DhtTaskExecutor,
immediate_executor: DhtTaskExecutor,
}
impl DhtTaskQueue {
pub fn new() -> Self {
Self::with_concurrency(DEFAULT_NUM_CONCURRENT)
}
pub fn with_concurrency(num_concurrent: usize) -> Self {
Self {
periodic_executor_1: DhtTaskExecutor::new(num_concurrent),
periodic_executor_2: DhtTaskExecutor::new(num_concurrent),
immediate_executor: DhtTaskExecutor::new(num_concurrent),
}
}
pub async fn add_periodic_task_1(&self, task: BoxedDhtTask) -> bool {
self.periodic_executor_1.add_task(task).await
}
pub async fn try_add_periodic_task_1_if_idle(&self, task: BoxedDhtTask) -> bool {
self.periodic_executor_1.try_add_task_if_idle(task).await
}
pub async fn add_periodic_task_2(&self, task: BoxedDhtTask) -> bool {
self.periodic_executor_2.add_task(task).await
}
pub async fn try_add_periodic_task_2_if_idle(&self, task: BoxedDhtTask) -> bool {
self.periodic_executor_2.try_add_task_if_idle(task).await
}
pub async fn add_immediate_task(&self, task: BoxedDhtTask) -> bool {
self.immediate_executor.add_task(task).await
}
pub async fn shutdown(&self) {
tokio::join!(
self.periodic_executor_1.shutdown(),
self.periodic_executor_2.shutdown(),
self.immediate_executor.shutdown(),
);
}
pub fn cancel(&self) {
self.periodic_executor_1.cancel();
self.periodic_executor_2.cancel();
self.immediate_executor.cancel();
}
}
impl Default for DhtTaskQueue {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for DhtTaskQueue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DhtTaskQueue").finish()
}
}