Skip to main content

nodedb_types/sync/wire/
resync.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Re-sync request and throttle messages.
4
5use serde::{Deserialize, Serialize};
6
7/// Re-sync request message (bidirectional, 0x50).
8///
9/// Sent when a receiver detects:
10/// - Sequence gap: missing `mutation_id`s in the delta stream
11/// - Checksum failure: CRC32C mismatch on a delta payload
12/// - State divergence: local state inconsistent with received deltas
13///
14/// On receiving a ResyncRequest, the sender should:
15/// 1. Re-send all deltas from `from_mutation_id` onwards, OR
16/// 2. Send a full snapshot if `from_mutation_id` is 0
17#[derive(
18    Debug, Clone, Serialize, Deserialize, zerompk::ToMessagePack, zerompk::FromMessagePack,
19)]
20pub struct ResyncRequestMsg {
21    /// Reason for requesting re-sync.
22    pub reason: ResyncReason,
23    /// Resume from this mutation ID (0 = full re-sync).
24    pub from_mutation_id: u64,
25    /// Collection scope (empty = all collections).
26    pub collection: String,
27    /// Shape the gap was detected on; Origin re-snapshots this shape.
28    /// Empty string if the caller does not know the shape (e.g. legacy path).
29    #[serde(default)]
30    pub shape_id: String,
31}
32
33/// Reason for a re-sync request.
34#[derive(
35    Debug, Clone, Serialize, Deserialize, zerompk::ToMessagePack, zerompk::FromMessagePack,
36)]
37#[serde(rename_all = "snake_case")]
38#[non_exhaustive]
39pub enum ResyncReason {
40    /// Detected missing mutation IDs in the delta stream.
41    #[serde(rename = "sequence_gap")]
42    SequenceGap {
43        /// The expected next mutation ID.
44        expected: u64,
45        /// The mutation ID that was actually received.
46        received: u64,
47    },
48    /// CRC32C checksum mismatch on a delta payload.
49    #[serde(rename = "checksum_mismatch")]
50    ChecksumMismatch {
51        /// The mutation ID of the corrupted delta.
52        mutation_id: u64,
53    },
54    /// Corruption detected on cold start, need full re-sync.
55    #[serde(rename = "corrupted_state")]
56    CorruptedState,
57}
58
59/// Downstream throttle message (client → server, 0x52).
60///
61/// Sent by Lite when its incoming shape delta queue is overwhelmed.
62/// Origin should reduce its push rate for this peer until a
63/// `Throttle { throttle: false }` is received.
64#[derive(
65    Debug, Clone, Serialize, Deserialize, zerompk::ToMessagePack, zerompk::FromMessagePack,
66)]
67pub struct ThrottleMsg {
68    /// `true` to enable throttling, `false` to release.
69    pub throttle: bool,
70    /// Current queue depth at Lite (informational).
71    pub queue_depth: u64,
72    /// Suggested max deltas per second (0 = use server default).
73    pub suggested_rate: u64,
74}