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
+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() {