pub struct ClientManager { /* private fields */ }Expand description
Manages all connected WebSocket clients using lock-free DashMap.
Key design decisions:
- Uses DashMap for lock-free concurrent access to client registry
- Uses try_send instead of send to never block on slow clients
- Disconnects clients that are backpressured (queue full) to prevent cascade failures
- All public methods are non-blocking or use fine-grained per-key locks
Implementations§
Source§impl ClientManager
impl ClientManager
pub fn new() -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_message_queue_size(self, queue_size: usize) -> Self
Sourcepub fn add_client(&self, client_id: Uuid, ws_sender: WebSocketSender)
pub fn add_client(&self, client_id: Uuid, ws_sender: WebSocketSender)
Add a new client connection.
Spawns a dedicated sender task for this client that reads from its mpsc channel and writes to the WebSocket. If the WebSocket write fails, the client is automatically removed from the registry.
Sourcepub fn remove_client(&self, client_id: Uuid)
pub fn remove_client(&self, client_id: Uuid)
Remove a client from the registry.
Sourcepub fn client_count(&self) -> usize
pub fn client_count(&self) -> usize
Get the current number of connected clients.
This is lock-free and returns an approximate count (may be slightly stale under high concurrency, which is fine for max_clients checks).
Sourcepub fn send_to_client(
&self,
client_id: Uuid,
data: Arc<Bytes>,
) -> Result<(), SendError>
pub fn send_to_client( &self, client_id: Uuid, data: Arc<Bytes>, ) -> Result<(), SendError>
Send data to a specific client (non-blocking).
This method NEVER blocks. If the client’s queue is full, the client is considered too slow and is disconnected to prevent cascade failures. Use this for live streaming updates.
For initial snapshots where you expect to send many messages at once,
use send_to_client_async instead which will wait for queue space.
Sourcepub async fn send_to_client_async(
&self,
client_id: Uuid,
data: Arc<Bytes>,
) -> Result<(), SendError>
pub async fn send_to_client_async( &self, client_id: Uuid, data: Arc<Bytes>, ) -> Result<(), SendError>
Send data to a specific client (async, waits for queue space).
This method will wait if the client’s queue is full, allowing the client time to catch up. Use this for initial snapshots where you need to send many messages at once.
For live streaming updates, use send_to_client instead which will
disconnect slow clients rather than blocking.
Sourcepub fn update_subscription(
&self,
client_id: Uuid,
subscription: Subscription,
) -> bool
pub fn update_subscription( &self, client_id: Uuid, subscription: Subscription, ) -> bool
Update the subscription for a client.
Sourcepub fn update_client_last_seen(&self, client_id: Uuid)
pub fn update_client_last_seen(&self, client_id: Uuid)
Update the last_seen timestamp for a client.
Sourcepub fn get_subscription(&self, client_id: Uuid) -> Option<Subscription>
pub fn get_subscription(&self, client_id: Uuid) -> Option<Subscription>
Get the subscription for a client.
Sourcepub fn has_client(&self, client_id: Uuid) -> bool
pub fn has_client(&self, client_id: Uuid) -> bool
Check if a client exists.
pub async fn add_client_subscription( &self, client_id: Uuid, sub_key: String, token: CancellationToken, )
pub async fn remove_client_subscription( &self, client_id: Uuid, sub_key: &str, ) -> bool
pub async fn cancel_all_client_subscriptions(&self, client_id: Uuid)
Sourcepub fn cleanup_stale_clients(&self) -> usize
pub fn cleanup_stale_clients(&self) -> usize
Remove stale clients that haven’t been seen within the timeout period.
Sourcepub fn start_cleanup_task(&self)
pub fn start_cleanup_task(&self)
Start a background task that periodically cleans up stale clients.
Trait Implementations§
Source§impl Clone for ClientManager
impl Clone for ClientManager
Source§fn clone(&self) -> ClientManager
fn clone(&self) -> ClientManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more