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: u64Connection id, unique inside a Supervisor.
desired: SubscriptionSetThe subscriptions the owner wants on this connection.
actual: HashMap<SubscriptionId, SubState>What is known about each subscription on the wire.
health: ConnectionHealthReceive-side health of the connection.
Implementations§
Source§impl ConnectionState
impl ConnectionState
Sourcepub fn new(id: u64, now: Instant) -> Self
pub fn new(id: u64, now: Instant) -> Self
Creates a connection with nothing desired and nothing active, last heard from at now.
Sourcepub fn set_desired(&mut self, desired: SubscriptionSet)
pub fn set_desired(&mut self, desired: SubscriptionSet)
Replaces the whole desired set. The actual map is left as it is.
Sourcepub fn mark_pending(&mut self, sub: &DesiredSubscription, now: Instant)
pub fn mark_pending(&mut self, sub: &DesiredSubscription, now: Instant)
Records that the subscribe request for sub was sent at now.
Sourcepub fn mark_active(&mut self, sub_id: SubscriptionId, seq: u64)
pub fn mark_active(&mut self, sub_id: SubscriptionId, seq: u64)
Records that the broker confirmed sub_id, with the sequence number seq.
Sourcepub fn mark_failed(&mut self, sub_id: SubscriptionId, reason: impl Into<String>)
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.
Sourcepub fn remove(&mut self, sub_id: SubscriptionId)
pub fn remove(&mut self, sub_id: SubscriptionId)
Forgets the actual state of sub_id. It does not touch the desired set.
Sourcepub fn classify(
&self,
now: Instant,
dead_after: Duration,
degraded_after: Duration,
) -> HealthClass
pub fn classify( &self, now: Instant, dead_after: Duration, degraded_after: Duration, ) -> HealthClass
Classifies the connection health, see ConnectionHealth::classify.
Sourcepub fn needs_resubscribe(&self) -> Vec<DesiredSubscription>
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.
Sourcepub fn needs_unsubscribe(
&self,
previous: &SubscriptionSet,
) -> Vec<DesiredSubscription>
pub fn needs_unsubscribe( &self, previous: &SubscriptionSet, ) -> Vec<DesiredSubscription>
Subscriptions that were in previous but are no longer desired, to be cancelled.