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}
28
29/// Reason for a re-sync request.
30#[derive(
31    Debug, Clone, Serialize, Deserialize, zerompk::ToMessagePack, zerompk::FromMessagePack,
32)]
33#[serde(rename_all = "snake_case")]
34#[non_exhaustive]
35pub enum ResyncReason {
36    /// Detected missing mutation IDs in the delta stream.
37    #[serde(rename = "sequence_gap")]
38    SequenceGap {
39        /// The expected next mutation ID.
40        expected: u64,
41        /// The mutation ID that was actually received.
42        received: u64,
43    },
44    /// CRC32C checksum mismatch on a delta payload.
45    #[serde(rename = "checksum_mismatch")]
46    ChecksumMismatch {
47        /// The mutation ID of the corrupted delta.
48        mutation_id: u64,
49    },
50    /// Corruption detected on cold start, need full re-sync.
51    #[serde(rename = "corrupted_state")]
52    CorruptedState,
53}
54
55/// Downstream throttle message (client → server, 0x52).
56///
57/// Sent by Lite when its incoming shape delta queue is overwhelmed.
58/// Origin should reduce its push rate for this peer until a
59/// `Throttle { throttle: false }` is received.
60#[derive(
61    Debug, Clone, Serialize, Deserialize, zerompk::ToMessagePack, zerompk::FromMessagePack,
62)]
63pub struct ThrottleMsg {
64    /// `true` to enable throttling, `false` to release.
65    pub throttle: bool,
66    /// Current queue depth at Lite (informational).
67    pub queue_depth: u64,
68    /// Suggested max deltas per second (0 = use server default).
69    pub suggested_rate: u64,
70}