Struct MemorySyncPeer
pub struct MemorySyncPeer { /* private fields */ }memory only.Expand description
An in-memory peer.
Send + Sync, so it implements SendSyncPeer and gets the bare
SyncPeer for free. That is the right choice for a
fixture: a suite written against it can be run under tokio::spawn, and the
!Send half of the port is exercised by the Durable-Object-shaped stand-in
in tests/ instead.
Mutex rather than RefCell for exactly that reason, and the cost is
stated rather than hidden: every method can now fail with
MemoryPeerError::Poisoned, a failure mode a single-threaded peer does not
have.
§Examples
A device pushes what it holds to a hub, and a second device pulls it — the hub-and-spoke shape, with the hub played by this type:
use happenstance_core::{Event, EventId, RecordedAt, SequencePosition, StoreId};
use happenstance_sync::{
EventGroup, MemorySyncPeer, PushBatch, ReplicatedEvent, SyncPeer,
};
let tablet = StoreId::from_bytes([1; 16]);
let hub = MemorySyncPeer::new(StoreId::from_bytes([2; 16]));
// An event the tablet already made durable, carrying the identity it minted.
let recorded = ReplicatedEvent::new(
EventId::new(tablet, SequencePosition::FIRST),
RecordedAt::from_millis(1_700_000_000_000),
Event::new("ReadingTaken", &b"{\"celsius\":4}"[..])?,
);
let batch = PushBatch::new(vec![EventGroup::new(None, vec![recorded])]);
// The hub takes it once; offering it again is a skip, never a second copy.
let first = hub.push(&batch).await?;
assert_eq!((first.appended, first.skipped), (1, 0));
let again = hub.push(&batch).await?;
assert_eq!((again.appended, again.skipped), (0, 1));
assert_eq!(again.confirmed.get(tablet), Some(SequencePosition::FIRST));
// Another device pulls from the start and gets it, with an owned token
// that survives this handle being dropped.
let pulled = hub.pull(None).await?;
assert_eq!(pulled.batch.events().count(), 1);
let caught_up = hub.pull(Some(&pulled.resume)).await?;
assert_eq!(caught_up.batch.events().count(), 0, "empty means caught up");
assert_eq!(caught_up.waiting, happenstance_sync::Waiting::Nothing);Implementations§
§impl MemorySyncPeer
impl MemorySyncPeer
pub fn compact_recorded_before(
&self,
cutoff: RecordedAt,
) -> Result<Option<EventId>, MemoryPeerError>
pub fn compact_recorded_before( &self, cutoff: RecordedAt, ) -> Result<Option<EventId>, MemoryPeerError>
Drops the longest prefix of this peer’s log in which every event was
recorded before cutoff, as a peer compacting on a retention window
does, and answers the floor afterwards.
Only a prefix. A group recorded before cutoff that sits after a
newer one stays, because the floor describes resumability, not
completeness (SY-32, ADR-0028). cutoff is read as a window predicate
on each event’s RecordedAt, which never orders events: the prefix
is in this peer’s forwarding order.
The floor is the identity of the newest event dropped, and it only
moves forward: a cutoff that drops nothing more answers the floor as it
was. A pull from a token that has not consumed the dropped groups is
then refused with MemoryPeerError::BelowRetentionFloor, class
Reseed, and
limits reports the floor. A prefix of
groups with no events forgets no history, and is kept.
Dropped identities are forgotten, as a physical prune forgets them, so
a later push of one appends it again; Ack::confirmed
does not move back.
§Errors
MemoryPeerError::Poisoned if the log lock was poisoned.
pub fn floor_token(&self) -> Result<MemoryResume, MemoryPeerError>
pub fn floor_token(&self) -> Result<MemoryResume, MemoryPeerError>
A token that resumes just past this peer’s floor, which accepts the
gap below it: the explicit, operator-side answer to a
Reseed refusal (SY-32).
A pull from the start of a compacted log is refused, as a stale token
is, so an edge that lost its token cannot converge on what is retained
in silence; this is how a caller says the gap is accepted.
MemoryResume::start while nothing has been compacted.
§Errors
MemoryPeerError::Poisoned if the log lock was poisoned.
§impl MemorySyncPeer
impl MemorySyncPeer
pub fn with_limits(store: StoreId, limits: PeerLimits) -> Self
pub fn with_limits(store: StoreId, limits: PeerLimits) -> Self
A peer that declares limits.
Useful for the case the port exists to make survivable: an origin that has already durably committed an event larger than the far side can represent.
limits.retention_floor is not reported: the floor this peer’s own
compaction sets replaces it, None
until then. A floor a peer declares and does not enforce is the silent
failure SY-32 forbids.
pub fn seed(&self, group: EventGroup) -> Result<(), MemoryPeerError>
pub fn seed(&self, group: EventGroup) -> Result<(), MemoryPeerError>
Seeds the peer’s log directly, without going through a push.
For building a fixture’s starting state. Skips the limit checks on purpose: a test that needs an oversized event on the far side has to be able to put one there.
§Errors
MemoryPeerError::Poisoned if the log lock was poisoned.
pub fn group_count(&self) -> Result<usize, MemoryPeerError>
pub fn group_count(&self) -> Result<usize, MemoryPeerError>
Trait Implementations§
§impl Debug for MemorySyncPeer
impl Debug for MemorySyncPeer
§impl SendSyncPeer for MemorySyncPeer
impl SendSyncPeer for MemorySyncPeer
§async fn push(&self, batch: &PushBatch) -> Result<Ack, Self::Error>
async fn push(&self, batch: &PushBatch) -> Result<Ack, Self::Error>
Appends each event this peer has never held, skips each it has, and acknowledges with what it now holds.
Ack::confirmed is a per-origin high-water mark, not a contiguous
prefix: dedupe is by exact identity, so after a delayed page this peer
can confirm an origin’s position 2 while it still lacks position 1.
Read it as “the highest position held”, never as “everything up to”.
§Errors
MemoryPeerError::BatchTooLarge for a batch over the declared
max_batch_bytes, and MemoryPeerError::Poisoned if the log lock
was poisoned.
§fn limits(&self) -> PeerLimits
fn limits(&self) -> PeerLimits
The limits this peer was built with, and the floor its own compaction has set (SY-32).