mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
9caf816dca
- 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
192 lines
7.1 KiB
Plaintext
192 lines
7.1 KiB
Plaintext
; ============================================================================
|
|
; ...
|
|
; ============================================================================
|
|
|
|
|
|
XIncludeFile "../Packets.pbi"
|
|
|
|
; One message of a ReplicateMessages batch.
|
|
; PushedAt: when it was pushed to its origin (Packets.pbi), stored as is.
|
|
Structure BakaShrot_ReplicatedMessage
|
|
MessageId.q
|
|
PushedAt.q
|
|
JsonData.s
|
|
EndStructure
|
|
|
|
; Most messages a batch may carry, whatever the sender's PeerBatchSize.
|
|
; #BakaShrot_ReplicateBatchBytes usually stops a batch well before that.
|
|
#BakaShrot_MaxReplicateBatch = 10000
|
|
|
|
; About how big a batch gets: messages are added while they fit in it, and
|
|
; a bigger one goes alone (up to #BakaShrot_MaxMessageSize). Small enough to
|
|
; keep each batch's transaction and memory short, like when 64 KB was the
|
|
; biggest packet.
|
|
#BakaShrot_ReplicateBatchBytes = 65536
|
|
|
|
; Encoded size of a batch's own fields, and of each message on top of its
|
|
; JSON, rounded up. Used to stop a batch before it gets over #BakaShrot_MaxPacketSize.
|
|
#BakaShrot_ReplicateBatchOverhead = 64 + 2 * (#BakaShrot_MaxNameLength + 2)
|
|
#BakaShrot_ReplicateMessageOverhead = 25
|
|
|
|
; Peer -> Server, messages the server is missing for one topic/node, in
|
|
; message id order, with the message ids they were stored under on their
|
|
; origin. Topic and Node are names, never ids (Commons/Networking/Names.pbi):
|
|
; the server stores them under its own ids for those names, and refuses
|
|
; names it doesn't know.
|
|
; On the wire: topic, node, message count, then each message's id, PushedAt and JSON.
|
|
; Answered with a MessagePushed carrying the last message's id, or a RequestRefused.
|
|
Structure BakaShrot_ReplicateMessages Extends BakaShrot_PacketBase
|
|
Topic.s
|
|
Node.s
|
|
List Messages.BakaShrot_ReplicatedMessage()
|
|
EndStructure
|
|
|
|
|
|
; Encodes *Batch into a freshly allocated buffer, its base fields are set by this procedure.
|
|
; *OutSize receives its size in bytes.
|
|
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
|
|
Procedure.i BakaShrot_ReplicateMessagesPacket_Encode(*Batch.BakaShrot_ReplicateMessages, *OutSize.Integer)
|
|
Protected *MsgPackData.MsgPackData, *Buffer
|
|
|
|
*Batch\Magic = #BakaShrot_ProtocolMagic
|
|
*Batch\Version = #BakaShrot_ProtocolVersion
|
|
*Batch\PacketType = #BakaShrot_PacketType_ReplicateMessages
|
|
|
|
If ListSize(*Batch\Messages()) < 1 Or ListSize(*Batch\Messages()) > #BakaShrot_MaxReplicateBatch
|
|
ProcedureReturn 0
|
|
EndIf
|
|
|
|
*MsgPackData = MsgPackCreate(256)
|
|
If Not *MsgPackData
|
|
ProcedureReturn 0
|
|
EndIf
|
|
|
|
If Not BakaShrot_WritePacketBase(*Batch, *MsgPackData)
|
|
Goto EncodeFailed
|
|
EndIf
|
|
|
|
If MsgPackWriteString(*MsgPackData, *Batch\Topic) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
|
|
If MsgPackWriteString(*MsgPackData, *Batch\Node) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
|
|
; Messages
|
|
If MsgPackWriteInteger(*MsgPackData, ListSize(*Batch\Messages())) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
ForEach *Batch\Messages()
|
|
If MsgPackWriteInteger(*MsgPackData, *Batch\Messages()\MessageId) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
If MsgPackWriteInteger(*MsgPackData, *Batch\Messages()\PushedAt) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
If MsgPackWriteString(*MsgPackData, *Batch\Messages()\JsonData) <> #MsgPack_Error_Success
|
|
Goto EncodeFailed
|
|
EndIf
|
|
Next
|
|
|
|
; Frees *MsgPackData on success, leaves it untouched on failure.
|
|
*Buffer = MsgPackFinalize(*MsgPackData)
|
|
If Not *Buffer
|
|
Goto EncodeFailed
|
|
EndIf
|
|
|
|
*OutSize\i = MemorySize(*Buffer)
|
|
ProcedureReturn *Buffer
|
|
|
|
EncodeFailed:
|
|
MsgPackFree(*MsgPackData, #True)
|
|
ProcedureReturn 0
|
|
EndProcedure
|
|
|
|
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
|
|
; Message ids must be 1 or more, and strictly increasing.
|
|
; *MsgPackData stays owned by the caller, and should be discarded on failure.
|
|
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
|
|
Procedure.i BakaShrot_ReplicateMessagesPacket_Decode(*MsgPackData.MsgPackData)
|
|
Protected *Batch.BakaShrot_ReplicateMessages
|
|
Protected Value.q, Count.q, i.q, PreviousMessageId.q
|
|
|
|
*Batch = AllocateStructure(BakaShrot_ReplicateMessages)
|
|
If Not *Batch
|
|
ProcedureReturn 0
|
|
EndIf
|
|
|
|
; The base parser doesn't check which packet it is, that's on us.
|
|
If Not BakaShrot_ReadPacketBase(*Batch, *MsgPackData) Or *Batch\PacketType <> #BakaShrot_PacketType_ReplicateMessages
|
|
Goto DecodeFailed
|
|
EndIf
|
|
|
|
; Topic
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
|
Goto DecodeFailed
|
|
EndIf
|
|
*Batch\Topic = MsgPackReadString(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Batch\Topic)
|
|
Goto DecodeFailed
|
|
EndIf
|
|
|
|
; Node
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
|
Goto DecodeFailed
|
|
EndIf
|
|
*Batch\Node = MsgPackReadString(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Batch\Node)
|
|
Goto DecodeFailed
|
|
EndIf
|
|
|
|
; Message count
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
|
|
Goto DecodeFailed
|
|
EndIf
|
|
Count = MsgPackReadInteger(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Count < 1 Or Count > #BakaShrot_MaxReplicateBatch
|
|
Goto DecodeFailed
|
|
EndIf
|
|
|
|
; Messages
|
|
PreviousMessageId = 0
|
|
For i = 1 To Count
|
|
AddElement(*Batch\Messages())
|
|
|
|
; MessageId
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
|
|
Goto DecodeFailed
|
|
EndIf
|
|
*Batch\Messages()\MessageId = MsgPackReadInteger(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Batch\Messages()\MessageId <= PreviousMessageId
|
|
Goto DecodeFailed
|
|
EndIf
|
|
PreviousMessageId = *Batch\Messages()\MessageId
|
|
|
|
; PushedAt
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
|
|
Goto DecodeFailed
|
|
EndIf
|
|
*Batch\Messages()\PushedAt = MsgPackReadInteger(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidTimestamp(*Batch\Messages()\PushedAt)
|
|
Goto DecodeFailed
|
|
EndIf
|
|
|
|
; JsonData
|
|
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
|
|
Goto DecodeFailed
|
|
EndIf
|
|
*Batch\Messages()\JsonData = MsgPackReadString(*MsgPackData)
|
|
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
|
|
Goto DecodeFailed
|
|
EndIf
|
|
Next
|
|
|
|
ProcedureReturn *Batch
|
|
|
|
; Everything read so far is tainted, drop it.
|
|
DecodeFailed:
|
|
FreeStructure(*Batch)
|
|
ProcedureReturn 0
|
|
EndProcedure
|