Skip to main content

Supervisor

Struct Supervisor 

Source
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: Duration

How often the owner should call reconcile. Default 1 second. The supervisor does not read it itself.

§dead_after: Duration

Silence after which a connection is HealthClass::Dead. Default 60 seconds.

§degraded_after: Duration

Silence after which a connection is HealthClass::Degraded. Default 15 seconds.

§connections: Vec<ConnectionState>

The supervised connections.

Implementations§

Source§

impl Supervisor

Source

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.

Source

pub fn connection_mut(&mut self, id: u64) -> Option<&mut ConnectionState>

The connection with this id, if there is one.

Source

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.

Source

pub fn update_desired(&mut self, conn_id: u64, desired: SubscriptionSet)

Sets the desired subscriptions of a connection. An unknown conn_id is ignored.

Source

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.

Trait Implementations§

Source§

impl Debug for Supervisor

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