Skip to main content

Crate turbojet_sql

Crate turbojet_sql 

Source
Expand description

SQL session storage for Turbojet: SqlStorage keeps each FIX session’s sequence numbers and sent messages in SQLite or PostgreSQL, through sqlx.

It suits deployments that can’t rely on local disk, or that want session state next to their other data. Each session’s state lives in a row of turbojet_sessions, and its stored messages in turbojet_messages; SqlStorage::migrate creates both.

use std::sync::Arc;
use turbojet_sql::{SqlConfig, SqlStorage};

let storage = SqlStorage::connect("sqlite://sessions.db?mode=rwc", SqlConfig::default()).await?;
storage.migrate().await?;
let registry = turbojet::SessionRegistry::new(Arc::new(storage));

§How it stores

A session’s changes are kept in memory until the session commits them, once per batch of work; each commit is one transaction. Opening a session’s log, committing and reading messages for a resend all run off the connection’s task, as jobs the connection’s driver awaits (see turbojet::store::Job), so a database round trip never blocks the runtime.

Each session keeps at most SqlConfig::max_session_bytes of messages: past it, the oldest are deleted, and a resend that reaches back to them gap-fills them, as with DiskStorage.

§One gateway per session

A gateway’s registry lets one connection at a time run a session. Across gateways sharing a database, a lease does: opening a session’s log takes its lease, for SqlConfig::lease, and each commit renews it. A gateway whose lease has expired and been taken by another can’t commit, so its session fails and disconnects rather than send under numbers the other is using. A session’s commits come at least once per heartbeat interval while it’s connected (each heartbeat is stored), so the lease must be longer than the longest heartbeat interval, with a margin for a slow database. Closing the log gives the lease up; a gateway that stops without closing it leaves it to expire.

Leases compare times from each gateway’s clock, so gateways sharing a database need clocks in step to well within the lease.

§Backends

Each is a feature: sqlite (the default, bundled) and postgres, with tls for TLS to PostgreSQL. The URL given to SqlStorage::connect picks one: sqlite://path?mode=rwc or postgres://user:password@host/database.

Structs§

SqlConfig
How a SqlStorage keeps sessions.
SqlStorage
Stores sessions in a SQL database: see the crate documentation.