pub struct DepthCacheManager { /* private fields */ }Expand description
Manages a local order book cache with WebSocket updates.
This manager follows Binance’s recommended approach for maintaining a local order book:
- Open a WebSocket connection for depth updates
- Buffer incoming events
- Fetch a REST API snapshot
- Apply buffered events that are newer than the snapshot
- Continue applying real-time updates
§Example
ⓘ
use binance_api_client::Binance;
use binance_api_client::websocket::{DepthCacheManager, DepthCacheConfig};
let client = Binance::new_unauthenticated()?;
let config = DepthCacheConfig::default();
let mut manager = DepthCacheManager::new(client, "BTCUSDT", config).await?;
// Wait for initial sync
manager.wait_for_sync().await?;
// Get current order book
let cache = manager.get_cache().await;
println!("Best bid: {:?}", cache.best_bid());
println!("Best ask: {:?}", cache.best_ask());
// Receive updates
while let Some(cache) = manager.next().await {
println!("Mid price: {:?}", cache.mid_price());
}Implementations§
Source§impl DepthCacheManager
impl DepthCacheManager
Sourcepub async fn new(
client: Binance,
symbol: &str,
config: DepthCacheConfig,
) -> Result<Self>
pub async fn new( client: Binance, symbol: &str, config: DepthCacheConfig, ) -> Result<Self>
Create a new depth cache manager.
This will start the WebSocket connection and begin syncing the order book.
Sourcepub async fn wait_for_sync(&self) -> Result<()>
pub async fn wait_for_sync(&self) -> Result<()>
Wait for the cache to be synchronized.
Sourcepub async fn get_cache(&self) -> DepthCache
pub async fn get_cache(&self) -> DepthCache
Get the current depth cache.
Sourcepub async fn state(&self) -> DepthCacheState
pub async fn state(&self) -> DepthCacheState
Get the current state of the manager.
Sourcepub async fn next(&mut self) -> Option<DepthCache>
pub async fn next(&mut self) -> Option<DepthCache>
Receive the next cache update.
Auto Trait Implementations§
impl Freeze for DepthCacheManager
impl !RefUnwindSafe for DepthCacheManager
impl Send for DepthCacheManager
impl Sync for DepthCacheManager
impl Unpin for DepthCacheManager
impl !UnwindSafe for DepthCacheManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more