pub struct Supervisor {
pub reconcile_interval: Duration,
pub dead_after: Duration,
pub degraded_after: Duration,
pub connections: Vec<ConnectionState>,
}Expand description
Connection supervisor without I/O: it compares desired and actual state and says what to do.
Call reconcile on a timer (every reconcile_interval).
It returns one ReconcileAction per connection and changes nothing on the wire. The decision
is made by the connection’s silence: see HealthClass.
A new supervisor holds one connection with id 1.
§Examples
use std::time::{Duration, Instant};
use brokapi_core::{InstrumentId, InstrumentKey, NativeId, SubscriptionId};
use brokapi_runtime::{ReconcileAction, Supervisor};
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 t0 = Instant::now();
let mut sup = Supervisor::new(t0);
sup.update_desired(1, SubscriptionSet::with_subscriptions(vec![sub.clone()]));
// Fresh connection: apply the desired set. One subscription is missing on the wire.
assert_eq!(sup.reconcile(t0), vec![(1, ReconcileAction::ApplyDesired)]);
assert_eq!(sup.resubscribe_targets(1), vec![sub]);
// Silent for longer than `dead_after` (60 s by default): replace the connection.
let later = t0 + Duration::from_secs(61);
assert_eq!(sup.reconcile(later), vec![(1, ReconcileAction::ScheduleReconnect)]);Fields§
§reconcile_interval: DurationHow often the owner should call reconcile. Default 1 second. The
supervisor does not read it itself.
dead_after: DurationSilence after which a connection is HealthClass::Dead. Default 60 seconds.
degraded_after: DurationSilence after which a connection is HealthClass::Degraded. Default 15 seconds.
connections: Vec<ConnectionState>The supervised connections.
Implementations§
Source§impl Supervisor
impl Supervisor
Sourcepub fn new(now: Instant) -> Self
pub fn new(now: Instant) -> Self
Creates a supervisor with one connection (id 1) last heard from at now, and the default
timings listed on the fields.
Sourcepub fn connection_mut(&mut self, id: u64) -> Option<&mut ConnectionState>
pub fn connection_mut(&mut self, id: u64) -> Option<&mut ConnectionState>
The connection with this id, if there is one.
Sourcepub fn reconcile(&mut self, now: Instant) -> Vec<(u64, ReconcileAction)>
pub fn reconcile(&mut self, now: Instant) -> Vec<(u64, ReconcileAction)>
Decides what each connection needs at now. Pure: the connections are not changed.
The silence of the connection picks the action: healthy gives
ReconcileAction::ApplyDesired, degraded gives ReconcileAction::ResubscribeMissing, dead
gives ReconcileAction::ScheduleReconnect. One entry per connection, in connection order.
Sourcepub fn update_desired(&mut self, conn_id: u64, desired: SubscriptionSet)
pub fn update_desired(&mut self, conn_id: u64, desired: SubscriptionSet)
Sets the desired subscriptions of a connection. An unknown conn_id is ignored.
Sourcepub fn resubscribe_targets(&self, conn_id: u64) -> Vec<DesiredSubscription>
pub fn resubscribe_targets(&self, conn_id: u64) -> Vec<DesiredSubscription>
The subscriptions of a connection that must be (re)sent, see
ConnectionState::needs_resubscribe. Empty for an unknown conn_id.