rmqtt 0.23.0

MQTT Server for v3.1, v3.1.1 and v5.0 protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! MQTT Inflight Message Management System
//!
//! Provides reliable message delivery tracking for QoS 1 and 2 with:
//! - Outbound message retransmission
//! - Inbound message deduplication
//! - Configurable window sizes
//! - Automatic expiry handling
//!
//! ## Core Functionality
//! 1. ​**​Outbound Tracking (OutInflight)​**​:
//!    - Manages unacknowledged publishes (QoS 1/2)
//!    - Handles retransmission timeouts
//!    - Maintains packet ID sequencing
//!    - Supports configurable capacity limits
//!
//! 2. ​**​Inbound Tracking (InInflight)​**​:
//!    - Detects duplicate messages (QoS 2)
//!    - Enforces maximum window size
//!    - Provides packet ID lifecycle management
//!
//! ## Key Features
//! - Dual interval timing (retry/expiry)
//! - Event hooks for push/pop operations
//! - Atomic packet ID generation
//! - Time-based message expiry
//! - Statistics integration
//!
//! ## Implementation Details
//! - DequeMap for O(1) front access
//! - BTreeSet for efficient deduplication
//! - Atomic counters for thread safety
//! - Zero-cost status tracking
//!
//! Configuration Parameters:
//! - `cap`: Maximum concurrent outbound messages
//! - `retry_interval`: Retransmission delay (ms)
//! - `expiry_interval`: Message expiry timeout (ms)
//! - `max_inflight`: Maximum inbound window size
//!
//! Usage Patterns:
//! 1. Assign packet IDs via `next_id()`
//! 2. Track outbound messages with `push_back()`
//! 3. Process acknowledgements with `remove()`
//! 4. Handle timeouts via `pop_front_timeout()`
//! 5. Manage inbound flow with `add()`/`remove()`
//!
//! Note: Implements MQTT spec requirements for:
//! - Packet ID uniqueness (2.2.1)
//! - QoS flow control (4.6)
//! - Message expiry (3.3.2.3.2)

use std::collections::BTreeSet;
use std::num::NonZeroU16;
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::Arc;
use std::time::Duration;

use anyhow::anyhow;
use itertools::Itertools;
use rust_box::dequemap::DequeBTreeMap as DequeMap;
use serde::{Deserialize, Serialize};

use crate::context::ServerContext;
use crate::net::MqttError;
use crate::queue::OnEventFn;
use crate::types::{From, PacketId, Publish, TimestampMillis};
use crate::types::{QoS, Reason};
use crate::utils::timestamp_millis;
use crate::Result;

type OutQueues = DequeMap<PacketId, OutInflightMessage>;

/// Tracks the acknowledgment status of an inflight message.
///
/// Represents the current state in the QoS flow:
/// - `UnAck`: Published but not yet acknowledged by the subscriber.
/// - `UnReceived`: PUBREL sent but PUBREC not yet received (QoS 2 only).
/// - `UnComplete`: QoS 2 handshake initiated but not completed.
#[derive(Debug, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
pub enum MomentStatus {
    /// Published to subscriber, awaiting acknowledgment.
    UnAck,
    /// PUBREL sent, awaiting PUBREC (QoS 2 only).
    UnReceived,
    /// QoS 2 handshake in progress, awaiting completion.
    UnComplete,
}

/// An outbound inflight message with delivery tracking metadata.
///
/// Tracks a publish message that has been sent but not yet
/// acknowledged by the receiver. Used for QoS 1 and QoS 2
/// retransmission and deduplication logic.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutInflightMessage {
    /// The publish message content.
    pub publish: Publish,
    /// The source of the message (client, broker, or bridge).
    pub from: From,
    /// Current delivery acknowledgment status.
    pub status: MomentStatus,
    /// Last status update timestamp (milliseconds since epoch).
    pub update_time: TimestampMillis,
}

impl OutInflightMessage {
    #[inline]
    pub fn new(status: MomentStatus, from: From, publish: Publish) -> Self {
        Self { publish, from, status, update_time: timestamp_millis() }
    }

    #[inline]
    fn update_status(&mut self, status: MomentStatus) {
        self.update_time = timestamp_millis();
        self.status = status;
    }

    #[inline]
    pub fn timeout(&self, interval_millis: TimestampMillis) -> bool {
        log::debug!("interval_millis:{} {}", interval_millis, timestamp_millis() - self.update_time);
        interval_millis > 0 && ((timestamp_millis() - self.update_time) >= interval_millis)
    }
}

/// Manages outbound inflight messages for QoS 1 and QoS 2 delivery.
///
/// Provides packet ID generation, retransmission tracking via
/// dual-interval timing (retry/expiry), and capacity-based flow control.
///
/// # Packet ID Management
///
/// Packet IDs are generated atomically via `AtomicU16` starting from 1,
/// wrapping around when necessary. The system probes up to `u16::MAX`
/// attempts before reporting exhaustion.
///
/// # Timeout Behavior
///
/// The effective check interval is `min(retry_interval, expiry_interval)`.
/// When a timeout fires, [`pop_front_timeout`](OutInflight::pop_front_timeout)
/// returns the expired message for retransmission or cleanup.
#[derive(Clone)]
pub struct OutInflight {
    cap: usize,
    interval: TimestampMillis,
    next: Arc<AtomicU16>,
    queues: OutQueues,
    on_push_fn: Option<Arc<dyn OnEventFn>>,
    on_pop_fn: Option<Arc<dyn OnEventFn>>,
}

impl OutInflight {
    /// Create a new outbound inflight tracker.
    ///
    /// # Arguments
    ///
    /// * `cap` — Maximum concurrent outbound messages.
    /// * `retry_interval` — Delay before retransmitting unacknowledged messages (ms).
    /// * `expiry_interval` — Time after which messages expire (ms).
    #[inline]
    pub fn new(cap: usize, retry_interval: TimestampMillis, expiry_interval: TimestampMillis) -> Self {
        let interval = Self::interval(retry_interval, expiry_interval);
        Self {
            cap,
            interval,
            next: Arc::new(AtomicU16::new(1)),
            queues: OutQueues::default(),
            on_push_fn: None,
            on_pop_fn: None,
        }
    }

    /// Register a callback invoked when a message is pushed to the inflight queue.
    #[inline]
    pub fn on_push<F>(mut self, f: F) -> Self
    where
        F: OnEventFn,
    {
        self.on_push_fn = Some(Arc::new(f));
        self
    }

    /// Register a callback invoked when a message is popped from the inflight queue.
    #[inline]
    pub fn on_pop<F>(mut self, f: F) -> Self
    where
        F: OnEventFn,
    {
        self.on_pop_fn = Some(Arc::new(f));
        self
    }

    #[inline]
    fn interval(retry_interval: TimestampMillis, expiry_interval: TimestampMillis) -> TimestampMillis {
        match (retry_interval, expiry_interval) {
            (0, 0) => 0,
            (0, expiry_interval) => expiry_interval,
            (retry_interval, 0) => retry_interval,
            (retry_interval, expiry_interval) => retry_interval.min(expiry_interval),
        }
    }

    /// Compute the timeout duration until the next inflight message expires.
    ///
    /// Returns `None` if no messages are queued or if intervals are zero.
    #[inline]
    pub fn get_timeout(&self) -> Option<Duration> {
        if self.interval == 0 {
            return None;
        }
        if let Some((_, m)) = self.queues.front() {
            let mut t = self.interval - (timestamp_millis() - m.update_time);
            if t < 1 {
                t = 1;
            }
            log::debug!("get timeout t: {t}");
            return Some(Duration::from_millis(t as u64));
        }
        None
    }

    #[inline]
    fn front_timeout(&self) -> bool {
        if self.interval == 0 {
            return false;
        }
        if let Some((_, m)) = self.queues.front() {
            if m.timeout(self.interval) {
                return true;
            }
        }
        false
    }

    #[inline]
    pub fn get(&self, packet_id: PacketId) -> Option<&OutInflightMessage> {
        self.queues.get(&packet_id)
    }

    #[inline]
    pub fn front(&self) -> Option<(&PacketId, &OutInflightMessage)> {
        self.queues.front()
    }

    #[inline]
    pub fn pop_front(&mut self) -> Option<OutInflightMessage> {
        if let Some(msg) = self.queues.pop_front().map(|(_, m)| m) {
            if let Some(f) = self.on_pop_fn.as_ref() {
                f();
            }
            Some(msg)
        } else {
            None
        }
    }

    #[inline]
    pub fn pop_front_timeout(&mut self) -> Option<OutInflightMessage> {
        if self.front_timeout() {
            self.pop_front()
        } else {
            None
        }
    }

    #[inline]
    pub fn push_back(&mut self, m: OutInflightMessage) -> Option<NonZeroU16> {
        if let Some(packet_id) = m.publish.packet_id {
            if let Some(f) = self.on_push_fn.as_ref() {
                f();
            }
            let old = self.queues.insert(packet_id.get(), m);
            if old.is_some() {
                if let Some(f) = self.on_pop_fn.as_ref() {
                    f();
                }
            }
            old.and_then(|old| old.publish.packet_id)
        } else {
            log::warn!("packet_id is None, inflight message: {m:?}");
            None
        }
    }

    #[inline]
    pub fn remove(&mut self, packet_id: &PacketId) -> Option<OutInflightMessage> {
        if let Some(msg) = self.queues.remove(packet_id) {
            if let Some(f) = self.on_pop_fn.as_ref() {
                f();
            }
            Some(msg)
        } else {
            None
        }
    }

    #[inline]
    pub fn update_status(&mut self, packet_id: &PacketId, s: MomentStatus) {
        if let Some(m) = self.queues.get_mut(packet_id) {
            m.update_status(s);
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.queues.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.queues.is_empty()
    }

    #[inline]
    pub fn exist(&self, packet_id: &PacketId) -> bool {
        self.queues.contains_key(packet_id)
    }

    #[inline]
    pub fn has_credit(&self) -> bool {
        (self.cap - self.queues.len()) > 0
    }

    #[inline]
    pub fn next_id(&self) -> Result<PacketId> {
        for _ in 0..u16::MAX {
            let packet_id = self.next.fetch_add(1, Ordering::SeqCst);
            if packet_id == 0 {
                continue;
            }
            if !self.queues.contains_key(&packet_id) {
                return Ok(packet_id);
            }
        }
        Err(anyhow!("no packet_id available, should unreachable!()"))
    }

    /// Advance the packet-id allocator so that ids already reserved by
    /// transferred inflight messages (kept under their old ids) are never
    /// re-issued to concurrently delivered messages. `next_id` only checks
    /// the current queue, so without this isolation a stored message
    /// delivered during session resume can be assigned the same id as a
    /// transferred message and `push_back` would silently overwrite it.
    #[inline]
    pub fn advance_next_id(&self, max_reserved: u16) {
        self.next.fetch_max(max_reserved.saturating_add(1), Ordering::SeqCst);
    }

    #[inline]
    pub fn to_inflight_messages(&mut self) -> Vec<OutInflightMessage> {
        let mut inflight_messages = Vec::new();
        while let Some(msg) = self.pop_front() {
            //@TODO ..., check message expired
            inflight_messages.push(msg);
        }
        inflight_messages
    }

    #[inline]
    pub fn clone_inflight_messages(&mut self) -> Vec<OutInflightMessage> {
        self.queues.iter().map(|(_, msg)| msg.clone()).collect_vec()
    }
}

/// Tracks inbound inflight messages for QoS 2 deduplication.
///
///
/// Maintains a set of packet IDs received from the client to detect
/// duplicate PUBLISH packets (a requirement of the MQTT QoS 2 protocol).
///
/// # Flow Control
///
/// Enforces a `max_inflight` window to prevent the client from
/// overwhelming the broker with concurrent QoS 2 publishes.
///
/// Size limit for concurrent inbound messages on a single connection.
/// TODO: Make this configurable per listener/client.
pub struct InInflight {
    cached: BTreeSet<NonZeroU16>,
    #[allow(dead_code)]
    scx: ServerContext,
    max_inflight: u16,
}

impl Drop for InInflight {
    fn drop(&mut self) {
        #[cfg(feature = "stats")]
        self.scx.stats.in_inflights.decs(self.cached.len() as isize);
    }
}

impl InInflight {
    pub(crate) fn new(scx: ServerContext, max_inflight: u16) -> Self {
        Self { cached: BTreeSet::default(), scx, max_inflight }
    }

    #[inline]
    pub(crate) fn add(&mut self, pid: NonZeroU16, qos: QoS) -> std::result::Result<bool, Reason> {
        if self.cached.len() >= self.max_inflight as usize {
            return Err(Reason::InflightWindowFull);
        }
        if self.cached.insert(pid) {
            #[cfg(feature = "stats")]
            self.scx.stats.in_inflights.inc();
            Ok(true)
        } else if matches!(qos, QoS::ExactlyOnce) {
            Err(MqttError::PacketIdInUse(pid).into())
        } else {
            Ok(false)
        }
    }

    /// Check whether a packet id is already tracked in the inbound inflight set.
    ///
    /// Used by the QoS 2 duplicate detection path ([MQTT-4.3.3-10]): when a
    /// PUBLISH with the same Packet Identifier arrives before the exchange is
    /// complete, the broker must answer PUBREC without delivering again.
    #[inline]
    pub(crate) fn exist(&self, pid: &NonZeroU16) -> bool {
        self.cached.contains(pid)
    }

    #[inline]
    pub(crate) fn remove(&mut self, pid: &NonZeroU16) -> bool {
        #[allow(clippy::needless_bool)]
        if self.cached.remove(pid) {
            #[cfg(feature = "stats")]
            self.scx.stats.in_inflights.dec();
            true
        } else {
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use bytestring::ByteString;

    use crate::types::{CodecPublish, Id};

    fn make_publish(topic: &str, packet_id: u16) -> Publish {
        Publish {
            inner: Box::new(CodecPublish {
                dup: false,
                retain: false,
                qos: QoS::ExactlyOnce,
                topic: ByteString::from(topic),
                packet_id: NonZeroU16::new(packet_id),
                properties: None,
                payload: Bytes::from_static(b"payload"),
            }),
            target_clientid: None,
            delay_interval: None,
            create_time: None,
        }
    }

    fn make_from(client: &str) -> From {
        From::from_custom(Id::new(1, 1, None, None, ByteString::from(client), None))
    }

    fn make_msg(topic: &str, pid: u16, status: MomentStatus) -> OutInflightMessage {
        OutInflightMessage::new(status, make_from(topic), make_publish(topic, pid))
    }

    /// Root cause 1: the packet-id allocator restarts at 1 and is not
    /// isolated from the id space of transferred inflight messages
    /// (which keep their old ids 1..N and are registered later by
    /// `send_rerelease`). `next_id` only checks the current queue, so a
    /// concurrently delivered stored message can be assigned the same id.
    #[test]
    fn next_id_restarts_at_one_and_collides_with_transfer_ids() {
        let inflight = OutInflight::new(100, 0, 0);
        // The transferred messages (old ids 1..=3) are still queued behind
        // `SendRerelease`; the new session's queue is empty, so the allocator
        // hands out id 1 again — the exact overlap that causes the collision.
        assert_eq!(inflight.next_id().unwrap(), 1, "allocator is not isolated from the transferred id space");
    }

    /// Root cause 2: `push_back` silently overwrites an existing entry with
    /// the same packet-id (`HashMap::insert`), returning the overwritten id.
    /// A second registration therefore destroys the first message's QoS 2 state.
    #[test]
    fn push_back_same_packet_id_silently_overwrites() {
        let mut inflight = OutInflight::new(100, 0, 0);
        inflight.push_back(make_msg("stored/a", 1, MomentStatus::UnReceived));

        // Second registration with the same id (send_rerelease path).
        let old = inflight.push_back(make_msg("inflight/b", 1, MomentStatus::UnComplete));
        assert_eq!(old.map(|id| id.get()), Some(1), "push_back reported the overwrite");

        let cur = inflight.get(1).expect("packet 1 is still tracked");
        assert_eq!(cur.publish.inner.topic, ByteString::from_static("inflight/b"));
        assert_eq!(cur.status, MomentStatus::UnComplete);
    }

    /// Fix verification: after `advance_next_id`, new allocations skip the
    /// transferred id range (1..=N), so a concurrently delivered stored
    /// message can never collide with a transferred message's old id.
    #[test]
    fn advance_next_id_isolates_transfer_id_space() {
        let inflight = OutInflight::new(100, 0, 0);
        inflight.advance_next_id(5); // transferred messages keep ids 1..=5
        assert_eq!(inflight.next_id().unwrap(), 6, "allocator must skip the transferred id range");
    }

    /// End-to-end mechanism reproduction: during session resume, a stored
    /// message is first delivered (id 1), then `send_rerelease` registers a
    /// transferred message under the same old id 1 and overwrites it. The
    /// stored message's QoS 2 state is permanently lost — the PUBCOMP that
    /// arrives later completes the *other* message and the stored one has no
    /// record at all (no resend, no ack hook).
    #[test]
    fn resume_collision_loses_stored_message() {
        let mut inflight = OutInflight::new(100, 0, 0);

        // (1) stored message M1 delivered by send_storaged_messages: next_id=1
        let pid = NonZeroU16::new(inflight.next_id().unwrap()).unwrap();
        assert_eq!(pid.get(), 1);
        inflight.push_back(make_msg("stored/M1", pid.get(), MomentStatus::UnReceived));

        // (2) transferred message M2 (old id 1) registered by send_rerelease → overwrites M1
        let old = inflight.push_back(make_msg("inflight/M2", pid.get(), MomentStatus::UnComplete));
        assert_eq!(
            old.map(|id| id.get()),
            Some(1),
            "send_rerelease overwrote the stored message (bug reproduced)"
        );

        // (3) M1's PUBREC/PUBCOMP removes M2 — M1 is left with no tracking at all
        let removed = inflight.remove(&pid.get()).expect("something is tracked under id 1");
        assert_eq!(removed.publish.inner.topic, ByteString::from_static("inflight/M2"));
        assert!(
            inflight.get(pid.get()).is_none(),
            "M1's QoS 2 state is gone: no resend, no ack hook, message effectively lost"
        );
    }
}