pub struct OrderBookManager { /* private fields */ }Expand description
Thread-safe order book manager This manages multiple order books (one per token) and handles concurrent access Multiple threads can read/write different books simultaneously
The depth limiting becomes even more critical here because we might be tracking hundreds or thousands of different tokens simultaneously. If each book had unlimited depth, we could easily use gigabytes of RAM for mostly useless data.
Example: 1000 tokens × 1000 price levels × 32 bytes per level = 32MB just for prices With depth limiting: 1000 tokens × 50 levels × 32 bytes = 1.6MB (20x less memory)
Implementations§
Source§impl OrderBookManager
impl OrderBookManager
Sourcepub fn new(max_depth: usize) -> Self
pub fn new(max_depth: usize) -> Self
Create a new order book manager Starts with an empty collection of books
Sourcepub fn get_or_create_book(&self, token_id: &str) -> Result<OrderBook>
pub fn get_or_create_book(&self, token_id: &str) -> Result<OrderBook>
Get or create an order book for a token If we don’t have a book for this token yet, create a new empty one
Sourcepub fn apply_delta(&self, delta: OrderDelta) -> Result<()>
pub fn apply_delta(&self, delta: OrderDelta) -> Result<()>
Update a book with a delta This is called when we receive real-time updates from the exchange
Sourcepub fn get_book(&self, token_id: &str) -> Result<OrderBook>
pub fn get_book(&self, token_id: &str) -> Result<OrderBook>
Get a book snapshot Returns a copy of the current book state that won’t change
Sourcepub fn get_all_books(&self) -> Result<Vec<OrderBook>>
pub fn get_all_books(&self) -> Result<Vec<OrderBook>>
Get all available books Returns snapshots of every book we’re currently tracking
Sourcepub fn cleanup_stale_books(&self, max_age: Duration) -> Result<usize>
pub fn cleanup_stale_books(&self, max_age: Duration) -> Result<usize>
Remove stale books Cleans up books that haven’t been updated recently (probably disconnected) This prevents memory leaks from accumulating dead books
Trait Implementations§
Auto Trait Implementations§
impl Freeze for OrderBookManager
impl RefUnwindSafe for OrderBookManager
impl Send for OrderBookManager
impl Sync for OrderBookManager
impl Unpin for OrderBookManager
impl UnwindSafe for OrderBookManager
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more