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.
| Need | Type |
|---|---|
| Stay under the broker’s rate limits | BrokerLimiter, BrokerLimits, BucketCfg, ConnQuota |
| Refuse trading before the first reconcile, after an overflow, during shutdown | CommandGate |
| Compare the client’s order ledger with the broker | reconcile_ledger |
| Send events to the consumer without blocking, with a loss policy per subscription | Emitter, BackpressureManager, SubscriptionBuffer, PushResult |
Hold subscribe_* until the broker has confirmed the stream | wait_ack, AckState |
| Never send the same place twice, track unanswered orders | OrderRuntime |
| Finish a command even when the caller stops waiting | run_detached |
| One command at a time per instrument, in order | InstrumentLocks |
| Refuse an order price off the price step before sending | PriceGrid |
| Decide when to resubscribe or reconnect | Supervisor, ConnectionState, ConnectionHealth |
| Stop cleanly | ShutdownController |
| Read a long candle history within the broker’s span and rate limits | history::CandlePages, history::load_candles |
| Record the event stream to a file and play it back | record::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§
- Backpressure
Manager - The buffers of all active subscriptions of one connector, plus the connector’s degraded flag.
- Broker
Limiter - Shared multi-bucket rate limiter: one instance per broker user.
- Broker
Limits - The full limit policy of one broker user: token buckets, routes and connection quotas.
- Bucket
Cfg - Token-bucket configuration for one limit bucket.
- Command
Gate - Decides whether a trading command may go out now.
- Conn
Quota - Connection and subscription quotas of one broker user.
- Connection
Health - Receive-side health signals of one connection.
- Connection
State - 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.
- Finished
Window - A memory window for things that belong to finished orders: the cid maps of a connector.
- Instrument
Locks - Per-instrument command serialization: one trading command at a time on each instrument.
- Order
Runtime - 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.
- Price
Grid - The price step of each instrument the connector was told about.
- Runtime
Metrics - A plain set of counters about the connector runtime.
- Shutdown
Controller - Shutdown coordinator for one connector instance: it closes the command gate, gives up waiting for in-flight commands and cancels the background tasks.
- Subscription
Buffer - 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. - Health
Class - How healthy a connection looks, judged by how long it has been silent.
- Push
Result - What happened to one event offered to a buffer. Returned by
SubscriptionBuffer::try_pushandBackpressureManager::ingest. - Reconcile
Action - What the owner should do for one connection after a
Supervisor::reconcilepass. - SubState
- What the connection knows about one subscription: the “actual” side of desired-versus-actual.
Constants§
- BOUND_
CIDS - How many cids
InstrumentLocksremembers. 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
OrderRuntimeremembers 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
workas a task of its own and waits for its result. If the caller’s future is dropped (aselect!branch lost, a timeout around the call, a cancelled task),workis not stopped: it runs to its end. - wait_
ack - Waits until the broker has confirmed a subscription.
Type Aliases§
- Bucket
Id - Name of a token bucket in a limits file, for example
"global"or"orders". - Instrument
Guard - Held while a command on the instrument is on the wire.
- Method
Id - Name of an operation in the
routetable, for example"PlaceOrder".