Skip to main content

Subscription

Struct Subscription 

Source
pub struct Subscription { /* private fields */ }
Expand description

A handle to one subscription — owns the receive end of the bus channel.

Drop unsubscribes from everything automatically. While the handle is alive, recv / recv_timeout / try_recv drain queued PubsubFrames in arrival order.

Threading. Subscription is Send + SyncArc<Subscription> works, so multiple async tasks (or spawn_blocking jobs) can share one subscription and call recv concurrently. The underlying std::sync::mpsc::Receiver is !Sync, so we wrap it (and the matching ack Sender) in a Mutex; concurrent recv callers serialise on that lock, with each call receiving a different frame in arrival order (single-consumer semantics — NOT broadcast fanout). try_recv is non-blocking even under contention: if the lock is held by a blocking recv, try_recv returns Ok(None) rather than waiting.

If you need broadcast fanout (every subscriber sees every message), open a separate Subscription per consumer — they’re cheap.

Implementations§

Source§

impl Subscription

Source

pub fn subscribe(&mut self, channels: &[&[u8]])

SUBSCRIBE channel [channel ...]. Per-channel Subscribe acks are enqueued onto the receive queue in order.

Source

pub fn psubscribe(&mut self, patterns: &[&[u8]])

PSUBSCRIBE pattern [pattern ...]. Patterns use Redis glob syntax (*, ?, [abc]).

Source

pub fn unsubscribe(&mut self, channels: &[&[u8]])

UNSUBSCRIBE [channel ...]. Empty channels removes every channel subscription this handle holds (matching the Redis wire shape: individual ack frames for each channel that was actually removed, or a single Unsubscribe { channel: None } if none were held).

Source

pub fn punsubscribe(&mut self, patterns: &[&[u8]])

PUNSUBSCRIBE [pattern ...]. Empty patterns removes every pattern.

Source

pub fn recv(&self) -> Result<PubsubFrame>

Block until one frame is queued. Err(io::ErrorKind::UnexpectedEof) once the underlying bus tears down (last Store clone dropped).

Acquires the receiver mutex for the entire blocking wait — other recv/recv_timeout callers serialise behind this one. Concurrent try_recv calls return Ok(None) while a recv is blocked (no wait on the lock); see the type-level doc for the trade-off.

Source

pub fn recv_timeout(&self, dur: Duration) -> Result<PubsubFrame>

Bounded blocking recv. Err(io::ErrorKind::TimedOut) when dur elapses; Err(io::ErrorKind::UnexpectedEof) when the bus is gone.

Source

pub fn try_recv(&self) -> Result<Option<PubsubFrame>>

Non-blocking recv. Ok(None) if the queue is empty; Err(UnexpectedEof) when the bus is gone.

Uses try_lock so a concurrent blocking recv doesn’t make try_recv itself block — lock contention is reported as Ok(None) (semantically: “no frame available right now”). Same shape callers already handle for an empty queue.

Trait Implementations§

Source§

impl Debug for Subscription

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Subscription

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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.