pub struct SyncEngine { /* private fields */ }Expand description
The sync engine orchestrates synchronization between local state and a remote sequencer.
This is the Rust equivalent of the JS SyncEngine class, providing:
- Event recording to the outbox
- Push (outbox -> remote) via a
Transport - Pull (remote -> buffer) via a
Transport - Conflict resolution during pull
- Status reporting
§Examples
use stateset_sync::{SyncEngine, SyncConfig, SyncEvent};
use serde_json::json;
let config = SyncConfig::new("agent-1", "tenant-1", "store-1");
let mut engine = SyncEngine::new(config).expect("valid sync config");
let seq = engine.record(SyncEvent::new("order.created", "order", "ORD-1", json!({"total": 99})));
assert!(seq.is_ok());
assert_eq!(engine.pending_count(), 1);Implementations§
Source§impl SyncEngine
impl SyncEngine
Sourcepub fn new(config: SyncConfig) -> Result<Self, SyncError>
pub fn new(config: SyncConfig) -> Result<Self, SyncError>
Create a new SyncEngine with the given configuration.
§Errors
Returns SyncError::InvalidConfig for invalid settings or
SyncError::Storage when durable outbox initialization fails.
Sourcepub fn with_strategy(
config: SyncConfig,
strategy: ConflictStrategy,
) -> Result<Self, SyncError>
pub fn with_strategy( config: SyncConfig, strategy: ConflictStrategy, ) -> Result<Self, SyncError>
Create a SyncEngine with a custom conflict resolution strategy.
§Errors
Returns SyncError::InvalidConfig for invalid settings or
SyncError::Storage when durable outbox initialization fails.
Sourcepub fn try_new(config: SyncConfig) -> Result<Self, SyncError>
pub fn try_new(config: SyncConfig) -> Result<Self, SyncError>
Fallible constructor that validates config and initializes persistence.
§Errors
Returns SyncError::InvalidConfig for invalid settings or
SyncError::Storage when durable outbox initialization fails.
Sourcepub fn try_with_strategy(
config: SyncConfig,
strategy: ConflictStrategy,
) -> Result<Self, SyncError>
pub fn try_with_strategy( config: SyncConfig, strategy: ConflictStrategy, ) -> Result<Self, SyncError>
Fallible constructor with explicit conflict strategy.
§Errors
Returns SyncError::InvalidConfig for invalid settings or
SyncError::Storage when durable outbox initialization fails.
Sourcepub fn record(&mut self, event: SyncEvent) -> Result<u64, SyncError>
pub fn record(&mut self, event: SyncEvent) -> Result<u64, SyncError>
Record an event into the outbox for later push.
Returns the assigned local sequence number.
§Errors
Returns SyncError::OutboxFull if the outbox is at capacity.
Sourcepub async fn push(
&mut self,
transport: &dyn Transport,
) -> Result<PushResult, SyncError>
pub async fn push( &mut self, transport: &dyn Transport, ) -> Result<PushResult, SyncError>
Push pending events from the outbox to the remote via the given transport.
Drains up to batch_size events from the outbox.
§Errors
Returns SyncError::Transport if the transport operation fails.
Sourcepub async fn pull(
&mut self,
transport: &dyn Transport,
) -> Result<PullResult, SyncError>
pub async fn pull( &mut self, transport: &dyn Transport, ) -> Result<PullResult, SyncError>
Pull events from the remote sequencer into the local buffer.
Pulled events are added to the event buffer. If conflicts are
detected (same entity_type + entity_id in both outbox and pulled),
they are resolved using the configured strategy.
§Errors
Returns SyncError::Transport if the transport operation fails.
Sourcepub fn status(&self) -> SyncStatus
pub fn status(&self) -> SyncStatus
Get the current sync status.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Return the number of events pending in the outbox.
Sourcepub fn buffered_count(&self) -> usize
pub fn buffered_count(&self) -> usize
Return the number of events currently in the pull buffer.
Sourcepub fn drain_buffer(&mut self) -> Vec<SyncEvent>
pub fn drain_buffer(&mut self) -> Vec<SyncEvent>
Drain all events from the pull buffer.
Sourcepub const fn config(&self) -> &SyncConfig
pub const fn config(&self) -> &SyncConfig
Return a reference to the sync configuration.
Sourcepub const fn resolver(&self) -> &ConflictResolver
pub const fn resolver(&self) -> &ConflictResolver
Return a reference to the conflict resolver.
Sourcepub async fn full_sync(
&mut self,
transport: &dyn Transport,
) -> Result<(PushResult, PullResult), SyncError>
pub async fn full_sync( &mut self, transport: &dyn Transport, ) -> Result<(PushResult, PullResult), SyncError>
Perform a full sync: push first, then pull.
§Errors
Returns the first error encountered during push or pull.