Skip to main content

Module source

Module source 

Source
Expand description

Primary-side replication source — bounded backlog of recent mutations, indexed by monotonic offset.

Behaviour at a glance:

  • ReplicationSource::push_mutation is called on every applied write. It assigns the next monotonic offset, encodes the frame with crate::wire::encode_frame, and appends to the backlog.
  • The backlog is bounded by a byte budget (max_bytes, fed from [replication] replication_buffer_size in config). When a new frame would exceed the budget, the oldest frames are dropped to make room.
  • Replicas that disconnect and reconnect within the backlog window resume via ReplicationSource::frames_from. Replicas that fall off the back of the buffer get Err(FromOffset::TooOld) and the caller initiates a full snapshot ship.

The source does not know about replicas — slot tracking lives in crate::slot::SlotTable. The source is a passive structure the streaming loop reads; mutation/serialisation lock policy is the wiring layer’s concern.

use kevy_replicate::source::{FromOffset, ReplicationSource};

let set = kevy_resp::Argv::from(vec![b"SET".to_vec(), b"k".to_vec(), b"v".to_vec()]);
let frame_len = kevy_replicate::wire::encode_frame(0, &set).len();
let mut backlog = ReplicationSource::new(2 * frame_len); // room for two frames
for _ in 0..3 {
    backlog.push_mutation(&set);
}
// a replica that acked offset 2 resumes from the backlog ...
assert_eq!(backlog.frames_from(2).map(Iterator::count), Ok(1));
// ... one still at offset 0 fell off the back and needs a snapshot
assert!(matches!(backlog.frames_from(0), Err(FromOffset::TooOld)));

Structs§

Frame
One encoded mutation frame parked in the backlog.
FramesIter
Iterator over backlog frames returned by ReplicationSource::frames_from.
ReplicationSource
Bounded backlog of recent replicated mutations.

Enums§

FromOffset
Reason ReplicationSource::frames_from cannot serve a replica from the backlog.