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
impl SubscriptionManager
Sourcepub fn new(max_subscriptions: usize) -> SubscriptionManager
pub fn new(max_subscriptions: usize) -> SubscriptionManager
Sourcepub fn with_default_capacity() -> SubscriptionManager
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);Sourcepub fn max_subscriptions(&self) -> usize
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);Sourcepub fn try_add(
&self,
key: String,
subscription: Subscription,
) -> Result<(), Error>
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);Sourcepub fn remove(&self, key: &str) -> Option<Subscription>
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");Sourcepub fn count(&self) -> usize
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);Sourcepub fn remaining_capacity(&self) -> usize
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);Sourcepub fn get(&self, key: &str) -> Option<Ref<'_, String, Subscription>>
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
Sourcepub fn clear(&self)
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);Sourcepub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Subscription>>
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Subscription>>
Returns an iterator over all subscriptions.
The iterator yields (key, subscription) pairs.
Sourcepub fn collect_subscriptions(&self) -> Vec<Subscription>
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());