pub struct SyncClient { /* private fields */ }Expand description
Sync client — manages the WebSocket connection to Origin.
The client runs as a background Tokio task. It:
- Connects to Origin via WebSocket
- Sends handshake with JWT + vector clock + shape subscriptions
- Pushes accumulated CRDT deltas
- Receives shape snapshots and incremental deltas
- Handles rejections via CompensationRegistry
- Auto-reconnects with exponential backoff on disconnect
Implementations§
Source§impl SyncClient
impl SyncClient
Sourcepub fn new(config: SyncConfig, peer_id: u64) -> Self
pub fn new(config: SyncConfig, peer_id: u64) -> Self
Create a new sync client (does not connect yet).
Sourcepub fn with_flow_control(
config: SyncConfig,
peer_id: u64,
flow_config: FlowControlConfig,
) -> Self
pub fn with_flow_control( config: SyncConfig, peer_id: u64, flow_config: FlowControlConfig, ) -> Self
Create a new sync client with custom flow control config.
Sourcepub fn set_identity(&mut self, lite_id: String, epoch: u64)
pub fn set_identity(&mut self, lite_id: String, epoch: u64)
Set the Lite identity for fork detection (called after LiteIdentity::load_or_create).
Sourcepub fn set_compensation_handler(&self, handler: Arc<dyn CompensationHandler>)
pub fn set_compensation_handler(&self, handler: Arc<dyn CompensationHandler>)
Register a compensation handler.
Sourcepub fn shapes(&self) -> &Arc<Mutex<ShapeManager>> ⓘ
pub fn shapes(&self) -> &Arc<Mutex<ShapeManager>> ⓘ
Access the shape manager (for subscribing/unsubscribing).
Sourcepub async fn build_handshake(&self) -> HandshakeMsg
pub async fn build_handshake(&self) -> HandshakeMsg
Build a handshake message from current state.
Sourcepub async fn handle_handshake_ack(&self, ack: &HandshakeAckMsg) -> bool
pub async fn handle_handshake_ack(&self, ack: &HandshakeAckMsg) -> bool
Process a handshake acknowledgment from Origin.
Sourcepub async fn build_delta_pushes(
&self,
pending: &[PendingDelta],
) -> Vec<DeltaPushMsg>
pub async fn build_delta_pushes( &self, pending: &[PendingDelta], ) -> Vec<DeltaPushMsg>
Build DeltaPush messages from pending deltas.
Respects the flow control window: returns at most next_batch_size()
deltas. Each message includes a CRC32C checksum of the delta payload
for integrity verification at Origin.
Sourcepub async fn record_push(&self, mutation_ids: &[u64])
pub async fn record_push(&self, mutation_ids: &[u64])
Record that deltas were pushed (update flow control in-flight tracking).
Sourcepub async fn handle_delta_ack(&self, ack: &DeltaAckMsg)
pub async fn handle_delta_ack(&self, ack: &DeltaAckMsg)
Process a DeltaAck from Origin.
Sourcepub async fn handle_delta_reject(&self, reject: &DeltaRejectMsg)
pub async fn handle_delta_reject(&self, reject: &DeltaRejectMsg)
Process a DeltaReject from Origin.
Sourcepub async fn handle_shape_snapshot(&self, msg: &ShapeSnapshotMsg)
pub async fn handle_shape_snapshot(&self, msg: &ShapeSnapshotMsg)
Process a ShapeSnapshot from Origin.
Sourcepub async fn handle_shape_delta(&self, msg: &ShapeDeltaMsg)
pub async fn handle_shape_delta(&self, msg: &ShapeDeltaMsg)
Process a ShapeDelta from Origin.
Sourcepub async fn handle_clock_sync(&self, msg: &VectorClockSyncMsg)
pub async fn handle_clock_sync(&self, msg: &VectorClockSyncMsg)
Process a VectorClockSync from Origin.
Sourcepub async fn check_sequence_gap(
&self,
shape_id: &str,
lsn: u64,
) -> Option<ResyncRequestMsg>
pub async fn check_sequence_gap( &self, shape_id: &str, lsn: u64, ) -> Option<ResyncRequestMsg>
Check an incoming ShapeDelta for sequence gaps.
For each shape, we track the last LSN received. If the incoming LSN
is not contiguous (gap > 1), this indicates missing deltas in the stream.
Returns Some(ResyncRequestMsg) if a gap is detected, None otherwise.
Note: LSNs may not be strictly +1 sequential (Origin may skip LSNs for other shapes), so we only flag a gap when the new LSN is MORE than 1 ahead of the last seen LSN for the SAME shape. A gap means deltas were lost in transit.
Sourcepub async fn reset_sequence_tracking(&self)
pub async fn reset_sequence_tracking(&self)
Reset sequence tracking state on reconnect.
Sourcepub async fn set_pending_resync(&self, msg: ResyncRequestMsg)
pub async fn set_pending_resync(&self, msg: ResyncRequestMsg)
Store a pending re-sync request (set by gap detection in receive loop).
Sourcepub async fn take_pending_resync(&self) -> Option<ResyncRequestMsg>
pub async fn take_pending_resync(&self) -> Option<ResyncRequestMsg>
Take the pending re-sync request (consumed by delta push loop).
Sourcepub fn build_ping(&self) -> SyncFrame
pub fn build_ping(&self) -> SyncFrame
Build a ping frame.
Sourcepub fn backoff_duration(&self, attempt: u32) -> Duration
pub fn backoff_duration(&self, attempt: u32) -> Duration
Calculate backoff duration for reconnection attempt N.
Sourcepub fn compensation(&self) -> &Arc<CompensationRegistry> ⓘ
pub fn compensation(&self) -> &Arc<CompensationRegistry> ⓘ
Access the compensation registry.
Sourcepub fn config(&self) -> &SyncConfig
pub fn config(&self) -> &SyncConfig
Access config.
Sourcepub fn metrics(&self) -> &Arc<SyncMetrics> ⓘ
pub fn metrics(&self) -> &Arc<SyncMetrics> ⓘ
Access the sync metrics.
Sourcepub async fn update_pending_stats(&self, count: usize, bytes: usize)
pub async fn update_pending_stats(&self, count: usize, bytes: usize)
Update pending queue stats in the flow controller. Called from the push loop after reading pending deltas.
Sourcepub async fn is_queue_full(&self) -> bool
pub async fn is_queue_full(&self) -> bool
Check if the pending queue is at capacity (flow control).
Sourcepub async fn sync_metrics(&self) -> SyncMetricsSnapshot
pub async fn sync_metrics(&self) -> SyncMetricsSnapshot
Get a snapshot of sync metrics for monitoring/health.
Sourcepub async fn reset_flow_control(&self)
pub async fn reset_flow_control(&self)
Reset flow controller on reconnect.
Sourcepub async fn should_refresh_token(&self) -> bool
pub async fn should_refresh_token(&self) -> bool
Check if the JWT token needs proactive refresh (at 80% of lifetime).
Returns true if a refresh should be initiated. Called from the
ping loop to piggyback on the keepalive timer.
Sourcepub async fn initiate_token_refresh(&self) -> Option<TokenRefreshMsg>
pub async fn initiate_token_refresh(&self) -> Option<TokenRefreshMsg>
Initiate a token refresh via the token provider.
Returns Some(TokenRefreshMsg) with the new token if the provider
returned one, or None if the provider failed.
Sourcepub async fn handle_token_refresh_ack(&self, ack: &TokenRefreshAckMsg)
pub async fn handle_token_refresh_ack(&self, ack: &TokenRefreshAckMsg)
Handle a TokenRefreshAck from Origin.
Sourcepub async fn pause_for_auth(&self)
pub async fn pause_for_auth(&self)
Pause delta push due to auth failure. Called when Origin rejects with PermissionDenied, indicating the token has expired.
Sourcepub async fn is_push_paused_for_auth(&self) -> bool
pub async fn is_push_paused_for_auth(&self) -> bool
Check if push is paused for auth.
Auto Trait Implementations§
impl !RefUnwindSafe for SyncClient
impl !UnwindSafe for SyncClient
impl Freeze for SyncClient
impl Send for SyncClient
impl Sync for SyncClient
impl Unpin for SyncClient
impl UnsafeUnpin for SyncClient
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);