use std::{num::NonZeroUsize, sync::Arc, time::Duration};
use tokio::sync::{Notify, RwLock, Semaphore, mpsc};
use tokio_util::sync::CancellationToken;
use crate::{core::TaskDefaults, events::Bus};
mod admission;
mod completion;
mod listener;
mod protocol;
mod query;
mod removal;
mod scheduler;
mod state;
pub(crate) use completion::{OutcomeTx, RemovalCompletion};
#[allow(unused_imports)]
pub(crate) use protocol::{
AddBatchItem, AddReply, AddReplyRx, CancelDecision, CancelReply, CancelReplyRx,
RegistryCommand, RemoveReply, RemoveReplyRx,
};
use listener::ListenerState;
use removal::PendingJoins;
use scheduler::ActorRuntime;
use state::Inner;
#[cfg(test)]
use removal::{JoinCompletion, RemovalReport, TerminalFinalizer};
#[cfg(test)]
use state::{Entry, EntryState, Handle, HandleCleanup};
pub(crate) struct Registry {
state: Arc<RwLock<Inner>>,
bus: Bus,
runtime_token: CancellationToken,
semaphore: Option<Arc<Semaphore>>,
grace: Duration,
task_defaults: TaskDefaults,
max_registered_tasks: Option<NonZeroUsize>,
empty_notify: Arc<Notify>,
pending_joins: Arc<PendingJoins>,
actors: ActorRuntime,
listener: ListenerState,
}
impl Registry {
pub fn new(
bus: Bus,
runtime_token: CancellationToken,
semaphore: Option<Arc<Semaphore>>,
grace: Duration,
task_defaults: TaskDefaults,
max_registered_tasks: Option<NonZeroUsize>,
cmd_rx: mpsc::Receiver<RegistryCommand>,
) -> Arc<Self> {
Arc::new(Self {
state: Arc::new(RwLock::new(Inner::default())),
bus,
runtime_token,
semaphore,
grace,
task_defaults,
max_registered_tasks,
empty_notify: Arc::new(Notify::new()),
pending_joins: Arc::new(PendingJoins::default()),
actors: ActorRuntime::new(),
listener: ListenerState::new(cmd_rx),
})
}
}
#[cfg(test)]
mod tests;