pub struct WsStats { /* private fields */ }Expand description
WebSocket connection statistics (lock-free).
This struct uses atomic types for all fields to enable lock-free concurrent access. This prevents potential deadlocks when stats are accessed across await points.
§Thread Safety
All operations on WsStats are thread-safe and lock-free. Multiple tasks can
read and update statistics concurrently without blocking.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
// Record a received message
stats.record_received(1024);
// Get a snapshot of current stats
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_received, 1);
assert_eq!(snapshot.bytes_received, 1024);Implementations§
Source§impl WsStats
impl WsStats
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new WsStats instance with all counters initialized to zero.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_received, 0);Sourcepub fn record_received(&self, bytes: u64)
pub fn record_received(&self, bytes: u64)
Records a received message.
Increments the message count, adds to bytes received, and updates the last message timestamp.
§Arguments
bytes- Number of bytes received in the message
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_received(512);
stats.record_received(256);
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_received, 2);
assert_eq!(snapshot.bytes_received, 768);Sourcepub fn record_sent(&self, bytes: u64)
pub fn record_sent(&self, bytes: u64)
Records a sent message.
Increments the message count and adds to bytes sent.
§Arguments
bytes- Number of bytes sent in the message
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_sent(128);
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_sent, 1);
assert_eq!(snapshot.bytes_sent, 128);Sourcepub fn record_ping(&self)
pub fn record_ping(&self)
Records a ping sent.
Updates the last ping timestamp.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_ping();
let snapshot = stats.snapshot();
assert!(snapshot.last_ping_time > 0);Sourcepub fn record_pong(&self)
pub fn record_pong(&self)
Records a pong received.
Updates the last pong timestamp.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_pong();
let snapshot = stats.snapshot();
assert!(snapshot.last_pong_time > 0);Sourcepub fn record_connected(&self)
pub fn record_connected(&self)
Records a connection established.
Updates the connected_at timestamp.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_connected();
let snapshot = stats.snapshot();
assert!(snapshot.connected_at > 0);Sourcepub fn increment_reconnect_attempts(&self) -> u32
pub fn increment_reconnect_attempts(&self) -> u32
Increments the reconnection attempt counter.
§Returns
The new reconnection attempt count after incrementing.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
let count = stats.increment_reconnect_attempts();
assert_eq!(count, 1);
let count = stats.increment_reconnect_attempts();
assert_eq!(count, 2);Sourcepub fn reset_reconnect_attempts(&self)
pub fn reset_reconnect_attempts(&self)
Resets the reconnection attempt counter to zero.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.increment_reconnect_attempts();
stats.increment_reconnect_attempts();
stats.reset_reconnect_attempts();
let snapshot = stats.snapshot();
assert_eq!(snapshot.reconnect_attempts, 0);Sourcepub fn last_pong_time(&self) -> i64
pub fn last_pong_time(&self) -> i64
Returns the last pong timestamp.
This is useful for checking connection health without creating a full snapshot.
§Returns
The last pong timestamp in milliseconds, or 0 if no pong has been received.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
assert_eq!(stats.last_pong_time(), 0);
stats.record_pong();
assert!(stats.last_pong_time() > 0);Sourcepub fn last_ping_time(&self) -> i64
pub fn last_ping_time(&self) -> i64
Returns the last ping timestamp.
This is useful for calculating latency without creating a full snapshot.
§Returns
The last ping timestamp in milliseconds, or 0 if no ping has been sent.
Sourcepub fn snapshot(&self) -> WsStatsSnapshot
pub fn snapshot(&self) -> WsStatsSnapshot
Creates an immutable snapshot of current statistics.
The snapshot captures all statistics at a point in time. Note that since each field is read independently, the snapshot may not represent a perfectly consistent state if updates are happening concurrently. However, this is acceptable for statistics purposes.
§Returns
A WsStatsSnapshot containing the current values of all statistics.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_received(100);
stats.record_sent(50);
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_received, 1);
assert_eq!(snapshot.bytes_received, 100);
assert_eq!(snapshot.messages_sent, 1);
assert_eq!(snapshot.bytes_sent, 50);Sourcepub fn reset(&self)
pub fn reset(&self)
Resets all statistics to their default values.
§Example
use ccxt_core::ws_client::WsStats;
let stats = WsStats::new();
stats.record_received(100);
stats.record_sent(50);
stats.reset();
let snapshot = stats.snapshot();
assert_eq!(snapshot.messages_received, 0);
assert_eq!(snapshot.bytes_received, 0);