1
0
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:
2026-11-19 23:19:58 +01:00
parent c513d8bf05
commit 6fae7e61c9
34 changed files with 1066 additions and 164 deletions
+5 -3
View File
@@ -32,10 +32,12 @@ synchronous = "full"
# well under it (more than a few GB is a lot for this anyway).
max_attachment_size = "1G" # bytes, or K M G T (1024-based); 0: none
retention_interval = "1m" # how often old messages are deleted
idle_database_connections = 8 # kept open once idle, for reads and as many for queries; closed after 1 min unused
# How long each topic's messages are kept, from when they were first
# pushed. A topic takes its name's line, else the first pattern matching
# it, else "*"'s; without any, it's kept forever.
# pushed: 100 years ("36500d") at most, or "forever". A topic takes its
# name's line, else the first pattern matching it, else "*"'s; without
# any, it's kept forever.
[retention]
logs = "30d"
"sensors.*" = "12h"
@@ -47,7 +49,7 @@ logs = "30d"
push_to = [] # dialed and sent to: ["10.0.0.2:7000", "server-2.lan:7000"]
accept_from = [] # taken from: ["10.0.0.2", "server-2.lan"], or ["*"]
retry_delay = "10s"
sync_interval = "5s" # keep it under 5 min: a link idle that long is closed
sync_interval = "5s" # a link waiting longer than a minute pings meanwhile
batch_size = 1000 # messages per packet, 1-10000
# What certificates with a role may do. A certificate gets everything its
+29
View File
@@ -29,6 +29,11 @@ use std::path::{Path, PathBuf};
use std::time::Duration;
use values::{DURATION_HELP, NAME_HELP, parse_age};
/// The longest retention age: 100 years (36500 days). Longer is
/// "forever"; past about 584 million years, an age wouldn't even fit in
/// milliseconds.
pub const MAX_RETENTION_AGE: Duration = Duration::from_secs(36_500 * 86_400);
/// What's wrong with a config, in words: where (the file, the line) and why.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error(String);
@@ -95,6 +100,14 @@ pub struct Server {
deserialize_with = "values::duration"
)]
pub retention_interval: Duration,
/// The most database connections kept open once idle, for reads, and
/// as many for queries (0 to 512); 8 by default. More are opened
/// while they're needed, and closed after a minute unused.
#[serde(
default = "defaults::idle_database_connections",
deserialize_with = "values::idle_connections"
)]
pub idle_database_connections: usize,
}
/// How hard the database makes sure a push is on disk before answering it
@@ -165,6 +178,11 @@ impl TryFrom<IndexMap<String, toml::Value>> for Retention {
let age = age.ok_or_else(|| {
format!("{key} = {value} isn't an age: {DURATION_HELP}, or \"forever\"")
})?;
if age.is_some_and(|age| age > MAX_RETENTION_AGE) {
return Err(format!(
"{key} = {value}: 100 years (36500d) at most, or \"forever\""
));
}
lines.push((key, age));
}
Ok(Retention { lines })
@@ -185,6 +203,14 @@ impl Retention {
.or_else(|| self.lines.iter().find(|(key, _)| key == "*"))
.and_then(|(_, age)| *age)
}
/// When `topic`'s messages pushed before are past their age, for a
/// clock at `now` (both in ms since 1970), `None` if they're kept
/// forever. 0 while the clock is earlier than the age.
pub fn cutoff(&self, topic: &str, now: u64) -> Option<u64> {
let age = self.age(topic)?;
Some(now.saturating_sub(u64::try_from(age.as_millis()).unwrap_or(u64::MAX)))
}
}
/// `[replication]`.
@@ -238,6 +264,9 @@ mod defaults {
pub(crate) fn retention_interval() -> Duration {
Duration::from_secs(60)
}
pub(crate) fn idle_database_connections() -> usize {
8
}
}
/// `[roles.*]`: each section's name is its role's, none twice.
+15
View File
@@ -184,6 +184,21 @@ pub(crate) fn batch_size<'de, D: Deserializer<'de>>(reader: D) -> Result<u32, D:
Ok(size)
}
/// The most idle database connections a server keeps per pool.
pub(crate) const MAX_IDLE_CONNECTIONS: u32 = 512;
/// How many idle database connections to keep, 0 to
/// `MAX_IDLE_CONNECTIONS`.
pub(crate) fn idle_connections<'de, D: Deserializer<'de>>(reader: D) -> Result<usize, D::Error> {
let count = u32::deserialize(reader)?;
if count > MAX_IDLE_CONNECTIONS {
return Err(D::Error::custom(format!(
"{count} idle database connections: 0 to {MAX_IDLE_CONNECTIONS}"
)));
}
Ok(count as usize)
}
/// Checks a list of names or patterns, none twice.
pub(crate) fn check_names(list: &[String]) -> Result<(), String> {
for (at, entry) in list.iter().enumerate() {
+39
View File
@@ -59,6 +59,7 @@ fn defaults() {
assert_eq!(server.synchronous, Synchronous::Full);
assert_eq!(server.max_attachment_size, 1 << 30);
assert_eq!(server.retention_interval, Duration::from_secs(60));
assert_eq!(server.idle_database_connections, 8);
assert_eq!(config.retention.age("logs"), None);
assert_eq!(config.replication.retry_delay, Duration::from_secs(10));
assert_eq!(config.replication.sync_interval, Duration::from_secs(5));
@@ -75,6 +76,7 @@ database = "data/messages.sqlite"
synchronous = "NORMAL"
max_attachment_size = 0
retention_interval = "1h30m"
idle_database_connections = 0
[replication]
push_to = ["10.0.0.2:7000", "server-3.lan:7001", "[fd00::4]:7000"]
@@ -89,6 +91,7 @@ batch_size = 10000
assert_eq!(config.server.synchronous, Synchronous::Normal);
assert_eq!(config.server.max_attachment_size, 0);
assert_eq!(config.server.retention_interval, Duration::from_secs(5400));
assert_eq!(config.server.idle_database_connections, 0);
assert_eq!(config.replication.push_to.len(), 3);
assert_eq!(config.replication.accept_from[2], "fd00::4");
assert_eq!(config.replication.retry_delay, Duration::from_secs(60));
@@ -122,6 +125,24 @@ logs = "30d"
assert_eq!(age("other"), days(365));
}
/// Ages go up to 100 years, and the cutoff never wraps around.
#[test]
fn retention_cutoff() {
let config = Config::parse(&with(
"[retention]\nlogs = \"36500d\"\nsensors = \"1h\"\n\"*\" = \"forever\"\n",
))
.unwrap();
assert_eq!(config.retention.age("logs"), days(36_500));
let now = 2_000_000_000_000;
assert_eq!(
config.retention.cutoff("sensors", now),
Some(now - 3_600_000)
);
// Longer than the clock is old: nothing is past it yet.
assert_eq!(config.retention.cutoff("logs", now), Some(0));
assert_eq!(config.retention.cutoff("other", now), None);
}
#[test]
fn permissions() {
let config = Config::parse(&with(
@@ -277,6 +298,14 @@ fn refusals() {
&with("retention_interval = \"0s\"\n"),
"'0s' isn't a duration",
),
(
&with("idle_database_connections = 513\n"),
"513 idle database connections: 0 to 512",
),
(
&with("idle_database_connections = -1\n"),
"invalid value: integer `-1`",
),
// [retention]
(
&with("[retention]\nlogs = \"30\"\n"),
@@ -295,6 +324,16 @@ fn refusals() {
&with("[retention]\nsensors.kitchen = \"1d\"\n"),
"quote names with dots",
),
// 100 years at most: past 2^64 ms an age once wrapped around to
// almost nothing ("18446744073709552s" was 384 ms).
(
&with("[retention]\nlogs = \"36501d\"\n"),
"100 years (36500d) at most",
),
(
&with("[retention]\nlogs = \"18446744073709552s\"\n"),
"100 years (36500d) at most",
),
// [replication]
(
&with("[replication]\npush_to = [\"10.0.0.2\"]\n"),