pub struct CommandGate { /* private fields */ }Expand description
Decides whether a trading command may go out now.
A connector asks the gate before it sends a place, cancel or replace. The gate refuses in three cases, checked in this order: the connector is shutting down, it is degraded, it has not been reconciled yet. The gate starts closed (not reconciled), so the first reconcile with the broker opens it. Why: before the client knows which orders already exist at the broker, a new command could duplicate or fight an order it cannot see.
The gate only holds flags. The connector sets them: mark_reconciled
after a successful reconcile, mark_degraded when the
Emitter reports an overflow, begin_shutdown when
closing. Reads (order book, portfolio, active orders) are not gated.
§Examples
use brokapi_core::Error;
use brokapi_runtime::CommandGate;
let mut gate = CommandGate::new();
// A new gate refuses commands until the first reconcile.
assert!(matches!(gate.check_command(), Err(Error::NotReconciled)));
gate.mark_reconciled();
assert!(gate.check_command().is_ok());
// An overflow of a lossless stream closes it again until the next reconcile.
gate.mark_degraded();
assert!(matches!(gate.check_command(), Err(Error::Degraded)));
gate.clear_degraded();
assert!(gate.check_command().is_ok());
// Shutdown wins over everything else and is final.
gate.begin_shutdown();
assert!(matches!(gate.check_command(), Err(Error::ShuttingDown)));Implementations§
Source§impl CommandGate
impl CommandGate
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a closed gate. Same as Default::default.
Sourcepub fn is_reconciled(&self) -> bool
pub fn is_reconciled(&self) -> bool
Whether mark_reconciled was called.
Sourcepub fn is_degraded(&self) -> bool
pub fn is_degraded(&self) -> bool
Whether the gate is in the degraded state.
Sourcepub fn mark_reconciled(&mut self)
pub fn mark_reconciled(&mut self)
Records a successful reconcile. There is no way to undo it.
Sourcepub fn mark_degraded(&mut self)
pub fn mark_degraded(&mut self)
Records that events were lost (see Emitter::is_degraded).
Commands are refused until clear_degraded.
Sourcepub fn clear_degraded(&mut self)
pub fn clear_degraded(&mut self)
Leaves the degraded state. Call it after the reconcile that made up for the lost events.
Sourcepub fn begin_shutdown(&mut self)
pub fn begin_shutdown(&mut self)
Closes the gate for good: every later check fails with [Error::ShuttingDown].
Sourcepub fn check_command(&self) -> Result<(), Error>
pub fn check_command(&self) -> Result<(), Error>
Says whether a trading command may be sent now.
§Errors
In this order of priority:
- [
Error::ShuttingDown] afterbegin_shutdown. - [
Error::Degraded] while the gate is degraded. - [
Error::NotReconciled] untilmark_reconciled.
All three mean nothing was sent, so the command may be repeated once the cause is gone (except after a shutdown).
Trait Implementations§
Source§impl Clone for CommandGate
impl Clone for CommandGate
Source§fn clone(&self) -> CommandGate
fn clone(&self) -> CommandGate
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for CommandGate
Source§impl Debug for CommandGate
impl Debug for CommandGate
Source§impl Default for CommandGate
impl Default for CommandGate
impl Eq for CommandGate
Source§impl PartialEq for CommandGate
impl PartialEq for CommandGate
Source§fn eq(&self, other: &CommandGate) -> bool
fn eq(&self, other: &CommandGate) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for CommandGate
Auto Trait Implementations§
impl Freeze for CommandGate
impl RefUnwindSafe for CommandGate
impl Send for CommandGate
impl Sync for CommandGate
impl Unpin for CommandGate
impl UnsafeUnpin for CommandGate
impl UnwindSafe for CommandGate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.