TimeSyncManager

Struct TimeSyncManager 

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

Thread-safe time synchronization manager.

Maintains a cached time offset between local system time and Binance server time. Uses atomic operations for thread-safe access without locks.

§Thread Safety

All operations use atomic memory ordering:

  • Ordering::Acquire for reads to ensure visibility of prior writes
  • Ordering::Release for writes to ensure visibility to subsequent reads

§Example

use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();

// Check if sync is needed (always true initially)
assert!(manager.needs_resync());

// Simulate server time update
let server_time = 1704110400000i64;
manager.update_offset(server_time);

// Now initialized
assert!(manager.is_initialized());

// Get estimated server timestamp
let timestamp = manager.get_server_timestamp();
assert!(timestamp > 0);

Implementations§

Source§

impl TimeSyncManager

Source

pub fn new() -> Self

Creates a new TimeSyncManager with default configuration.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
assert!(!manager.is_initialized());
Source

pub fn with_config(config: TimeSyncConfig) -> Self

Creates a new TimeSyncManager with custom configuration.

§Arguments
  • config - Time sync configuration
§Example
use ccxt_exchanges::binance::time_sync::{TimeSyncConfig, TimeSyncManager};
use std::time::Duration;

let config = TimeSyncConfig {
    sync_interval: Duration::from_secs(60),
    auto_sync: true,
    max_offset_drift: 3000,
};
let manager = TimeSyncManager::with_config(config);
Source

pub fn is_initialized(&self) -> bool

Returns whether initial sync has been performed.

§Returns

true if update_offset() has been called at least once successfully.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
assert!(!manager.is_initialized());

manager.update_offset(1704110400000);
assert!(manager.is_initialized());
Source

pub fn needs_resync(&self) -> bool

Returns whether a resync is needed based on sync interval.

Returns true if:

  • The manager is not initialized, OR
  • Auto sync is enabled AND the time since last sync exceeds the sync interval
§Returns

true if resync is needed, false otherwise.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();

// Always needs resync when not initialized
assert!(manager.needs_resync());

// After initialization, depends on sync interval
manager.update_offset(1704110400000);
assert!(!manager.needs_resync()); // Just synced
Source

pub fn get_offset(&self) -> i64

Gets the current cached time offset.

The offset represents: server_time - local_time in milliseconds.

§Returns

The cached time offset in milliseconds.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
assert_eq!(manager.get_offset(), 0); // Default offset

// After sync, offset reflects the difference
// (actual value depends on local vs server time)
Source

pub fn get_server_timestamp(&self) -> i64

Calculates the estimated server timestamp using cached offset.

Formula: server_timestamp = local_time + offset

Uses saturating arithmetic to prevent overflow.

§Returns

Estimated server timestamp in milliseconds.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
let timestamp = manager.get_server_timestamp();
assert!(timestamp > 0);
Source

pub fn update_offset(&self, server_time: i64)

Updates the time offset based on server time.

This method should be called after fetching server time from the API. It calculates the offset as: offset = server_time - local_time

§Arguments
  • server_time - The server timestamp in milliseconds
§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();

// Simulate receiving server time
let server_time = 1704110400000i64;
manager.update_offset(server_time);

assert!(manager.is_initialized());
Source

pub fn last_sync_time(&self) -> i64

Returns the last sync timestamp (local time).

§Returns

The local timestamp when the last sync occurred, in milliseconds. Returns 0 if never synced.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
assert_eq!(manager.last_sync_time(), 0);

manager.update_offset(1704110400000);
assert!(manager.last_sync_time() > 0);
Source

pub fn config(&self) -> &TimeSyncConfig

Returns a reference to the sync configuration.

§Returns

Reference to the TimeSyncConfig.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;
use std::time::Duration;

let manager = TimeSyncManager::new();
assert_eq!(manager.config().sync_interval, Duration::from_secs(30));
Source

pub fn reset(&self)

Resets the manager to uninitialized state.

This clears the cached offset and marks the manager as needing resync. Useful for testing or when a forced resync is needed.

§Example
use ccxt_exchanges::binance::time_sync::TimeSyncManager;

let manager = TimeSyncManager::new();
manager.update_offset(1704110400000);
assert!(manager.is_initialized());

manager.reset();
assert!(!manager.is_initialized());

Trait Implementations§

Source§

impl Debug for TimeSyncManager

Source§

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

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

impl Default for TimeSyncManager

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