|
| 1 | +# bsa_sessions |
| 2 | +Sessions represent live or recently-live account presence on servers.\ |
| 3 | +An account may have multiple session rows at once. |
| 4 | + |
| 5 | +!!! warning |
| 6 | + This table may incur high reads to the database due to online-state lookups.\ |
| 7 | + Please ensure your platform isn't requesting information so often. |
| 8 | + |
| 9 | +## Structure |
| 10 | +```sql |
| 11 | +CREATE TABLE `bsa_sessions` ( |
| 12 | + `session_id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, |
| 13 | + `account_id` BIGINT UNSIGNED NOT NULL, |
| 14 | + `server_id` BIGINT UNSIGNED NULL, |
| 15 | + `heartbeat_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), |
| 16 | + `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), |
| 17 | + |
| 18 | + CONSTRAINT fk_conn_server |
| 19 | + FOREIGN KEY (`server_id`) REFERENCES `bsa_servers`(`server_id`) |
| 20 | + ON DELETE SET NULL |
| 21 | + ON UPDATE CASCADE, |
| 22 | + |
| 23 | + CONSTRAINT fk_conn_account |
| 24 | + FOREIGN KEY (`account_id`) REFERENCES `bsa_accounts`(`account_id`) |
| 25 | + ON DELETE CASCADE |
| 26 | + ON UPDATE CASCADE |
| 27 | +); |
| 28 | +``` |
| 29 | + |
| 30 | +- session_id - unique id of the session. |
| 31 | +- account_id - account that owns this session. |
| 32 | +- server_id - server currently attached to the session; null means detached or orphaned from a server record. |
| 33 | +- heartbeat_at - timestamp for the last session heartbeat. |
| 34 | +- created_at - timestamp for when the session row was created. |
| 35 | + |
| 36 | +## Restrictions |
| 37 | +- There are no trigger-protected rows in this table. |
| 38 | +- Multiple sessions per account are allowed by design. |
| 39 | +- Deleting an account cascades and deletes its sessions. |
| 40 | +- Deleting a server does not delete sessions; it sets `server_id` to `NULL`. |
| 41 | + |
| 42 | +## Design Requirements |
| 43 | +- Treat a session as alive only when `heartbeat_at` is within 60 seconds, and the linked server heartbeat in `bsa_servers` is also within 60 seconds. |
| 44 | +- Create a new row when a player/account attaches to a server and no reusable `session_id` is already held locally. |
| 45 | +- Refresh `heartbeat_at` regularly for active sessions, the current platform also refreshes `server_id` on heartbeat. |
| 46 | +- Delete the row on clean disconnect/shutdown rather than keeping indefinite session history. |
| 47 | +- Always propagate `session_id` with replicated account presence so remote caches can update the correct live entry. |
| 48 | + |
| 49 | +## Interlink Behavior |
| 50 | +- `database.players:connected(account, secondaries)` payloads must include `session_id`. |
| 51 | +- `database.players:disconnected(account, secondaries)` payloads must include `session_id`. |
| 52 | +- On interlink reconnect/startup, platforms should clear any stale sessions owned by their current `server_id` and broadcast matching disconnect events. |
0 commit comments