SubscriptionManager

Struct SubscriptionManager 

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

Subscription manager with capacity limits.

This struct manages WebSocket subscriptions with a configurable maximum limit to prevent resource exhaustion. It uses DashMap for lock-free concurrent access.

§Example

use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert_eq!(manager.count(), 0);
assert_eq!(manager.remaining_capacity(), 100);
assert_eq!(manager.max_subscriptions(), 100);

Implementations§

Source§

impl SubscriptionManager

Source

pub fn new(max_subscriptions: usize) -> SubscriptionManager

Creates a new subscription manager with the specified maximum capacity.

§Arguments
  • max_subscriptions - Maximum number of subscriptions allowed
§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(50);
assert_eq!(manager.max_subscriptions(), 50);
Source

pub fn with_default_capacity() -> SubscriptionManager

Creates a new subscription manager with the default maximum capacity (100).

§Example
use ccxt_core::ws_client::{SubscriptionManager, DEFAULT_MAX_SUBSCRIPTIONS};

let manager = SubscriptionManager::with_default_capacity();
assert_eq!(manager.max_subscriptions(), DEFAULT_MAX_SUBSCRIPTIONS);
Source

pub fn max_subscriptions(&self) -> usize

Returns the maximum number of subscriptions allowed.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(75);
assert_eq!(manager.max_subscriptions(), 75);
Source

pub fn try_add( &self, key: String, subscription: Subscription, ) -> Result<(), Error>

Attempts to add a subscription.

Returns Ok(()) if the subscription was added successfully, or Err(Error::ResourceExhausted) if the maximum capacity has been reached.

If a subscription with the same key already exists, it will be replaced without counting against the capacity limit.

§Arguments
  • key - Unique subscription key (typically “channel:symbol”)
  • subscription - The subscription metadata
§Errors

Returns Error::ResourceExhausted if the subscription count has reached the maximum capacity and the key doesn’t already exist.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(2);

// First two subscriptions succeed
// (Note: try_add requires internal Subscription type, this is conceptual)
assert_eq!(manager.count(), 0);
assert_eq!(manager.remaining_capacity(), 2);
Source

pub fn remove(&self, key: &str) -> Option<Subscription>

Removes a subscription by key.

Returns the removed subscription if it existed, or None if not found.

§Arguments
  • key - The subscription key to remove
§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
// After adding and removing a subscription:
// let removed = manager.remove("ticker:BTC/USDT");
Source

pub fn count(&self) -> usize

Returns the current number of active subscriptions.

This operation is lock-free and thread-safe.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert_eq!(manager.count(), 0);
Source

pub fn remaining_capacity(&self) -> usize

Returns the remaining capacity for new subscriptions.

This is calculated as max_subscriptions - current_count.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert_eq!(manager.remaining_capacity(), 100);
Source

pub fn contains(&self, key: &str) -> bool

Checks if a subscription exists for the given key.

§Arguments
  • key - The subscription key to check
§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert!(!manager.contains("ticker:BTC/USDT"));
Source

pub fn get(&self, key: &str) -> Option<Ref<'_, String, Subscription>>

Returns a reference to the subscription for the given key, if it exists.

§Arguments
  • key - The subscription key to look up
Source

pub fn clear(&self)

Clears all subscriptions.

This removes all subscriptions from the manager, freeing all capacity.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
// After adding subscriptions:
manager.clear();
assert_eq!(manager.count(), 0);
assert_eq!(manager.remaining_capacity(), 100);
Source

pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Subscription>>

Returns an iterator over all subscriptions.

The iterator yields (key, subscription) pairs.

Source

pub fn collect_subscriptions(&self) -> Vec<Subscription>

Collects all subscriptions into a vector.

This is useful when you need to iterate over subscriptions while potentially modifying the manager.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
let subs = manager.collect_subscriptions();
assert!(subs.is_empty());
Source

pub fn is_full(&self) -> bool

Checks if the manager is at full capacity.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert!(!manager.is_full());
Source

pub fn is_empty(&self) -> bool

Checks if the manager has no subscriptions.

§Example
use ccxt_core::ws_client::SubscriptionManager;

let manager = SubscriptionManager::new(100);
assert!(manager.is_empty());

Trait Implementations§

Source§

impl Debug for SubscriptionManager

Source§

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

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

impl Default for SubscriptionManager

Source§

fn default() -> SubscriptionManager

Returns the “default value” for a type. 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> Instrument for T

Source§

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

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

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> PolicyExt for T
where T: ?Sized,

Source§

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

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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