Skip to main content

Crate brokapi_runtime

Crate brokapi_runtime 

Source
Expand description

Connector runtime: the building blocks every broker connector shares.

This is the layer between the broker-specific protocol code and the capability traits of [brokapi_core]. It holds the parts that do not depend on a broker and do no network I/O, so they are the same for BCS, T-Bank and Finam and can be tested with plain values.

NeedType
Stay under the broker’s rate limitsBrokerLimiter, BrokerLimits, BucketCfg, ConnQuota
Refuse trading before the first reconcile, after an overflow, during shutdownCommandGate
Compare the client’s order ledger with the brokerreconcile_ledger
Send events to the consumer without blocking, with a loss policy per subscriptionEmitter, BackpressureManager, SubscriptionBuffer, PushResult
Hold subscribe_* until the broker has confirmed the streamwait_ack, AckState
Never send the same place twice, track unanswered ordersOrderRuntime
Finish a command even when the caller stops waitingrun_detached
One command at a time per instrument, in orderInstrumentLocks
Refuse an order price off the price step before sendingPriceGrid
Decide when to resubscribe or reconnectSupervisor, ConnectionState, ConnectionHealth
Stop cleanlyShutdownController
Read a long candle history within the broker’s span and rate limitshistory::CandlePages, history::load_candles
Record the event stream to a file and play it backrecord::EnvelopeWriter, record::EnvelopeReader, record::replay

Limits belong to the broker user, not to a connector: build one BrokerLimiter per user and share its Arc between all connectors of that user.

The book is at https://brokapi.ru/docs/.

§Examples

A trading command passes two checks before it is sent: the command gate and the rate limiter.

use std::time::Instant;
use brokapi_core::Error;
use brokapi_runtime::{BrokerLimiter, BrokerLimits, CommandGate};

let limiter = BrokerLimiter::new(&BrokerLimits::from_toml_str(raw)?);
let mut gate = CommandGate::new();

// Before the first reconcile no command may go out.
assert!(matches!(gate.check_command(), Err(Error::NotReconciled)));
gate.mark_reconciled();

// Now the gate is open and the limiter lets the first order through. The second one, sent
// at once, is refused locally. Nothing reached the broker.
let now = Instant::now();
assert!(gate.check_command().is_ok());
assert!(limiter.acquire("PlaceOrder", now).is_ok());
assert!(matches!(limiter.acquire("PlaceOrder", now), Err(Error::RateLimited { .. })));

Modules§

history
Reading a long candle history in windows that the broker accepts, within the rate limit.
record
Writing the event stream to a file and playing it back.

Structs§

BackpressureManager
The buffers of all active subscriptions of one connector, plus the connector’s degraded flag.
BrokerLimiter
Shared multi-bucket rate limiter: one instance per broker user.
BrokerLimits
The full limit policy of one broker user: token buckets, routes and connection quotas.
BucketCfg
Token-bucket configuration for one limit bucket.
CommandGate
Decides whether a trading command may go out now.
ConnQuota
Connection and subscription quotas of one broker user.
ConnectionHealth
Receive-side health signals of one connection.
ConnectionState
The desired and actual subscriptions of one connection, and its health.
Emitter
Moves events from a connector’s reader task into the consumer’s channel, without ever blocking.
FinishedWindow
A memory window for things that belong to finished orders: the cid maps of a connector.
InstrumentLocks
Per-instrument command serialization: one trading command at a time on each instrument.
OrderRuntime
Runtime order book: one state machine per client order id, a timeout sweep, and a guard that never lets the same place command go out twice.
PriceGrid
The price step of each instrument the connector was told about.
RuntimeMetrics
A plain set of counters about the connector runtime.
ShutdownController
Shutdown coordinator for one connector instance: it closes the command gate, gives up waiting for in-flight commands and cancels the background tasks.
SubscriptionBuffer
Egress buffer of one subscription. It applies the subscription’s [Backpressure] policy.
Supervisor
Connection supervisor without I/O: it compares desired and actual state and says what to do.

Enums§

AckState
What a connector knows about one new subscription, for wait_ack.
HealthClass
How healthy a connection looks, judged by how long it has been silent.
PushResult
What happened to one event offered to a buffer. Returned by SubscriptionBuffer::try_push and BackpressureManager::ingest.
ReconcileAction
What the owner should do for one connection after a Supervisor::reconcile pass.
SubState
What the connection knows about one subscription: the “actual” side of desired-versus-actual.

Constants§

BOUND_CIDS
How many cids InstrumentLocks remembers. The oldest are forgotten first: a cancel of such an old order is no longer serialized with the other commands of its instrument.
DEFAULT_ROUTE
Route key for “any method that has no route of its own”.
RETAINED_FINISHED
How many finished orders OrderRuntime remembers at most (100 000).
RETENTION
How long a finished order is remembered by OrderRuntime (24 hours).
SUBSCRIPTION_REFUSED
The code of the [Error::Rejected] that a refused subscription gives. The same at every broker.

Functions§

not_confirmed
The error of a subscription that was not confirmed within limit: [Error::Transport].
now
The current instant of the runtime clock.
reconcile_ledger
Compares the client’s ledger of orders with the broker’s active orders and sorts them into three groups.
run_detached
Runs work as a task of its own and waits for its result. If the caller’s future is dropped (a select! branch lost, a timeout around the call, a cancelled task), work is not stopped: it runs to its end.
wait_ack
Waits until the broker has confirmed a subscription.

Type Aliases§

BucketId
Name of a token bucket in a limits file, for example "global" or "orders".
InstrumentGuard
Held while a command on the instrument is on the wire.
MethodId
Name of an operation in the route table, for example "PlaceOrder".