mq-bridge 0.2.15

An asynchronous message bridging library connecting Kafka, MQTT, AMQP, NATS, MongoDB, HTTP, and more.
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
//  mq-bridge
//  © Copyright 2026, by Marco Mengelkoch
//  Licensed under MIT License, see License file for more details
//  git clone https://github.com/marcomq/mq-bridge

use crate::canonical_message::tracing_support::LazyMessageIds;
use crate::models::SledConfig;
use crate::traits::{
    ConsumerError, EndpointStatus, MessageConsumer, MessageDisposition, MessagePublisher,
    PublisherError, Received, ReceivedBatch, Sent, SentBatch,
};
use crate::CanonicalMessage;
use anyhow::{anyhow, Context};
use async_trait::async_trait;
use once_cell::sync::Lazy;
use sled::Transactional;
use sled::{Db, IVec, Tree};
use std::any::Any;
use std::collections::HashMap;
use std::ops::Bound;
use std::sync::Mutex;
use std::time::Duration;
use tracing::trace;

pub struct SledPublisher {
    db: Db,
    tree: Tree,
}

static SLED_DBS: Lazy<Mutex<HashMap<String, Db>>> = Lazy::new(|| Mutex::new(HashMap::new()));

fn get_or_open_db(path: &str) -> anyhow::Result<Db> {
    let mut dbs = SLED_DBS
        .lock()
        .map_err(|_| anyhow!("Sled DB registry lock poisoned"))?;
    if let Some(db) = dbs.get(path) {
        return Ok(db.clone());
    }
    let db = sled::open(path)?;
    dbs.insert(path.to_string(), db.clone());
    Ok(db)
}

pub fn close_db(path: &str) -> anyhow::Result<()> {
    let mut dbs = SLED_DBS
        .lock()
        .map_err(|_| anyhow!("Sled DB registry lock poisoned"))?;
    if let Some(db) = dbs.remove(path) {
        db.flush()?;
    }
    Ok(())
}

impl SledPublisher {
    pub fn new(config: &SledConfig) -> anyhow::Result<Self> {
        let db = get_or_open_db(&config.path).context("Failed to open Sled DB")?;
        let tree_name = config.tree.as_deref().unwrap_or("default");
        let tree = db
            .open_tree(tree_name)
            .context("Failed to open Sled tree")?;
        Ok(Self { db, tree })
    }
}

#[async_trait]
impl MessagePublisher for SledPublisher {
    async fn send(&self, message: CanonicalMessage) -> Result<Sent, PublisherError> {
        let id = self
            .db
            .generate_id()
            .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;
        let key = id.to_be_bytes();
        let value =
            serde_json::to_vec(&message).map_err(|e| PublisherError::NonRetryable(anyhow!(e)))?;

        self.tree
            .insert(key, value)
            .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;

        self.tree
            .flush_async()
            .await
            .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;
        Ok(Sent::Ack)
    }

    async fn send_batch(
        &self,
        messages: Vec<CanonicalMessage>,
    ) -> Result<SentBatch, PublisherError> {
        trace!(count = messages.len(), message_ids = ?LazyMessageIds(&messages), "Publishing batch to Sled");
        let mut batch = sled::Batch::default();
        for message in messages {
            let id = self
                .db
                .generate_id()
                .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;
            let key = id.to_be_bytes();
            let value = serde_json::to_vec(&message)
                .map_err(|e| PublisherError::NonRetryable(anyhow!(e)))?;
            batch.insert(&key, value);
        }
        self.tree
            .apply_batch(batch)
            .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;
        self.tree
            .flush_async()
            .await
            .map_err(|e| PublisherError::Retryable(anyhow!(e)))?;

        Ok(SentBatch::Ack)
    }

    async fn status(&self) -> EndpointStatus {
        let (healthy, error) = match self.tree.first() {
            Ok(_) => (true, None),
            Err(e) => (false, Some(format!("Sled error: {}", e))),
        };
        EndpointStatus {
            healthy,
            target: String::from_utf8_lossy(&self.tree.name()).to_string(),
            error,
            ..Default::default()
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

pub struct SledConsumer {
    _db: Db,
    tree: Tree,
    inflight_tree: Tree,
    notify_rx: async_channel::Receiver<()>,
    delete_after_read: bool,
    last_key: Option<IVec>,
}

impl SledConsumer {
    pub fn new(config: &SledConfig) -> anyhow::Result<Self> {
        let db = get_or_open_db(&config.path).context("Failed to open Sled DB")?;
        let tree_name = config.tree.as_deref().unwrap_or("default");
        let tree = db
            .open_tree(tree_name)
            .context("Failed to open Sled tree")?;
        let inflight_tree = db
            .open_tree(format!("{}_inflight", tree_name))
            .context("Failed to open Sled inflight tree")?;

        if config.delete_after_read && !inflight_tree.is_empty() {
            let inflight_items: Vec<_> = inflight_tree.iter().collect::<Result<_, _>>()?;
            if !inflight_items.is_empty() {
                tracing::info!(
                    "Found {} inflight messages from previous Sled session, attempting recovery...",
                    inflight_items.len()
                );
                let (tree_ref, inflight_tree_ref) = (&tree, &inflight_tree);
                let tx_result: Result<(), _> =
                    (tree_ref, inflight_tree_ref).transaction(|(t_main, t_inflight)| {
                        for (key, value) in &inflight_items {
                            t_main.insert(key, value)?;
                            t_inflight.remove(key)?;
                        }
                        Ok::<(), sled::transaction::ConflictableTransactionError<()>>(())
                    });
                if let Err(e) = tx_result {
                    return Err(anyhow!("Failed to recover inflight Sled messages: {:?}", e));
                }
            }
        }

        let subscriber = tree.watch_prefix(vec![]);
        let (tx, rx) = async_channel::bounded(1);

        std::thread::spawn(move || {
            let mut subscriber = subscriber;
            loop {
                match subscriber.next_timeout(Duration::from_millis(100)) {
                    Ok(_event) => {
                        if tx.send_blocking(()).is_err() {
                            break;
                        }
                    }
                    Err(_) => {
                        if tx.is_closed() {
                            break;
                        }
                    }
                }
                if tx.is_closed() {
                    break;
                }
            }
        });

        let last_key = if !config.read_from_start {
            tree.last().map_err(|e| anyhow!(e))?.map(|(k, _)| k)
        } else {
            None
        };

        Ok(Self {
            _db: db,
            tree,
            inflight_tree,
            notify_rx: rx,
            delete_after_read: config.delete_after_read,
            last_key,
        })
    }
}

#[async_trait]
impl MessageConsumer for SledConsumer {
    async fn receive(&mut self) -> Result<Received, ConsumerError> {
        loop {
            let next_item = if self.delete_after_read {
                // Queue mode: Move from main tree to inflight tree atomically
                loop {
                    if let Some((k, v)) = self
                        .tree
                        .first()
                        .map_err(|e| ConsumerError::Connection(anyhow!(e)))?
                    {
                        let k_clone = k.clone();
                        let v_clone = v.clone();
                        let (tree, inflight) = (&self.tree, &self.inflight_tree);
                        let tx_result: std::result::Result<
                            Option<(IVec, IVec)>,
                            sled::transaction::TransactionError<()>,
                        > = (tree, inflight).transaction(|(t_main, t_inflight)| {
                            if t_main.get(&k_clone)?.is_some() {
                                t_main.remove(&k_clone)?;
                                t_inflight.insert(&k_clone, &v_clone)?;
                                Ok(Some((k_clone.clone(), v_clone.clone())))
                            } else {
                                Ok(None)
                            }
                        });
                        match tx_result {
                            Ok(Some(item)) => break Some(item),
                            Ok(None) => continue,
                            Err(e) => return Err(ConsumerError::Connection(anyhow!("{:?}", e))),
                        }
                    } else {
                        break None;
                    }
                }
            } else {
                // Topic mode: Scan forward
                let start = if let Some(k) = &self.last_key {
                    Bound::Excluded(k)
                } else {
                    Bound::Unbounded
                };
                self.tree
                    .range::<&IVec, _>((start, Bound::Unbounded))
                    .next()
                    .transpose()
                    .map_err(|e| ConsumerError::Connection(anyhow!(e)))?
            };

            if let Some((key, value)) = next_item {
                self.last_key = Some(key.clone());
                let message = serde_json::from_slice(&value)
                    .map_err(|e| ConsumerError::Connection(anyhow!(e)))?;

                let tree = self.tree.clone();
                let inflight_tree = self.inflight_tree.clone();
                let delete = self.delete_after_read;
                let key_clone = key.clone();
                let value_clone = value.to_vec();

                let commit = Box::new(move |disposition: MessageDisposition| {
                    Box::pin(async move {
                        if delete {
                            match disposition {
                                MessageDisposition::Ack | MessageDisposition::Reply(_) => {
                                    if matches!(disposition, MessageDisposition::Reply(_)) {
                                        tracing::warn!("Sled consumer received a Reply/StreamReply, but replying is not supported. Dropping reply.");
                                    }
                                    inflight_tree.remove(key_clone).map_err(|e| anyhow!(e))?;
                                }
                                MessageDisposition::Nack => {
                                    // Re-insert on Nack in Queue mode (move back from inflight to main)
                                    (&tree, &inflight_tree)
                                        .transaction(|(t_main, t_inflight)| {
                                            if t_inflight.remove(&key_clone)?.is_some() {
                                                t_main
                                                    .insert(&key_clone, value_clone.as_slice())?;
                                            }
                                            Ok(())
                                        })
                                        .map_err(|e: sled::transaction::TransactionError<()>| {
                                            anyhow!("{:?}", e)
                                        })?;
                                }
                            }
                        }
                        Ok(())
                    }) as crate::traits::BoxFuture<'static, anyhow::Result<()>>
                });

                return Ok(Received { message, commit });
            }

            // If no message, wait for notification
            if self.notify_rx.recv().await.is_err() {
                return Err(ConsumerError::EndOfStream);
            }
        }
    }

    async fn receive_batch(&mut self, max_messages: usize) -> Result<ReceivedBatch, ConsumerError> {
        if max_messages == 0 {
            return Ok(ReceivedBatch {
                messages: Vec::new(),
                commit: Box::new(|_| Box::pin(async { Ok(()) })),
            });
        }
        let mut messages = Vec::with_capacity(max_messages);
        let mut commits = Vec::with_capacity(max_messages);

        // First message blocks
        let first = self.receive().await?;
        messages.push(first.message);
        commits.push(first.commit);

        // Subsequent messages poll with timeout
        for _ in 1..max_messages {
            match tokio::time::timeout(Duration::from_millis(10), self.receive()).await {
                Ok(Ok(received)) => {
                    messages.push(received.message);
                    commits.push(received.commit);
                }
                _ => break,
            }
        }

        Ok(ReceivedBatch {
            messages,
            commit: Box::new(move |dispositions| {
                Box::pin(async move {
                    for (commit, disposition) in commits.into_iter().zip(dispositions) {
                        commit(disposition).await?;
                    }
                    Ok(())
                })
            }),
        })
    }

    async fn status(&self) -> EndpointStatus {
        // Note: Tree::len() is O(n) in sled and can be expensive for large trees.
        let (healthy, error, pending, inflight) = match self.tree.flush() {
            Ok(_) => (
                true,
                None,
                Some(self.tree.len()),
                Some(self.inflight_tree.len()),
            ),
            Err(e) => (false, Some(format!("Sled flush failed: {}", e)), None, None),
        };

        EndpointStatus {
            healthy,
            target: String::from_utf8_lossy(&self.tree.name()).to_string(),
            pending,
            error,
            details: serde_json::json!({
                "inflight": inflight
            }),
            ..Default::default()
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::CanonicalMessage;
    use tempfile::tempdir;
    use tokio::time::{timeout, Duration};

    #[tokio::test]
    async fn test_sled_queue_mode() {
        let dir = tempdir().unwrap();
        let path = dir.path().to_str().unwrap().to_string();
        let config = SledConfig {
            path: path.clone(),
            tree: None,
            read_from_start: true,
            delete_after_read: true,
        };

        let publisher = SledPublisher::new(&config).unwrap();
        let mut consumer = SledConsumer::new(&config).unwrap();

        let msg = CanonicalMessage::new(b"queue_item".to_vec(), None);
        publisher.send(msg.clone()).await.unwrap();

        let received = timeout(Duration::from_secs(2), consumer.receive())
            .await
            .expect("Timed out waiting for message")
            .unwrap();

        assert_eq!(received.message.payload, msg.payload);

        // Commit (Ack)
        (received.commit)(MessageDisposition::Ack).await.unwrap();

        // Verify DB is empty
        let db = get_or_open_db(&path).unwrap();
        let tree = db.open_tree("default").unwrap();
        assert!(tree.is_empty());
        close_db(&path).unwrap();
    }

    #[tokio::test]
    async fn test_sled_topic_mode() {
        let dir = tempdir().unwrap();
        let path = dir.path().to_str().unwrap().to_string();
        let config = SledConfig {
            path: path.clone(),
            tree: Some("topic".to_string()),
            read_from_start: true,
            delete_after_read: false,
        };

        let publisher = SledPublisher::new(&config).unwrap();
        let mut consumer = SledConsumer::new(&config).unwrap();

        let msg1 = CanonicalMessage::new(b"msg1".to_vec(), None);
        publisher.send(msg1.clone()).await.unwrap();

        let received1 = timeout(Duration::from_secs(2), consumer.receive())
            .await
            .expect("Timed out waiting for msg1")
            .unwrap();
        assert_eq!(received1.message.payload, msg1.payload);

        let msg2 = CanonicalMessage::new(b"msg2".to_vec(), None);
        publisher.send(msg2.clone()).await.unwrap();

        let received2 = timeout(Duration::from_secs(2), consumer.receive())
            .await
            .expect("Timed out waiting for msg2")
            .unwrap();
        assert_eq!(received2.message.payload, msg2.payload);
        close_db(&path).unwrap();
    }

    #[tokio::test]
    async fn test_sled_nack_requeue() {
        let dir = tempdir().unwrap();
        let path = dir.path().to_str().unwrap().to_string();
        let config = SledConfig {
            path: path.clone(),
            tree: None,
            read_from_start: true,
            delete_after_read: true,
        };

        let publisher = SledPublisher::new(&config).unwrap();
        let mut consumer = SledConsumer::new(&config).unwrap();

        let msg = CanonicalMessage::new(b"retry_me".to_vec(), None);
        publisher.send(msg.clone()).await.unwrap();

        // Receive and Nack
        let received = consumer.receive().await.unwrap();
        (received.commit)(MessageDisposition::Nack).await.unwrap();

        // Should be available again
        let received_retry = timeout(Duration::from_secs(2), consumer.receive())
            .await
            .expect("Timed out waiting for retry")
            .unwrap();

        assert_eq!(received_retry.message.payload, msg.payload);
        close_db(&path).unwrap();
    }

    #[tokio::test]
    async fn test_sled_status() {
        let dir = tempdir().unwrap();
        let path = dir.path().to_str().unwrap().to_string();
        let config = SledConfig {
            path: path.clone(),
            tree: Some("status_tree".to_string()),
            ..Default::default()
        };

        let publisher = SledPublisher::new(&config).unwrap();
        let status = publisher.status().await;
        assert!(status.healthy);
        assert_eq!(status.target, "status_tree");
        close_db(&path).unwrap();
    }
}