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::Acquirefor reads to ensure visibility of prior writesOrdering::Releasefor 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
impl TimeSyncManager
Sourcepub fn new() -> Self
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());Sourcepub fn with_config(config: TimeSyncConfig) -> Self
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);Sourcepub fn is_initialized(&self) -> bool
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());Sourcepub fn needs_resync(&self) -> bool
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 syncedSourcepub fn get_offset(&self) -> i64
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)Sourcepub fn get_server_timestamp(&self) -> i64
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);Sourcepub fn update_offset(&self, server_time: i64)
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());Sourcepub fn last_sync_time(&self) -> i64
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);Sourcepub fn config(&self) -> &TimeSyncConfig
pub fn config(&self) -> &TimeSyncConfig
Sourcepub fn reset(&self)
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());