Skip to main content

InstrumentLocks

Struct InstrumentLocks 

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

Per-instrument command serialization: one trading command at a time on each instrument.

The lock is keyed by [InstrumentKey] (symbol and mic), not by the whole [InstrumentId]: the same instrument reached through a ticker, a uid or a figi is still one instrument. Waiters are served in the order they asked (the lock is FIFO), so a cancel sent before a place reaches the broker before it. Why: the broker’s command channel is not ordered, and a place that overtakes a cancel would act on a different book than the caller intended.

Different instruments do not wait for each other. The lock is released when the guard is dropped, on every exit path, including errors and cancelled futures.

bind and instrument_of remember which instrument a cid belongs to, for commands that carry only a cid (cancel, replace).

§Examples

use brokapi_core::{ClientOrderId, InstrumentId, InstrumentKey, NativeId};
use brokapi_runtime::InstrumentLocks;

let sber = InstrumentId::new(
    InstrumentKey::new("SBER", "TQBR"),
    NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
let locks = InstrumentLocks::new();
let cid = ClientOrderId::new_v4();
locks.bind(cid, &sber);

// A place holds the instrument while its command is on the wire.
let guard = locks.lock(&sber).await;
assert!(locks.is_busy(&sber));
drop(guard);
assert!(!locks.is_busy(&sber));

// A cancel carries only the cid. It finds the instrument and takes the same lock.
let guard = locks.lock_cid(cid).await;
assert!(guard.is_some() && locks.is_busy(&sber));

Implementations§

Source§

impl InstrumentLocks

Source

pub fn new() -> Self

Creates an empty set of locks.

Source

pub async fn lock(&self, instrument: &InstrumentId) -> InstrumentGuard

Waits for earlier commands on instrument, then holds it until the guard drops.

Source

pub fn is_busy(&self, instrument: &InstrumentId) -> bool

Whether a command on the instrument is on the wire right now. For tests and diagnostics: the answer can be stale by the time the caller acts on it.

Source

pub fn bind(&self, cid: ClientOrderId, instrument: &InstrumentId)

Remembers the instrument of an order we placed.

Source

pub fn forget(&self, cid: ClientOrderId)

Forgets the instrument of a cid, for an order that is long finished. A later cancel of that cid is no longer serialized with the other commands of its instrument.

Source

pub fn bound(&self) -> usize

How many cids are bound to an instrument. For tests and metrics.

Source

pub fn instrument_of(&self, cid: ClientOrderId) -> Option<InstrumentId>

The instrument of a cid, if this process placed (or replaced) it.

Source

pub async fn lock_cid(&self, cid: ClientOrderId) -> Option<InstrumentGuard>

Locks the instrument of cid when it is known. None: an order from before this process started, its commands are not serialized with the others.

Trait Implementations§

Source§

impl Debug for InstrumentLocks

Source§

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

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

impl Default for InstrumentLocks

Source§

fn default() -> InstrumentLocks

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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, 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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more