Skip to main content

ConnectionState

Struct ConnectionState 

Source
pub struct ConnectionState {
    pub id: u64,
    pub desired: SubscriptionSet,
    pub actual: HashMap<SubscriptionId, SubState>,
    pub health: ConnectionHealth,
}
Expand description

The desired and actual subscriptions of one connection, and its health.

The owner sets what it wants (set_desired) and records what happens on the wire (mark_pending, mark_active, mark_failed). The difference between the two is what needs_resubscribe and needs_unsubscribe report. The struct does no I/O.

§Examples

use std::time::Instant;
use brokapi_core::{InstrumentId, InstrumentKey, NativeId, SubscriptionId};
use brokapi_runtime::ConnectionState;
use brokapi_sansio::{DesiredSubscription, SubscriptionKind, SubscriptionSet};

let sber = InstrumentId::new(
    InstrumentKey::new("SBER", "TQBR"),
    NativeId::TickerClass { ticker: "SBER".into(), class_code: "TQBR".into() },
);
let sub = DesiredSubscription {
    id: SubscriptionId::new(1),
    kind: SubscriptionKind::Quotes { instrument: sber },
};

let mut conn = ConnectionState::new(1, Instant::now());
conn.set_desired(SubscriptionSet::with_subscriptions(vec![sub.clone()]));
// Wanted but not yet on the wire.
assert_eq!(conn.needs_resubscribe().len(), 1);

conn.mark_active(sub.id, 1);
assert!(conn.needs_resubscribe().is_empty());

// The broker drops it: it must be sent again.
conn.mark_failed(sub.id, "denied");
assert_eq!(conn.needs_resubscribe().len(), 1);

Fields§

§id: u64

Connection id, unique inside a Supervisor.

§desired: SubscriptionSet

The subscriptions the owner wants on this connection.

§actual: HashMap<SubscriptionId, SubState>

What is known about each subscription on the wire.

§health: ConnectionHealth

Receive-side health of the connection.

Implementations§

Source§

impl ConnectionState

Source

pub fn new(id: u64, now: Instant) -> Self

Creates a connection with nothing desired and nothing active, last heard from at now.

Source

pub fn set_desired(&mut self, desired: SubscriptionSet)

Replaces the whole desired set. The actual map is left as it is.

Source

pub fn mark_pending(&mut self, sub: &DesiredSubscription, now: Instant)

Records that the subscribe request for sub was sent at now.

Source

pub fn mark_active(&mut self, sub_id: SubscriptionId, seq: u64)

Records that the broker confirmed sub_id, with the sequence number seq.

Source

pub fn mark_failed(&mut self, sub_id: SubscriptionId, reason: impl Into<String>)

Records that sub_id was refused or broke, with a cause for logs.

Source

pub fn remove(&mut self, sub_id: SubscriptionId)

Forgets the actual state of sub_id. It does not touch the desired set.

Source

pub fn classify( &self, now: Instant, dead_after: Duration, degraded_after: Duration, ) -> HealthClass

Classifies the connection health, see ConnectionHealth::classify.

Source

pub fn needs_resubscribe(&self) -> Vec<DesiredSubscription>

Desired subscriptions that are not confirmed active, to be (re)sent without a full reconnect.

A subscription is listed when it has no state, has failed, or is still pending. Pending ones are included whatever their age, so call this when you are ready to send again, not on every tick, or you will resend requests that are merely slow.

Source

pub fn needs_unsubscribe( &self, previous: &SubscriptionSet, ) -> Vec<DesiredSubscription>

Subscriptions that were in previous but are no longer desired, to be cancelled.

Trait Implementations§

Source§

impl Debug for ConnectionState

Source§

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

Formats the value using the given formatter. 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