mqtt5 0.31.2

Complete MQTT v5.0 platform with high-performance async client and full-featured broker supporting TCP, TLS, WebSocket, authentication, bridging, and resource monitoring
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
use crate::error::{MqttError, Result};
use crate::transport::client_config::StreamStrategy;
use crate::transport::flow::{DataFlowHeader, FlowFlags, FlowId, FlowIdGenerator};
use crate::transport::packet_io::encode_packet_to_buffer;
use bytes::BytesMut;
use mqtt5_protocol::packet::Packet;
use quinn::{Connection, SendStream};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tracing::{debug, instrument, trace, warn};

struct StreamInfo {
    stream: SendStream,
    flow_id: FlowId,
    last_used: Instant,
}

struct FlowStreamInfo {
    stream: SendStream,
    last_used: Instant,
}

const DEFAULT_STREAM_IDLE_TIMEOUT: Duration = Duration::from_secs(300);
const DEFAULT_FLOW_EXPIRE_INTERVAL: u64 = 300;

pub struct QuicStreamManager {
    connection: Arc<Connection>,
    strategy: StreamStrategy,
    topic_streams: Arc<Mutex<HashMap<String, StreamInfo>>>,
    flow_streams: Arc<Mutex<HashMap<FlowId, FlowStreamInfo>>>,
    max_cached_streams: usize,
    stream_idle_timeout: Duration,
    flow_id_generator: Arc<Mutex<FlowIdGenerator>>,
    flow_expire_interval: u64,
    flow_flags: FlowFlags,
    enable_flow_headers: bool,
}

impl QuicStreamManager {
    #[must_use]
    pub fn new(connection: Arc<Connection>, strategy: StreamStrategy) -> Self {
        Self {
            connection,
            strategy,
            topic_streams: Arc::new(Mutex::new(HashMap::new())),
            flow_streams: Arc::new(Mutex::new(HashMap::new())),
            max_cached_streams: 100,
            stream_idle_timeout: DEFAULT_STREAM_IDLE_TIMEOUT,
            flow_id_generator: Arc::new(Mutex::new(FlowIdGenerator::new())),
            flow_expire_interval: DEFAULT_FLOW_EXPIRE_INTERVAL,
            flow_flags: FlowFlags::default(),
            enable_flow_headers: false,
        }
    }

    #[must_use]
    pub fn with_flow_headers(mut self, enable: bool) -> Self {
        self.enable_flow_headers = enable;
        self
    }

    #[must_use]
    pub fn with_flow_expire_interval(mut self, seconds: u64) -> Self {
        self.flow_expire_interval = seconds;
        self
    }

    #[must_use]
    pub fn with_flow_flags(mut self, flags: FlowFlags) -> Self {
        self.flow_flags = flags;
        self
    }

    /// # Errors
    /// Returns an error if the QUIC connection fails to open a stream.
    #[instrument(skip(self), level = "debug")]
    pub async fn open_data_stream(&self) -> Result<quinn::SendStream> {
        self.connection
            .open_uni()
            .await
            .map_err(|e| MqttError::ConnectionError(format!("Failed to open QUIC stream: {e}")))
    }

    /// # Errors
    /// Returns an error if the stream cannot be opened or the flow header write fails.
    #[instrument(skip(self), fields(strategy = ?self.strategy), level = "debug")]
    pub async fn open_data_stream_with_flow(&self) -> Result<(quinn::SendStream, FlowId)> {
        let mut send = self.open_data_stream().await?;

        let flow_id = if self.enable_flow_headers {
            let flow_id = {
                let mut gen = self.flow_id_generator.lock().await;
                gen.next_client()
            };

            let mut buf = BytesMut::with_capacity(32);
            let header =
                DataFlowHeader::client(flow_id, self.flow_expire_interval, self.flow_flags);
            header.encode(&mut buf);

            send.write_all(&buf).await.map_err(|e| {
                MqttError::ConnectionError(format!("Failed to write flow header: {e}"))
            })?;

            debug!(flow_id = ?flow_id, "Wrote client data flow header on new stream");
            flow_id
        } else {
            FlowId::client(0)
        };

        Ok((send, flow_id))
    }

    /// # Errors
    /// Returns an error if the stream cannot be opened or the recovery header write fails.
    pub async fn open_recovery_stream(
        &self,
        flow_id: FlowId,
        recovery_flags: FlowFlags,
    ) -> Result<quinn::SendStream> {
        let mut send = self.open_data_stream().await?;

        if self.enable_flow_headers {
            let mut buf = BytesMut::with_capacity(32);
            let header = DataFlowHeader::client(flow_id, self.flow_expire_interval, recovery_flags);
            header.encode(&mut buf);

            send.write_all(&buf).await.map_err(|e| {
                MqttError::ConnectionError(format!("Failed to write recovery flow header: {e}"))
            })?;

            debug!(
                flow_id = ?flow_id,
                ?recovery_flags,
                "Wrote recovery flow header on stream"
            );
        }

        Ok(send)
    }

    pub fn set_recovery_mode(&mut self, enable: bool) {
        self.flow_flags.clean = u8::from(!enable);
    }

    #[must_use]
    pub fn current_flow_flags(&self) -> FlowFlags {
        self.flow_flags
    }

    /// Sends a packet on a dedicated QUIC stream.
    ///
    /// # Errors
    /// Returns an error if the stream operation or packet encoding fails.
    #[instrument(skip(self, packet), level = "debug")]
    pub async fn send_packet_on_stream(&self, packet: Packet) -> Result<()> {
        let (mut send, flow_id) = self.open_data_stream_with_flow().await?;

        let mut buf = BytesMut::with_capacity(1024);
        encode_packet_to_buffer(&packet, &mut buf)?;

        send.write_all(&buf)
            .await
            .map_err(|e| MqttError::ConnectionError(format!("QUIC write error: {e}")))?;

        send.finish()
            .map_err(|e| MqttError::ConnectionError(format!("QUIC stream finish error: {e}")))?;

        // Yield to allow QUIC I/O driver to transmit stream frames before returning.
        // Without this, rapid sequential publishes can queue many streams faster than
        // the I/O driver can transmit them, and a subsequent disconnect() may close
        // the connection before all frames are sent.
        tokio::task::yield_now().await;

        debug!(flow_id = ?flow_id, "Sent packet on dedicated QUIC stream");

        Ok(())
    }

    #[must_use]
    pub fn strategy(&self) -> StreamStrategy {
        self.strategy
    }

    #[must_use]
    pub fn flow_headers_enabled(&self) -> bool {
        self.enable_flow_headers
    }

    // [MQoQ§5.3] Per-topic stream caching
    async fn get_or_create_topic_stream(&self, topic: &str) -> Result<(SendStream, FlowId)> {
        let mut streams = self.topic_streams.lock().await;
        let now = Instant::now();

        let idle_topics: Vec<String> = streams
            .iter()
            .filter(|(_, info)| now.duration_since(info.last_used) > self.stream_idle_timeout)
            .map(|(topic, _)| topic.clone())
            .collect();

        for idle_topic in &idle_topics {
            if let Some(mut info) = streams.remove(idle_topic) {
                let _ = info.stream.finish();
                debug!(topic = %idle_topic, flow_id = ?info.flow_id, "Closed idle stream");
            }
        }

        if let Some(info) = streams.remove(topic) {
            trace!(topic = %topic, flow_id = ?info.flow_id, "Reusing existing stream for topic");
            return Ok((info.stream, info.flow_id));
        }

        if streams.len() >= self.max_cached_streams {
            let oldest = streams
                .iter()
                .min_by_key(|(_, info)| info.last_used)
                .map(|(k, _)| k.clone());

            if let Some(oldest_topic) = oldest {
                if let Some(mut info) = streams.remove(&oldest_topic) {
                    let _ = info.stream.finish();
                    debug!(
                        topic = %oldest_topic,
                        flow_id = ?info.flow_id,
                        "Evicted oldest stream from cache (LRU)"
                    );
                }
            }
        }
        drop(streams);

        debug!(topic = %topic, "Opening new stream for topic");
        let mut send = self.connection.open_uni().await.map_err(|e| {
            MqttError::ConnectionError(format!("Failed to open QUIC stream for topic: {e}"))
        })?;

        let flow_id = if self.enable_flow_headers {
            let flow_id = {
                let mut gen = self.flow_id_generator.lock().await;
                gen.next_client()
            };

            let mut buf = BytesMut::with_capacity(32);
            let header =
                DataFlowHeader::client(flow_id, self.flow_expire_interval, self.flow_flags);
            header.encode(&mut buf);

            send.write_all(&buf).await.map_err(|e| {
                MqttError::ConnectionError(format!("Failed to write flow header: {e}"))
            })?;

            debug!(topic = %topic, flow_id = ?flow_id, "Wrote flow header for new topic stream");
            flow_id
        } else {
            FlowId::client(0)
        };

        Ok((send, flow_id))
    }

    /// Sends a packet on a topic-specific stream.
    ///
    /// # Errors
    /// Returns an error if the stream operation or packet encoding fails.
    pub async fn send_on_topic_stream(&self, topic: String, packet: Packet) -> Result<()> {
        let (mut stream, flow_id) = self.get_or_create_topic_stream(&topic).await?;

        let mut buf = BytesMut::with_capacity(1024);
        encode_packet_to_buffer(&packet, &mut buf)?;

        stream
            .write_all(&buf)
            .await
            .map_err(|e| MqttError::ConnectionError(format!("QUIC write error: {e}")))?;

        debug!(topic = %topic, flow_id = ?flow_id, "Sent packet on topic-specific stream");

        self.topic_streams.lock().await.insert(
            topic,
            StreamInfo {
                stream,
                flow_id,
                last_used: Instant::now(),
            },
        );

        Ok(())
    }

    pub async fn get_flow_id_for_topic(&self, topic: &str) -> Option<FlowId> {
        let streams = self.topic_streams.lock().await;
        streams.get(topic).map(|info| info.flow_id)
    }

    #[instrument(skip(self, stream), level = "debug")]
    pub async fn register_flow_stream(&self, flow_id: FlowId, stream: SendStream) {
        let mut flows = self.flow_streams.lock().await;
        flows.insert(
            flow_id,
            FlowStreamInfo {
                stream,
                last_used: Instant::now(),
            },
        );
        debug!(flow_id = ?flow_id, "Registered flow stream");
    }

    /// Sends a packet on an existing flow stream.
    ///
    /// # Errors
    /// Returns an error if the flow stream is not found or the write operation fails.
    #[instrument(skip(self, packet), level = "debug")]
    pub async fn send_on_flow(&self, flow_id: FlowId, packet: Packet) -> Result<()> {
        let mut flows = self.flow_streams.lock().await;

        if let Some(info) = flows.get_mut(&flow_id) {
            let mut buf = BytesMut::with_capacity(1024);
            encode_packet_to_buffer(&packet, &mut buf)?;

            info.stream
                .write_all(&buf)
                .await
                .map_err(|e| MqttError::ConnectionError(format!("QUIC write error: {e}")))?;
            info.last_used = Instant::now();

            debug!(flow_id = ?flow_id, "Sent packet on flow stream");
            Ok(())
        } else {
            drop(flows);

            let (mut send, new_flow_id) = self.open_data_stream_with_flow().await?;

            let mut buf = BytesMut::with_capacity(1024);
            encode_packet_to_buffer(&packet, &mut buf)?;

            send.write_all(&buf)
                .await
                .map_err(|e| MqttError::ConnectionError(format!("QUIC write error: {e}")))?;

            self.flow_streams.lock().await.insert(
                new_flow_id,
                FlowStreamInfo {
                    stream: send,
                    last_used: Instant::now(),
                },
            );

            debug!(
                requested_flow_id = ?flow_id,
                actual_flow_id = ?new_flow_id,
                "Flow not found, opened new stream"
            );
            Ok(())
        }
    }

    pub async fn has_flow_stream(&self, flow_id: FlowId) -> bool {
        self.flow_streams.lock().await.contains_key(&flow_id)
    }

    pub async fn remove_flow_stream(&self, flow_id: FlowId) -> bool {
        let mut flows = self.flow_streams.lock().await;
        if let Some(mut info) = flows.remove(&flow_id) {
            let _ = info.stream.finish();
            debug!(flow_id = ?flow_id, "Removed flow stream");
            true
        } else {
            false
        }
    }

    /// # Errors
    /// Returns an error if flow headers are not enabled, the bi stream cannot be opened,
    /// or the peer does not respond within 2 seconds.
    #[instrument(skip(self), level = "debug")]
    pub async fn discard_flow(&self, flow_id: FlowId) -> Result<()> {
        if !self.enable_flow_headers {
            return Err(MqttError::ProtocolError(
                "flow headers not enabled, cannot discard flow".into(),
            ));
        }

        let (mut send, mut recv) = self.connection.open_bi().await.map_err(|e| {
            MqttError::ConnectionError(format!("failed to open bi stream for discard: {e}"))
        })?;

        let discard_flags = FlowFlags::discard();
        let header = DataFlowHeader::client(flow_id, 0, discard_flags);
        let mut buf = BytesMut::with_capacity(32);
        header.encode(&mut buf);

        send.write_all(&buf).await.map_err(|e| {
            MqttError::ConnectionError(format!("failed to write discard flow header: {e}"))
        })?;

        send.finish().map_err(|e| {
            MqttError::ConnectionError(format!("failed to finish discard send stream: {e}"))
        })?;

        let read_result = tokio::time::timeout(Duration::from_secs(2), recv.read_to_end(64)).await;

        match read_result {
            Ok(Ok(data)) => {
                if !data.is_empty() {
                    warn!(
                        flow_id = ?flow_id,
                        len = data.len(),
                        "peer returned non-empty data on discard response"
                    );
                }
            }
            Ok(Err(e)) => {
                return Err(MqttError::ConnectionError(format!(
                    "failed to read discard response: {e}"
                )));
            }
            Err(_) => {
                return Err(MqttError::Timeout);
            }
        }

        self.flow_streams.lock().await.remove(&flow_id);

        let mut topics = self.topic_streams.lock().await;
        topics.retain(|_, info| info.flow_id != flow_id);

        debug!(flow_id = ?flow_id, "discarded flow at peer and cleaned up local state");

        Ok(())
    }

    pub async fn close_all_streams(&self) {
        let mut streams = self.topic_streams.lock().await;
        for (topic, mut info) in streams.drain() {
            let _ = info.stream.finish();
            trace!(topic = %topic, flow_id = ?info.flow_id, "Closed topic stream");
        }
        drop(streams);

        let mut flows = self.flow_streams.lock().await;
        for (flow_id, mut info) in flows.drain() {
            let _ = info.stream.finish();
            trace!(flow_id = ?flow_id, "Closed flow stream");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stream_manager_creation() {
        let strategy = StreamStrategy::ControlOnly;
        assert_eq!(strategy, StreamStrategy::default());
    }

    #[test]
    #[allow(deprecated)]
    fn test_stream_strategy_variants() {
        assert_eq!(StreamStrategy::ControlOnly, StreamStrategy::default());
        assert_ne!(StreamStrategy::DataPerPublish, StreamStrategy::ControlOnly);
        assert_ne!(StreamStrategy::DataPerTopic, StreamStrategy::ControlOnly);
        assert_ne!(
            StreamStrategy::DataPerSubscription,
            StreamStrategy::ControlOnly
        );
    }

    #[test]
    fn test_flow_flags_config() {
        let flags = FlowFlags {
            clean: 1,
            abort_if_no_state: 0,
            err_tolerance: 1,
            persistent_qos: 1,
            persistent_topic_alias: 0,
            persistent_subscriptions: 1,
            optional_headers: 0,
        };

        assert_eq!(flags.clean, 1);
        assert_eq!(flags.abort_if_no_state, 0);
        assert_eq!(flags.err_tolerance, 1);
        assert_eq!(flags.persistent_qos, 1);
        assert_eq!(flags.persistent_topic_alias, 0);
        assert_eq!(flags.persistent_subscriptions, 1);
        assert_eq!(flags.optional_headers, 0);
    }

    #[test]
    fn test_default_flow_expire_interval() {
        assert_eq!(DEFAULT_FLOW_EXPIRE_INTERVAL, 300);
    }

    #[test]
    fn test_flow_id_generator_sequence() {
        let mut gen = FlowIdGenerator::new();
        let id1 = gen.next_client();
        let id2 = gen.next_client();
        let id3 = gen.next_client();

        assert!(id1.is_client_initiated());
        assert!(id2.is_client_initiated());
        assert!(id3.is_client_initiated());

        assert_eq!(id1.sequence(), 1);
        assert_eq!(id2.sequence(), 2);
        assert_eq!(id3.sequence(), 3);
    }
}