Skip to main content

SubscriptionState

Struct SubscriptionState 

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

Tracks subscription intent and acknowledgment state for WebSocket connections.

§State management

The tracker maintains desired subscription intent and three acknowledgment states:

  • Desired: Topics that should be active, independent of response ordering or connection.
  • Confirmed: Subscriptions acknowledged by the server and expected to stream data.
  • Pending subscribe: Subscribe requests awaiting server acknowledgment.
  • Pending unsubscribe: Unsubscribe requests awaiting server acknowledgment.

Late subscribe acknowledgments do not revive cancelled intent, and stale unsubscribe acknowledgments do not remove a later resubscription.

§Reference counting

Reference counts remain independent of acknowledgment state. The first consumer tells the caller to send a subscribe request, while removing the last tells it to send an unsubscribe request. The tracker records these transitions but does not send protocol messages.

§Topic format

Topics use channel{delimiter}symbol, with delimiters such as . or :. A topic without the delimiter represents a channel-level subscription.

§Thread safety

Clones share all state. Operations are thread-safe and can run concurrently from multiple tasks.

Implementations§

Source§

impl SubscriptionState

Source

pub fn new(delimiter: char) -> Self

Creates a new subscription state tracker with the specified topic delimiter.

Source

pub fn delimiter(&self) -> char

Returns the delimiter character used for topic splitting.

Source

pub fn confirmed(&self) -> SubscriptionSnapshot

Returns a read-only snapshot of confirmed subscriptions.

Source

pub fn pending_subscribe(&self) -> SubscriptionSnapshot

Returns a read-only snapshot of pending subscriptions.

Source

pub fn pending_unsubscribe(&self) -> SubscriptionSnapshot

Returns a read-only snapshot of pending unsubscriptions.

Source

pub fn len(&self) -> usize

Returns the number of confirmed subscriptions.

Counts both channel-level and symbol-level subscriptions.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no subscriptions (confirmed or pending).

Source

pub fn is_subscribed(&self, channel: &Ustr, symbol: &Ustr) -> bool

Returns true if a channel:symbol pair is subscribed (confirmed or pending subscribe).

Source

pub fn pending_subscribe_topics(&self) -> Vec<String>

Returns all pending subscribe topics as strings.

Source

pub fn pending_unsubscribe_topics(&self) -> Vec<String>

Returns all pending unsubscribe topics as strings.

Source

pub fn all_topics(&self) -> Vec<String>

Returns all topics that should be active after reconnect recovery.

The result includes confirmed and pending subscribe topics, but excludes pending unsubscribe topics.

Source

pub fn mark_subscribe(&self, topic: &str)

Marks a topic as pending subscription.

Call this after sending a subscribe request. This operation is idempotent for a confirmed topic and cancels any pending unsubscription for the same topic.

Source

pub fn try_mark_subscribe(&self, topic: &str) -> bool

Atomically tries to mark a topic as pending subscription.

Returns true if the topic was newly marked as pending (should send subscribe). Returns false if the topic was already confirmed or pending (skip sending).

The check and state transition are atomic across concurrent subscribe calls.

Source

pub fn confirm_subscribe(&self, topic: &str)

Confirms a subscription by moving it from pending to confirmed.

Call this when the server acknowledges a subscribe request. A late confirmation cannot restore a topic that is no longer desired.

Source

pub fn mark_unsubscribe(&self, topic: &str)

Marks a topic as pending unsubscription.

Removes the topic from confirmed and pending_subscribe state before adding it to pending_unsubscribe. This also handles unsubscription before initial confirmation.

Source

pub fn confirm_unsubscribe(&self, topic: &str)

Confirms an unsubscription by removing it from pending and confirmed state.

Call this when the server acknowledges an unsubscribe request. A stale acknowledgment is ignored if the topic is no longer pending unsubscription. pending_subscribe remains intact so an immediate resubscription survives a late unsubscribe acknowledgment.

Source

pub fn mark_failure(&self, topic: &str)

Marks a subscription as failed, moving it from confirmed back to pending.

This keeps failed subscriptions available for retry after reconnect. A topic pending unsubscription is unchanged because its subscription was cancelled.

Source

pub fn reset_after_reconnect(&self) -> Vec<String>

Resets acknowledgment state for a replacement connection.

Returns the desired topics to replay. Confirmed topics become pending subscriptions, pending unsubscriptions are completed by the closed connection, and reference counts are preserved.

Source

pub fn add_reference(&self, topic: &str) -> bool

Increments the reference count for a topic.

Returns true if this is the first subscription (caller should send subscribe message to server).

§Panics

Panics if the reference count exceeds usize::MAX subscriptions for a single topic.

Source

pub fn remove_reference(&self, topic: &str) -> bool

Decrements the reference count for a topic.

Returns true if this was the last subscription (caller should send unsubscribe message to server).

§Panics

Panics if the internal reference count state becomes inconsistent (should never happen if the API is used correctly).

Source

pub fn get_reference_count(&self, topic: &str) -> usize

Returns the current reference count for a topic.

Returns 0 if the topic has no references.

Source

pub fn clear(&self)

Clears all subscription state.

This resets desired intent, acknowledgment state, and reference counts.

Trait Implementations§

Source§

impl Clone for SubscriptionState

Source§

fn clone(&self) -> SubscriptionState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SubscriptionState

Source§

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

Formats the value using the given formatter. 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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.
§

impl<T> Ungil for T
where T: Send,

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more