WsStats

Struct WsStats 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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.

Source

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);
Source

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);

Trait Implementations§

Source§

impl Debug for WsStats

Source§

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

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

impl Default for WsStats

Source§

fn default() -> Self

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