mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Paged SyncState, Added Ping, Required ALPN on the server, Capped retention ages, Closed idle database connections
- Protocol 8: SyncStateRequest takes `after` (a Pair), SyncState has `more` and 32768 heads at most (MAX_HEADS_PER_PACKET), the lists and missing attachments in the first page only; heads_after in storage, both ends of replication read in pages (step 17) - Protocol 8: Ping / Pong (19, 20) keep an idle connection open (IDLE_TIMEOUT_SECS 300, KEEPALIVE_SECS 60). Client::ping, BakaShrot_Ping, Python ping(), C# Ping(), the PureBasic prototype; the tool's --follow, Python's follow(), C#'s Follow() and the replication links ping every minute while waiting longer, so sync_interval over 5 min works now (dropped from known-issues.md) - The server closes a client offering no ALPN right after TLS: rustls only refused one offering other protocols - Retention ages are 100 years at most (MAX_RETENTION_AGE); the cutoff is computed once, saturating (Retention::cutoff): a huge age wrapped around to almost nothing in milliseconds - [server] idle_database_connections (8 by default, 0-512): the idle read and query connections each pool keeps, those unused for a minute closed by a sweeping thread - Tests for each, docs, the C header, revamp.md (step 18) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+7
-4
@@ -76,6 +76,7 @@ a client's, or an enumeration's for the enumeration getters.
|
||||
| `SetCaCertificate`, `SetCertificate`, `SetPrivateKey`, `SetCrl` (`A`/`W`) | PEM text (starting with `-----BEGIN`), or the path of a file holding it; `SetCrl` null or empty for none. Before connecting (`AlreadyConnected`); a file that can't be read is `FileError` |
|
||||
| `Connect(Host, Port)` (`A`/`W`) | TLS with those; refused: `AuthRejected` (the server refused the certificate), `ServerNotVerified` (this client refused the server's), `NotAllowed` (none of its roles is the server's), `VersionMismatch` |
|
||||
| `IsConnected`, `Disconnect` | `IsConnected` notices a connection the server closed |
|
||||
| `Ping` | 1, or 0 if the connection is gone: keeps it open. The server closes one silent for 5 min; the DLL has no thread of its own, so an app idle longer calls it every minute or so |
|
||||
| `GetNode` (`A`/`W`) | the node the server says this client is |
|
||||
| `PushMessageTo(Topic, Data)` (`A`/`W`) | the id, -1 on failure |
|
||||
| `GetLatestMessageOf(Topic, Node)` (`A`/`W`) | 1, or 0 with `NotFound` when there's none yet |
|
||||
@@ -121,7 +122,8 @@ certificate, key, crl=None)` (PEM text or paths) or
|
||||
`Client.from_file("client.toml")` (Python 3.11+, `tomllib`; the rest runs
|
||||
on 3.8), `client.node` once connected, `push(data, topic,
|
||||
attachment=None)` (the topic required; the attachment bytes or a path),
|
||||
`latest()`, `get_messages()`, `iter_messages()`, `follow()`,
|
||||
`latest()`, `get_messages()`, `iter_messages()`, `follow()` (pinging
|
||||
every minute while it waits longer), `ping()` (`KEEPALIVE_SECONDS`),
|
||||
`get_attachment()` (bytes), `save_attachment()` (a file),
|
||||
`list_topics()` / `list_nodes()` (names; `topic=` and `node=`, a name or
|
||||
a pattern each, filter them: `list_topics(node="kitchen")` the topics
|
||||
@@ -165,7 +167,8 @@ using (Client client = new Client("10.0.0.2", 7000, "ca.crt", "kitchen.crt", "ki
|
||||
`new Client(host, port, ca, certificate, key, crl)` (PEM text or paths,
|
||||
`crl` null for none), `Connect()`, `Node`, `Connected`, `Push(topic,
|
||||
data[, byte[]])`, `PushFile(topic, data, path)`, `Latest()` (null if
|
||||
none), `GetMessages()` (a `MessagesPage`), `IterMessages()`, `Follow()`,
|
||||
none), `GetMessages()` (a `MessagesPage`), `IterMessages()`, `Follow()`
|
||||
(pinging every minute while it waits longer), `Ping()` (`KeepAliveMs`),
|
||||
`GetAttachment()` (bytes), `SaveAttachment()` (a file), `ListTopics()` /
|
||||
`ListNodes()` / `Query()` (the library's JSON, as a string: parsing it
|
||||
is the app's; `ListTopics(topic, node)` / `ListNodes(topic, node)`
|
||||
@@ -224,8 +227,8 @@ bakashrot list nodes --topics 'sensors.*' Nodes (2): those that pushed to s
|
||||
|
||||
- **`messages`:** the first 100 by default (`--limit 0` for all), `--after
|
||||
<id>` or `latest`; data as stored, a line break in it spanning lines;
|
||||
`--follow` checks every `--interval` ms (500) until stopped or the
|
||||
connection fails (exit 1); `--time` adds the push time between the id and
|
||||
`--follow` checks every `--interval` ms (500, up to an hour, pinging
|
||||
every minute meanwhile) until stopped or the connection fails (exit 1); `--time` adds the push time between the id and
|
||||
the data.
|
||||
- **Attachments:** `push -a <file>`; `attachment` replaces the file,
|
||||
leaves none if it fails; `latest` says a message's (", not on this
|
||||
|
||||
+13
-1
@@ -89,10 +89,22 @@ peers' `send_topics`.
|
||||
`[roles."site.reader"]`. Unquoted, TOML reads dots as tables: refused,
|
||||
`[retention]` says to quote.
|
||||
- **Durations:** numbers with a unit (`s m h d w`), added up: `"30s"`,
|
||||
`"1d12h"`. Ages in `[retention]` can also be `"forever"`.
|
||||
`"1d12h"`. Ages in `[retention]` can also be `"forever"`, and are 100
|
||||
years (`"36500d"`) at most (2026-11-19): longer ones once overflowed
|
||||
when turned into milliseconds, a huge age wrapping around to almost
|
||||
nothing.
|
||||
- **Sizes:** bytes, or `"500M"` with `K M G T` (1024-based), up to 1T.
|
||||
- **Paths:** relative ones are from the config file's folder.
|
||||
|
||||
## Database connections
|
||||
|
||||
Reads and queries each have a pool of SQLite connections: one is opened
|
||||
when none is idle, then kept for the next request.
|
||||
`[server] idle_database_connections` (8 by default, 0 to 512) is how many
|
||||
idle ones each pool keeps; more are closed as they're put back, and an
|
||||
idle one is closed after a minute unused (looked at every 10 s). 0 opens
|
||||
one per request.
|
||||
|
||||
## Retention
|
||||
|
||||
A topic takes its name's line, else the first pattern matching it in the
|
||||
|
||||
@@ -61,13 +61,6 @@ network**, not exposed to the internet. So:
|
||||
From a read of the source on 2026-11-15: no test shows any of these yet.
|
||||
Reproduce one before fixing it, and move it up (or drop it) once known.
|
||||
|
||||
- **`sync_interval` over 5 min.** The server dialed closes the idle link
|
||||
(`IDLE_TIMEOUT`), so every check-in fails once ("closed the link",
|
||||
warned), then the link is dialed again after `retry_delay`. Nothing
|
||||
in the config caps `sync_interval`. Left as it is (the owner,
|
||||
2026-11-16): replicating every 5 min or more isn't a real use, the
|
||||
example config says to keep it under. Fix if needed: cap it under the
|
||||
idle timeout, or have an idle link ask its `SyncState` more often.
|
||||
- **Resuming a big replicated attachment.** Before answering `Continue`,
|
||||
the receiver reads back and hashes every part it already has
|
||||
(`accept_from.rs`, `replicate_attachment`, `hash_parts`), while the
|
||||
@@ -79,11 +72,6 @@ Reproduce one before fixing it, and move it up (or drop it) once known.
|
||||
the example config says so by `max_attachment_size` (1 GiB by
|
||||
default). Fix if needed: a wait scaled to the parts already received,
|
||||
or the hash's state kept with the attachment.
|
||||
- **`SyncState` in one packet.** Every head the peer may
|
||||
send goes in one frame of 16 MiB at most: with very many topic and
|
||||
node pairs (hundreds of thousands) it doesn't fit, the packet can't
|
||||
be sent and replication stops. Fix: heads in several packets, or only
|
||||
those that changed.
|
||||
- **Peers with several roles.** `SyncState` lists the union of its
|
||||
roles' `accept_topics` and the union of their `accept_nodes`
|
||||
(`Permissions::accepted`), while what's taken is each role on its
|
||||
|
||||
+33
-9
@@ -20,8 +20,11 @@ connecting, pushes and messages (`server.md`).
|
||||
expected), no host names or IPs. Revoked certificates are in the CA's
|
||||
CRL: servers check it, and so do clients given it (servers dialing their
|
||||
peers too).
|
||||
- **ALPN `bakashrot`** (`limits::ALPN`): both ends offer it, what isn't
|
||||
BakaShrot doesn't get past the TLS handshake. It isn't versioned.
|
||||
- **ALPN `bakashrot`** (`limits::ALPN`): both ends offer it and check it
|
||||
was agreed on, what isn't BakaShrot doesn't get past TLS. rustls only
|
||||
refuses a client offering other protocols, so the server closes one
|
||||
offering none right after the handshake, before the versions
|
||||
(2026-11-19). It isn't versioned.
|
||||
- **The versions, first:** right after TLS, each end sends its protocol
|
||||
version, 4 bytes little-endian (`frame::write_version()`), and reads the
|
||||
other's (`frame::check_version()`). Different versions: both ends
|
||||
@@ -36,6 +39,11 @@ connecting, pushes and messages (`server.md`).
|
||||
or over `MAX_PACKET_SIZE` (16 MiB), aborts the connection as soon as it
|
||||
arrived. The blocking client reads and writes them with `frame::read()`
|
||||
/ `write()`, the async server with `frame::codec()` (`tokio-util`).
|
||||
The limit applies to authenticated links only, as the owner asked
|
||||
(2026-11-08, then 4 KB before authenticating): TLS authenticates both
|
||||
ends before any frame, so nothing unauthenticated reaches one (rustls
|
||||
bounds the handshake itself). Kept rather than lifted (2026-11-19): a
|
||||
buggy peer announcing gigabytes would make the other end hold them.
|
||||
|
||||
## Packets
|
||||
|
||||
@@ -84,8 +92,8 @@ format, its values are checked all the same.)
|
||||
|
||||
Direction: **C** client, **S** server, **P** peer (a server linked to
|
||||
another, as its client). Ids are protocol 7's own (6 can't connect; 8
|
||||
added `ListNames`' filters); a new packet takes the next one, none is
|
||||
ever reused.
|
||||
added `ListNames`' filters and `SyncState`'s pages); a new packet takes
|
||||
the next one, none is ever reused.
|
||||
|
||||
| Id | Packet | Dir | Fields | Answered by |
|
||||
|---|---|---|---|---|
|
||||
@@ -103,10 +111,12 @@ ever reused.
|
||||
| 12 | NamesList | S→C | `kind`, `truncated` bool, `[name...]` (65536 at most) | |
|
||||
| 13 | Query | C→S | `sql` str | QueryResult / RequestRefused |
|
||||
| 14 | QueryResult | S→C | `json` str: `{"columns":[...],"rows":[[...],...]}` | |
|
||||
| 15 | SyncStateRequest | P→S | none | SyncState / RequestRefused |
|
||||
| 16 | SyncState | S→P | `[topic...]`, `[node...]` (names and patterns it takes from P), `[[topic, node, last_id]...]`, `[[topic, node, message_id, next_part]...]` (attachments it lacks, 256 at most) | |
|
||||
| 15 | SyncStateRequest | P→S | `after`: `[topic, node]`, the last head of the previous page, or nil for the first | SyncState / RequestRefused |
|
||||
| 16 | SyncState | S→P | `more` bool, `[topic...]`, `[node...]` (names and patterns it takes from P), `[[topic, node, last_id]...]` (in the order of their names, 32768 at most), `[[topic, node, message_id, next_part]...]` (attachments it lacks, 256 at most); the lists and attachments in the first page only | |
|
||||
| 17 | ReplicateMessages | P→S | `topic`, `node`, `[[id, pushed_at, data, [size, sha256] or nil]...]` (1-10000, ids strictly increasing) | MessagePushed (the last id) / RequestRefused |
|
||||
| 18 | ReplicateAttachment | P→S | `topic`, `node`, `message_id`, `from_part` | Continue, then its parts, then MessagePushed / RequestRefused |
|
||||
| 19 | Ping | C→S, P→S | none: keeps an idle connection open | Pong |
|
||||
| 20 | Pong | S→C, S→P | none | |
|
||||
|
||||
## Flows
|
||||
|
||||
@@ -118,11 +128,13 @@ Reading C: GetMessages(after: id) S: Messages, `more` if other
|
||||
the latest C: GetMessages(after: nil, 1) S: Messages (empty if none)
|
||||
An attachment C: GetAttachment(from_part) S: Attachment, then AttachmentPart x the rest
|
||||
/ RequestRefused (NotFound, NotReady)
|
||||
Replicating P: SyncStateRequest S: SyncState
|
||||
Replicating P: SyncStateRequest S: SyncState, `more` if others follow
|
||||
P: SyncStateRequest(after) x pages S: SyncState
|
||||
P: ReplicateMessages x the batches S: MessagePushed each / RequestRefused
|
||||
P: ReplicateAttachment x missing S: Continue / RequestRefused
|
||||
(NotReady: another peer sending it)
|
||||
P: AttachmentPart x from next_part S: MessagePushed / RequestRefused
|
||||
Keeping alive C: Ping (every minute while idle) S: Pong
|
||||
```
|
||||
|
||||
- **Parts are streamed:** no request or answer per part, TCP paces them.
|
||||
@@ -133,8 +145,12 @@ Replicating P: SyncStateRequest S: SyncState
|
||||
them failed, then refuses: the client never has to read while sending.
|
||||
- **A server failing while it sends parts** closes the connection; the
|
||||
client asks again `from_part` the next one it needs.
|
||||
- **Replication:** `SyncState` is all a check-in needs. The peer compares
|
||||
it with what it has, by name, sends the messages the server lacks in
|
||||
- **Replication:** `SyncState` is all a check-in needs. Its heads come
|
||||
in pages of `MAX_HEADS_PER_PACKET` (32768): the biggest first page,
|
||||
every list full and every name 64 characters, is about 13.3 MB, in a
|
||||
16 MiB frame (`the_biggest_sync_state_fits`). The peer asks for the
|
||||
next `after` the last head while `more`, then compares them all with
|
||||
what it has, by name, sends the messages the server lacks in
|
||||
batches of up to `batch_size` messages and about 1 MiB (a bigger
|
||||
message alone), then the attachments it listed.
|
||||
|
||||
@@ -142,6 +158,14 @@ Replicating P: SyncStateRequest S: SyncState
|
||||
|
||||
- **One request at a time:** a client sends one, waits for its answer (the
|
||||
parts of an attachment being part of its request or answer).
|
||||
- **Silence:** the server closes a connection with no request for
|
||||
`IDLE_TIMEOUT_SECS` (5 min). An end waiting longer between requests
|
||||
sends a `Ping` every `KEEPALIVE_SECS` (1 min) meanwhile (added
|
||||
2026-11-19, protocol 8 not released yet): the tool's and the bindings'
|
||||
follows and the replication links do it themselves; the DLL has no
|
||||
thread of its own, so an app idle that long calls `BakaShrot_Ping()`.
|
||||
A `Ping` needs no permission; a revoked certificate's is dropped like
|
||||
any request.
|
||||
- **Refuse or abort:** valid but not allowed or not found (permissions,
|
||||
unknown names, over a limit, a failed query) is a `RequestRefused` with a
|
||||
code, the connection stays. Malformed or out of order (a bad frame, a
|
||||
|
||||
+11
-4
@@ -118,7 +118,9 @@ otherwise:
|
||||
|
||||
1. `SyncStateRequest`: the other server answers what it takes from this
|
||||
one (topics and nodes, patterns included), its last id in each topic
|
||||
and node, and the attachments it lacks (256 at most).
|
||||
and node, and the attachments it lacks (256 at most). The last ids
|
||||
come in pages of 32768, each asked after the last of the previous
|
||||
one, until none follow.
|
||||
2. **The messages:** for each topic and node here that it takes and that
|
||||
its roles here may be sent (`send_topics`, `send_nodes`), and where
|
||||
it's behind, the messages after its
|
||||
@@ -157,7 +159,8 @@ the same ids with different messages, undetected.
|
||||
## A connection
|
||||
|
||||
1. **TLS:** the client's certificate must be the CA's, valid, not revoked,
|
||||
with an identity (`certificates.md`). A refused one gets TLS's alert,
|
||||
with an identity (`certificates.md`), and ALPN `bakashrot` agreed on
|
||||
(a client offering none is closed at once). A refused one gets TLS's alert,
|
||||
then a gentle close: the server shuts down and reads until the client
|
||||
closes (1 s at most). Closing at once reset the connection (the
|
||||
client's version bytes unread), which could beat the alert to the
|
||||
@@ -176,7 +179,7 @@ the same ids with different messages, undetected.
|
||||
| Timeout | |
|
||||
|---|---|
|
||||
| TLS, versions, welcome | 10 s (the server); the client's connect too |
|
||||
| Silence between requests | 5 min, then the server closes |
|
||||
| Silence between requests | 5 min, then the server closes (a `Ping` is a request: links and follows send one every minute while they wait) |
|
||||
| A packet sent (an answer, an attachment's part, to a peer too) | 30 s (the server), then it closes: a client that stopped reading doesn't hold a connection |
|
||||
| An answer | 30 s (the client) |
|
||||
| An attachment's next part | 30 s (the server), then it closes |
|
||||
@@ -355,7 +358,11 @@ each:
|
||||
- refusals keeping the connection (not allowed, unknown topic, a bad name
|
||||
the client catches);
|
||||
- certificates refused (no known role, with why; another CA's client; a
|
||||
client trusting another CA; nothing listening);
|
||||
client trusting another CA; a client offering no ALPN; nothing
|
||||
listening);
|
||||
- pings (answered with no permission needed, a `Pong` sent to the server
|
||||
closing it, a revoked certificate's ping dropped); a server keeping no
|
||||
idle database connection still serving reads and queries;
|
||||
- revoked certificates (an open connection dropped at its next request, a
|
||||
new one refused, others going on, a bad CRL refused);
|
||||
- six connections pushing at once (one id stream per node, none lost or
|
||||
|
||||
+2
-2
@@ -97,8 +97,8 @@ read while the writer writes. Answers are the protocol's own types:
|
||||
| Reader | Gives | How |
|
||||
|---|---|---|
|
||||
| `messages(topic, node, after, limit)` | `Messages`: after an id in order, or the last ones; about 1 MiB at most (a bigger message alone), `more` if others follow | a range of keys |
|
||||
| `topics()`, `nodes()` | the names with messages | `streams` |
|
||||
| `heads()`, `last_id(topic, node)` | each stream's last id: `SyncState` | `streams` |
|
||||
| `topics()` | the topics with messages | `streams` |
|
||||
| `heads()`, `heads_after(after, limit)`, `last_id(topic, node)` | each stream's last id, all or a page after a pair (names without case): `SyncState` | `streams` (its `(topic, node)` index) |
|
||||
| `to_replicate(topic, node, after, limit)` | messages after an id as replication sends them (attachments' SHA-256s), about 1 MiB at most | a range of keys |
|
||||
| `missing_attachments(limit)` | replicated attachments not all here, from which part | `attachments` (index on states) |
|
||||
| `attachment(topic, node, id)`, `read_part(id, part)` | an attachment, its parts | the message's key; a part's key |
|
||||
|
||||
Reference in New Issue
Block a user