mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Add pushed_at timestamps and per-topic retention
- Every message is stamped with the time its push arrived (UTC ms since 1970, the server's clock), carried as is by replication. Exposed in LatestMessage and Messages, BakaShrot_GetMessagePushedAt() in the DLL, Message.pushed_at / .pushed in Python, GetMessages --time, GetLatestMessage, and the messages view (pushed_at, pushed_utc). - [BakaShrot.Retention] in each server's config: a max age per topic, by name, id, pattern or *, ages like 30d or 1d12h. A background pass (Server/Storage/Retention.pbi, every RetentionInterval seconds) deletes what's older, in batches through the writer. node_heads is left as is, so ids never go back and peers don't resend what was deleted. - Replicated messages already past the receiver's max age are skipped on arrival, their head still raised so they aren't sent again. - Schema v6: data.pushed_at (older databases stamped at upgrade time), a (topic_id, pushed_at) index, the unused archives table dropped. The writer takes the next id from node_heads. - Folded into protocol 3, not released yet. - Tests/Retention-Test.ps1 (52 checks), Python checks for pushed_at. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RwFeVPXGLs3KK1MAQn3ouP
This commit is contained in:
@@ -122,6 +122,11 @@ Structure BakaShrot_NetClient
|
||||
AcceptTopics.BakaShrot_NameSet
|
||||
AcceptNodes.BakaShrot_NameSet
|
||||
|
||||
; [BakaShrot.Retention], for peers only: what they send that's already
|
||||
; past its max age isn't stored. A copy, lists aren't safe to walk from
|
||||
; several threads.
|
||||
List Retention.BakaShrot_RetentionRule()
|
||||
|
||||
; This thread's own database connection, opened once authenticated.
|
||||
DbId.i
|
||||
EndStructure
|
||||
@@ -259,6 +264,7 @@ Procedure.i BakaShrot_NetClient_Authenticate(*Client.BakaShrot_NetClient)
|
||||
If *Client\PeerName <> "" And FindMapElement(*Server\Config\Peers(), *Client\PeerName)
|
||||
CopyStructure(@*Server\Config\Peers()\AcceptTopics, @*Client\AcceptTopics, BakaShrot_NameSet)
|
||||
CopyStructure(@*Server\Config\Peers()\AcceptNodes, @*Client\AcceptNodes, BakaShrot_NameSet)
|
||||
CopyList(*Server\Config\Retention(), *Client\Retention())
|
||||
EndIf
|
||||
UnlockMutex(*Server\Mutex)
|
||||
If ClientName = ""
|
||||
@@ -543,6 +549,7 @@ Procedure.i BakaShrot_NetClient_HandleGetLatestMessage(*Client.BakaShrot_NetClie
|
||||
Latest\TopicName = BakaShrot_Names_TopicRef(TopicId)
|
||||
Latest\NodeName = BakaShrot_Names_NodeRef(NodeId)
|
||||
Latest\MessageId = Message\MessageId
|
||||
Latest\PushedAt = Message\PushedAt
|
||||
Latest\JsonData = Message\JsonData
|
||||
*Answer = BakaShrot_LatestMessagePacket_Encode(@Latest, @AnswerSize)
|
||||
If Not *Answer
|
||||
@@ -627,6 +634,7 @@ Procedure.i BakaShrot_NetClient_HandleGetMessages(*Client.BakaShrot_NetClient, *
|
||||
|
||||
AddElement(Answer\Messages())
|
||||
Answer\Messages()\MessageId = Messages()\MessageId
|
||||
Answer\Messages()\PushedAt = Messages()\PushedAt
|
||||
Answer\Messages()\JsonData = Messages()\JsonData
|
||||
Next
|
||||
ClearList(Messages())
|
||||
@@ -907,11 +915,14 @@ EndProcedure
|
||||
; replicates, unless a wildcard there takes a name this server doesn't have,
|
||||
; which is then added, see Storage/Names.pbi. The sync state tells the peer
|
||||
; what's taken here, so it doesn't send the rest in the first place.
|
||||
; Messages already past the topic's max age here ([BakaShrot.Retention]) are
|
||||
; skipped, but acknowledged like the others.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleReplicateMessages(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Batch.BakaShrot_ReplicateMessages
|
||||
Protected Pushed.BakaShrot_MessagePushed
|
||||
Protected TopicId.i, NodeId.i, TopicRef.s, NodeRef.s, LastMessageId.q, IsStored.i, StoreError.String
|
||||
Protected MaxAge.q, SkipBefore.q, Skipped.Integer
|
||||
Protected *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
*Batch = 0
|
||||
@@ -960,8 +971,15 @@ Procedure.i BakaShrot_NetClient_HandleReplicateMessages(*Client.BakaShrot_NetCli
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The messages couldn't be stored")
|
||||
EndIf
|
||||
|
||||
IsStored = InsertReplicatedMessages(TopicId, NodeId, *Batch\Messages(), @StoreError)
|
||||
MaxAge = RetentionMaxAgeMs(*Client\Retention(), TopicId, TopicRef)
|
||||
If MaxAge > 0
|
||||
SkipBefore = BakaShrot_NowUtcMs() - MaxAge
|
||||
EndIf
|
||||
IsStored = InsertReplicatedMessages(TopicId, NodeId, *Batch\Messages(), @StoreError, SkipBefore, @Skipped)
|
||||
FreeStructure(*Batch)
|
||||
If #BakaShrot_Net_LogRequests And Skipped\i > 0
|
||||
PrintN("[Net] Skipped " + Str(Skipped\i) + " messages of " + TopicRef + ":" + NodeRef + " from peer '" + *Client\PeerName + "', past their max age here")
|
||||
EndIf
|
||||
If Not IsStored
|
||||
PrintN("[Net] Failed to store messages from peer '" + *Client\PeerName + "': " + StoreError\s)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The messages couldn't be stored")
|
||||
|
||||
Reference in New Issue
Block a user