Skip to main content

BackpressureManager

Struct BackpressureManager 

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

The buffers of all active subscriptions of one connector, plus the connector’s degraded flag.

Used from the parser task: ingest puts an event into the buffer of its subscription without blocking, and pop_ready takes the next one out for delivery. When any Lossless buffer overflows, the manager marks the whole connector degraded. A degraded connector refuses trading commands with [brokapi_core::Error::Degraded] (the caller applies that, for example through CommandGate) until a successful reconcile calls clear_degraded.

Most connectors use Emitter, which owns a manager and also moves events into the output channel.

§Examples

use brokapi_core::{Backpressure, Event, InstrumentId, InstrumentKey, NativeId, SubscriptionId, UnixNanos};
use brokapi_runtime::{BackpressureManager, PushResult};

let sber = InstrumentId::new(
    InstrumentKey::new("SBER", "TQBR"),
    NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
let sub = SubscriptionId::new(1);
let mut mgr = BackpressureManager::new();
mgr.register(sub, sber, Backpressure::Lossless { cap: 1 });

assert_eq!(mgr.ingest(sub, Event::Balances(vec![]), UnixNanos::ZERO), PushResult::Delivered);
assert_eq!(mgr.ingest(sub, Event::Balances(vec![]), UnixNanos::ZERO), PushResult::Overflow);
assert!(mgr.is_degraded());

// The reconcile that follows an overflow clears the flag.
mgr.clear_degraded();
assert!(!mgr.is_degraded());

Implementations§

Source§

impl BackpressureManager

Source

pub fn new() -> Self

Creates a manager with no subscriptions, not degraded.

Source

pub fn register( &mut self, sub: SubscriptionId, instrument: InstrumentId, policy: Backpressure, )

Adds a buffer for a subscription.

Registering the same sub twice creates two buffers. Only the first one is ever used, so register each subscription id once.

Source

pub fn unregister(&mut self, sub: SubscriptionId)

Removes the buffer of a subscription. Events still waiting in it are discarded. An unknown sub is ignored.

Source

pub fn is_degraded(&self) -> bool

Whether some Lossless buffer overflowed since the last clear_degraded.

Source

pub fn clear_degraded(&mut self)

Leaves the degraded state and reopens every overflowed buffer.

Call it after a successful reconcile, when the consumer has re-read the state it lost. Events that were refused during the overflow are not recovered.

Source

pub fn ingest( &mut self, sub: SubscriptionId, event: Event, lost_from: UnixNanos, ) -> PushResult

Offers an event to the buffer of sub without blocking.

An PushResult::Overflow also sets the degraded flag of the manager. The lost_from argument is accepted for the caller’s convenience and is not used by the manager: the caller builds the [Event::Overflow] notice itself (see SubscriptionBuffer::overflow_event).

An event for a sub that is not registered is discarded and the result is still PushResult::Delivered. Register the subscription before its first event can arrive.

Source

pub fn pop_ready(&mut self, sub: SubscriptionId) -> Option<Event>

Takes the next event of sub that is ready for delivery, if any.

A Conflate subscription yields its latest value of each kind, one per call. Others yield their queue front, oldest first. Returns None for an empty buffer and for an unknown sub.

§Examples
use brokapi_core::{Backpressure, Event, InstrumentId, InstrumentKey, NativeId, SubscriptionId, UnixNanos};
use brokapi_runtime::BackpressureManager;

let sber = InstrumentId::new(
    InstrumentKey::new("SBER", "TQBR"),
    NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
let sub = SubscriptionId::new(1);
let mut mgr = BackpressureManager::new();
mgr.register(sub, sber, Backpressure::Conflate);

// Three snapshots arrive before the consumer looks. Only the last one is kept.
for _ in 0..3 {
    mgr.ingest(sub, Event::Balances(vec![]), UnixNanos::ZERO);
}
assert!(mgr.pop_ready(sub).is_some());
assert!(mgr.pop_ready(sub).is_none());

Trait Implementations§

Source§

impl Debug for BackpressureManager

Source§

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

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

impl Default for BackpressureManager

Source§

fn default() -> BackpressureManager

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