pub struct WorkerRegistry<K, T> {
workers: DashMap<K, Sender<T>>,
}Expand description
Registers worker senders by key, without owning workers or their execution.
Closed senders are replaced on lookup/creation. Cleanup checks channel identity under the same lock as replacement, so an old worker cannot remove a replacement registered under its key.
Fields§
§workers: DashMap<K, Sender<T>>Implementations§
Source§impl<K: Eq + Hash, T> WorkerRegistry<K, T>
impl<K: Eq + Hash, T> WorkerRegistry<K, T>
pub fn new() -> Self
Sourcepub async fn get(&self, key: &K) -> Option<Sender<T>>
pub async fn get(&self, key: &K) -> Option<Sender<T>>
Returns a live sender, if one is currently registered. The receiver can close after this method returns; callers must handle a failed send and retry worker lookup without discarding the unsent item.
Sourcepub async fn get_or_create(
&self,
key: K,
capacity: usize,
) -> (Sender<T>, Option<Receiver<T>>)
pub async fn get_or_create( &self, key: K, capacity: usize, ) -> (Sender<T>, Option<Receiver<T>>)
Returns the registered sender and, only when created, its receiver.
Start the new worker before the next await so cancellation cannot leave
a registered channel without a consumer. Existing channels retain their
original capacity. New channel capacity must satisfy mpsc::channel.
Sourcepub async fn get_or_insert_with<F>(&self, key: K, create: F) -> Sender<T>where
F: FnOnce() -> Sender<T>,
pub async fn get_or_insert_with<F>(&self, key: K, create: F) -> Sender<T>where
F: FnOnce() -> Sender<T>,
Reuses a live sender or atomically creates its replacement.
create runs synchronously under the registry shard lock. It should only
prepare the sender and capture any initialization state (such as the
receiver) for the caller; start the worker after this method returns.
Do not block or reenter the registry from create.
Sourcepub async fn remove_if_same(&self, key: &K, tx: &Sender<T>) -> bool
pub async fn remove_if_same(&self, key: &K, tx: &Sender<T>) -> bool
Removes the key only if it still points to this worker’s channel.
Sourcepub async fn len(&self) -> usize
pub async fn len(&self) -> usize
Number of registered entries, including senders whose receivers closed.