SubscriptionState

Struct SubscriptionState 

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

Generic subscription state tracker for WebSocket connections.

Maintains three separate states for subscriptions:

  • Confirmed: Successfully subscribed and actively streaming data.
  • Pending Subscribe: Subscription requested but not yet confirmed by server.
  • Pending Unsubscribe: Unsubscription requested but not yet confirmed by server.

§Reference Counting

The tracker maintains reference counts for each topic. When multiple components subscribe to the same topic, only the first subscription sends a message to the server. Similarly, only the last unsubscription sends an unsubscribe message.

§Thread Safety

All operations are thread-safe and can be called 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) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>

Returns a clone of the confirmed subscriptions map.

Source

pub fn pending_subscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>

Returns a clone of the pending subscribe map.

Source

pub fn pending_unsubscribe(&self) -> Arc<DashMap<Ustr, AHashSet<Ustr>>>

Returns a clone of the pending unsubscribe map.

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 mark_subscribe(&self, topic: &str)

Marks a topic as pending subscription.

This should be called after sending a subscribe request to the server. Idempotent: if topic is already confirmed, this is a no-op. If topic is pending unsubscription, removes it.

Source

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

Marks a topic as pending unsubscription.

This removes the topic from both confirmed and pending_subscribe, then adds it to pending_unsubscribe. This handles the case where a user unsubscribes before the initial subscription is confirmed.

Source

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

Confirms a subscription by moving it from pending to confirmed.

This should be called when the server acknowledges a subscribe request. Ignores the confirmation if the topic is pending unsubscription (handles late confirmations after user has already unsubscribed).

Source

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

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

This should be called when the server acknowledges an unsubscribe request. Removes the topic from pending_unsubscribe and confirmed. Does NOT clear pending_subscribe to support immediate re-subscribe patterns (e.g., user calls subscribe() before unsubscribe ack arrives).

Stale ACK handling: Ignores unsubscribe ACKs if the topic is no longer in pending_unsubscribe (meaning user has already re-subscribed). This prevents stale ACKs from removing topics that were re-confirmed after the re-subscribe.

Source

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

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

This is useful when a subscription fails but should be retried on reconnect. Ignores the failure if the topic is pending unsubscription (user cancelled it).

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 (confirmed + pending_subscribe).

This is the key method for reconnection: it returns all topics that should be resubscribed after a connection is re-established.

Note: Does NOT include pending_unsubscribe topics, as those are being removed.

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 is useful when reconnecting or resetting the client.

Trait Implementations§

Source§

impl Clone for SubscriptionState

Source§

fn clone(&self) -> SubscriptionState

Returns a duplicate of the value. Read more
1.0.0 · 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
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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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
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 = 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.
§

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
§

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