Skip to main content

WorkerRegistry

Struct WorkerRegistry 

Source
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>

Source

pub fn new() -> Self

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn len(&self) -> usize

Number of registered entries, including senders whose receivers closed.

Source

pub async fn is_empty(&self) -> bool

Trait Implementations§

Source§

impl<K: Eq + Hash, T> Default for WorkerRegistry<K, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, T> !RefUnwindSafe for WorkerRegistry<K, T>

§

impl<K, T> Freeze for WorkerRegistry<K, T>

§

impl<K, T> Send for WorkerRegistry<K, T>
where K: Send, T: Send,

§

impl<K, T> Sync for WorkerRegistry<K, T>
where K: Send + Sync, T: Send,

§

impl<K, T> Unpin for WorkerRegistry<K, T>

§

impl<K, T> UnsafeUnpin for WorkerRegistry<K, T>

§

impl<K, T> UnwindSafe for WorkerRegistry<K, T>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.