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:
@@ -9,7 +9,7 @@ use bakashrot_proto::packets::{
|
||||
Attachment as Sent, Head, MessageEntry, Messages, MissingAttachment, ReplicatedMessage,
|
||||
StoredAttachment,
|
||||
};
|
||||
use rusqlite::{Connection, OptionalExtension, Row};
|
||||
use rusqlite::{Connection, OptionalExtension, Row, params};
|
||||
|
||||
/// About how many bytes of messages one answer carries: past it, the rest
|
||||
/// is left for the next request (`more`). A bigger message goes alone.
|
||||
@@ -160,11 +160,25 @@ impl Reader {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Every topic and node's last id: what `SyncState` compares.
|
||||
/// Every topic and node's last id, in the order of their names
|
||||
/// (without case).
|
||||
pub fn heads(&self) -> Result<Vec<Head>> {
|
||||
self.heads_after(None, usize::MAX)
|
||||
}
|
||||
|
||||
/// Topics and nodes' last ids, what `SyncState` compares: `limit` at
|
||||
/// most, in the order of their names (without case), those after the
|
||||
/// pair `after` (from the first without).
|
||||
pub fn heads_after(&self, after: Option<(&str, &str)>, limit: usize) -> Result<Vec<Head>> {
|
||||
// No name is empty: ("", "") comes before every pair.
|
||||
let (topic, node) = after.unwrap_or(("", ""));
|
||||
let limit = i64::try_from(limit).unwrap_or(i64::MAX);
|
||||
self.connection
|
||||
.prepare_cached("SELECT topic, node, last_id FROM streams ORDER BY topic, node")?
|
||||
.query_map([], |row| {
|
||||
.prepare_cached(
|
||||
"SELECT topic, node, last_id FROM streams WHERE (topic, node) > (?1, ?2) \
|
||||
ORDER BY topic, node LIMIT ?3",
|
||||
)?
|
||||
.query_map(params![topic, node, limit], |row| {
|
||||
Ok(Head {
|
||||
topic: row.get(0)?,
|
||||
node: row.get(1)?,
|
||||
|
||||
@@ -173,6 +173,31 @@ fn pushing_and_reading() {
|
||||
("sensors".into(), "kitchen".into(), 1)
|
||||
]
|
||||
);
|
||||
// In pages: after a pair (its names without case), so many at most.
|
||||
let page = |after, limit| -> Vec<_> {
|
||||
reader
|
||||
.heads_after(after, limit)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|h| (h.topic, h.node))
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(
|
||||
page(None, 2),
|
||||
[
|
||||
("logs".into(), "garage".into()),
|
||||
("logs".into(), "kitchen".into())
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
page(Some(("LOGS", "Kitchen")), 2),
|
||||
[("sensors".into(), "kitchen".into())]
|
||||
);
|
||||
assert_eq!(
|
||||
page(Some(("logs", "h")), 1),
|
||||
[("logs".into(), "kitchen".into())]
|
||||
);
|
||||
assert!(page(Some(("sensors", "kitchen")), 2).is_empty());
|
||||
assert_eq!(reader.last_id("logs", "kitchen").unwrap(), 3);
|
||||
assert_eq!(reader.last_id("nope", "kitchen").unwrap(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user