pub struct Publisher<T> { /* private fields */ }Expand description
The sending end of a gyre channel.
Publisher is responsible for sending events to the shared ring buffer.
It is Send and Sync, and can be cloned to support multiple concurrent producers.
The channel is closed when the last Publisher (including all its clones) is dropped.
Implementations§
Source§impl<T> Publisher<T>
impl<T> Publisher<T>
Sourcepub async fn publish(&self, event: T) -> Result<(), T>
pub async fn publish(&self, event: T) -> Result<(), T>
Asynchronously publishes an event to the channel.
This method first claims an exclusive slot in the ring buffer, then waits if necessary for the slowest consumer to advance (asynchronous backpressure). Once a slot is confirmed to be free, it writes the event and makes it visible to all consumers.
§Arguments
event: The event to be published.
§Returns
Ok(()): If the event was successfully published.Err(T): If there are currently no activeConsumers. In this case, the event is not published and is returned to the caller.
§Cancellation Safety
This method is cancellation safe. A publisher task first acquires a temporary,
exclusive lock to “claim” a slot. If the future is cancelled at any await
point (e.g., while waiting for backpressure), the lock is automatically
released, and the global state remains consistent. No sequence number “gap”
is created.
The next publisher will simply acquire the lock and attempt to publish to the
same slot. It is safe to use this method in tokio::select!, timeouts, etc.
Sourcepub async fn subscribe(&self) -> Consumer<T>
pub async fn subscribe(&self) -> Consumer<T>
Dynamically subscribes a new Consumer to the event bus.
The newly created Consumer will receive all events that are newly published
after this method call completes. It will not receive any historical events that
were already in the ring buffer before the subscription.
This is different from consumer.clone(), which creates a copy that starts
consuming from the same position.
§Returns
A new Consumer instance.
§Cancellation Safety
This method is cancellation safe. If the future is cancelled, it will be
before any changes to the shared state are made. The new consumer is only
registered with the bus after all await points have completed. It is safe
to use this method in contexts like tokio::select!.
Trait Implementations§
Source§impl<T> Clone for Publisher<T>
impl<T> Clone for Publisher<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones a Publisher.
All cloned Publisher instances share the same publish sequence number, allowing
multiple producers to publish to the same channel in a thread-safe manner.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more