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
@@ -201,6 +201,18 @@ namespace NibblePoker.BakaShrot
Native.BakaShrot_FreeClient(handle);
}
/// <summary>Keeps the connection open: the server closes one silent
/// for 5 min. An app waiting longer between requests calls it every
/// minute or so (KeepAliveMs); Follow() does it itself.</summary>
public void Ping()
{
lock (sync)
Check(Native.BakaShrot_Ping(Handle()), "Ping");
}
/// <summary>How often to Ping() while waiting between requests, in ms.</summary>
public const int KeepAliveMs = 60000;
/// <summary>Pushes data (JSON or not) to topic, as this client's node: its id.</summary>
public long Push(string topic, string data)
{
@@ -278,8 +290,9 @@ namespace NibblePoker.BakaShrot
}
/// <summary>Every message after id after, then new ones as they
/// come, checking every intervalMs: forever, stop enumerating to
/// stop. Nothing reconnects by itself.</summary>
/// come, checking every intervalMs (pinging every minute meanwhile
/// if it's longer): forever, stop enumerating to stop. Nothing
/// reconnects by itself.</summary>
public IEnumerable<Message> Follow(string topic, string node, long after, int intervalMs)
{
while (true)
@@ -289,7 +302,7 @@ namespace NibblePoker.BakaShrot
yield return message;
after = Math.Max(after, page.LastId);
if (!page.More)
Thread.Sleep(intervalMs);
Wait(intervalMs);
}
}
@@ -419,6 +432,21 @@ namespace NibblePoker.BakaShrot
// --- Inside ---------------------------------------------------------
// Sleeps intervalMs, pinging every KeepAliveMs meanwhile.
private void Wait(int intervalMs)
{
int left = intervalMs;
while (true)
{
int step = Math.Min(left, KeepAliveMs);
Thread.Sleep(step);
left -= step;
if (left <= 0)
return;
Ping();
}
}
private IEnumerable<string> EnumerateNames(int kind, string topic, string node)
{
long enumeration;
@@ -563,6 +591,7 @@ namespace NibblePoker.BakaShrot
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern IntPtr BakaShrot_SetCrlW(long client, string crl);
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern IntPtr BakaShrot_ConnectW(long client, string host, int port);
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_IsConnected(long client);
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_Ping(long client);
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetNodeW(long client, byte[] buffer, IntPtr size);
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_PushMessageToW(long client, string topic, string data);
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern IntPtr BakaShrot_GetLatestMessageOfW(long client, string topic, string node);