mq-bridge 0.3.2

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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
use crate::canonical_message::tracing_support::LazyMessageIds;
use crate::models::AmqpConfig;
use crate::traits::{
    BatchCommitFunc, BoxFuture, ConsumerError, EndpointStatus, MessageConsumer, MessageDisposition,
    MessagePublisher, PublisherError, ReceivedBatch, Sent, SentBatch,
};
use crate::CanonicalMessage;
use crate::APP_NAME;
use anyhow::{anyhow, bail, Context};
use async_trait::async_trait;
use futures::{future::join_all, FutureExt, StreamExt, TryStreamExt};
use lapin::tcp::{OwnedIdentity, OwnedTLSConfig};
use lapin::{
    options::{
        BasicAckOptions, BasicConsumeOptions, BasicPublishOptions, BasicQosOptions,
        ExchangeDeclareOptions, QueueBindOptions, QueueDeclareOptions,
    },
    types::{FieldTable, ShortString},
    Acker, BasicProperties, Channel, Confirmation, Connection, ConnectionProperties, Consumer,
    DefaultConnectionBuilder, ExchangeKind,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use std::{any::Any, sync::Arc};
use tokio::sync::RwLock;
use tracing::{error, info, trace};
use uuid::Uuid;

/// Maximum time to wait for a broker publisher confirmation before treating the
/// publish as failed. Prevents the producer from hanging indefinitely when the
/// connection drops and the confirm future never resolves.
const CONFIRM_TIMEOUT: Duration = Duration::from_secs(5);

/// How often the consumer wakes up while waiting for a message to re-check that
/// the broker connection is still alive. In lapin 4 a dropped connection does
/// not reliably terminate the consumer stream, so without this periodic health
/// check `Consumer::next()` can block forever after the broker goes away and the
/// route would never get the error it needs to reconnect.
const CONSUMER_HEALTH_POLL: Duration = Duration::from_secs(1);

/// Maximum time any single connection-setup step (connect, create_channel,
/// confirm_select, declares, qos, consume) may take. In lapin 4 a
/// `Connection::connect` can return `Ok` on a socket the broker immediately
/// force-closes; the io_loop then aborts recovery and any subsequent channel
/// operation on that connection never resolves. Without a bound, the
/// publisher's `reconnect` parks inside `connect` holding the state write lock
/// forever and the whole producer wedges. Bounding each step lets the attempt
/// fail and be retried against a fresh connection.
const SETUP_TIMEOUT: Duration = Duration::from_secs(5);

/// Bounds a single lapin setup operation, turning a hang or error into a
/// retryable failure. See [`SETUP_TIMEOUT`].
async fn with_setup_timeout<T>(
    op: &str,
    fut: impl std::future::Future<Output = Result<T, lapin::Error>>,
) -> anyhow::Result<T> {
    match tokio::time::timeout(SETUP_TIMEOUT, fut).await {
        Ok(Ok(v)) => Ok(v),
        Ok(Err(e)) => Err(anyhow!("AMQP {op} failed: {e}")),
        Err(_) => Err(anyhow!("AMQP {op} timed out after {SETUP_TIMEOUT:?}")),
    }
}

struct AmqpState {
    connection: Connection,
    channel: Channel,
    /// Bumped on every successful reconnect. Lets a worker that observed a
    /// failure ask for a reconnect while letting concurrent workers that share
    /// the same dead channel deduplicate, without trusting lapin's connection
    /// status (which in lapin 4 can keep reporting `connected` on a dead link).
    generation: u64,
}

pub struct AmqpPublisher {
    state: Arc<RwLock<AmqpState>>,
    config: AmqpConfig,
    exchange: String,
    queue: String,
    no_persistence: bool,
    delayed_ack: bool,
}

impl AmqpPublisher {
    pub async fn new(config: &AmqpConfig) -> anyhow::Result<Self> {
        let state = Self::connect(config).await?;
        let queue_or_exchange = config
            .queue
            .as_deref()
            .ok_or_else(|| anyhow!("Queue name is required for AMQP publisher"))?;

        let (exchange, queue) = if config.subscribe_mode {
            (
                config
                    .exchange
                    .clone()
                    .unwrap_or_else(|| queue_or_exchange.to_string()),
                "".to_string(),
            )
        } else {
            (
                config.exchange.clone().unwrap_or_default(),
                queue_or_exchange.to_string(),
            )
        };

        Ok(Self {
            state: Arc::new(RwLock::new(state)),
            config: config.clone(),
            exchange,
            queue,
            no_persistence: config.no_persistence,
            delayed_ack: config.delayed_ack,
        })
    }

    async fn connect(config: &AmqpConfig) -> anyhow::Result<AmqpState> {
        let queue_or_exchange = config
            .queue
            .as_deref()
            .ok_or_else(|| anyhow!("Queue name is required for AMQP publisher"))?;
        let conn = create_amqp_connection(config).await?;
        let channel = with_setup_timeout("create_channel", conn.create_channel()).await?;
        // Enable publisher confirms on this channel to allow waiting for acks.
        with_setup_timeout(
            "confirm_select",
            channel.confirm_select(lapin::options::ConfirmSelectOptions::default()),
        )
        .await?;

        if !config.no_declare_queue {
            if config.subscribe_mode {
                let exchange_name = config.exchange.as_deref().unwrap_or(queue_or_exchange);
                info!(exchange = %exchange_name, "Declaring AMQP Fanout exchange in sink");
                with_setup_timeout(
                    "exchange_declare",
                    channel.exchange_declare(
                        exchange_name.into(),
                        ExchangeKind::Fanout,
                        ExchangeDeclareOptions {
                            durable: !config.no_persistence,
                            ..Default::default()
                        },
                        FieldTable::default(),
                    ),
                )
                .await?;
            } else {
                // Ensure the queue exists before we try to publish to it. This is idempotent.
                info!(queue = %queue_or_exchange, "Declaring AMQP queue in sink");
                with_setup_timeout(
                    "queue_declare",
                    channel.queue_declare(
                        queue_or_exchange.into(),
                        QueueDeclareOptions {
                            durable: !config.no_persistence,
                            ..Default::default()
                        },
                        FieldTable::default(),
                    ),
                )
                .await?;
            }
        }

        Ok(AmqpState {
            connection: conn,
            channel,
            generation: 0,
        })
    }

    /// Returns the current channel together with the generation it belongs to.
    /// Callers pass the generation back to `reconnect` so a reconnect only fires
    /// for the channel that actually failed.
    async fn get_channel(&self) -> (Channel, u64) {
        let state = self.state.read().await;
        (state.channel.clone(), state.generation)
    }

    /// Reconnect after a worker observed a failure on the channel of generation
    /// `observed_generation`. If another worker has already reconnected since
    /// (generation moved on), this is a no-op. We deliberately do NOT consult
    /// `connection.status()` here: in lapin 4 a dead connection can keep
    /// reporting `connected`, which previously made this early-return and left
    /// the publisher wedged on a dead channel forever.
    async fn reconnect(&self, observed_generation: u64) {
        let mut state = self.state.write().await;
        if state.generation != observed_generation {
            // A concurrent worker already rebuilt the connection.
            return;
        }
        info!("Reconnecting AMQP publisher...");
        match Self::connect(&self.config).await {
            Ok(new_state) => {
                let next_generation = state.generation.wrapping_add(1);
                *state = new_state;
                state.generation = next_generation;
                info!("AMQP publisher reconnected.");
            }
            Err(e) => {
                error!("Failed to reconnect AMQP publisher: {}", e);
            }
        }
    }
}

#[async_trait]
impl MessagePublisher for AmqpPublisher {
    async fn send(&self, message: CanonicalMessage) -> Result<Sent, PublisherError> {
        trace!(
            message_id = %format!("{:032x}", message.message_id),
            queue = %self.queue,
            payload_size = message.payload.len(),
            "Publishing AMQP message"
        );
        let mut properties = if self.no_persistence {
            BasicProperties::default()
        } else {
            // Delivery mode 2 makes the message persistent
            BasicProperties::default().with_delivery_mode(2)
        };
        if let Some(reply_to) = message.metadata.get("reply_to") {
            properties = properties.with_reply_to(reply_to.clone().into());
        }
        if let Some(correlation_id) = message.metadata.get("correlation_id") {
            properties = properties.with_correlation_id(correlation_id.clone().into());
        }
        if !message.metadata.is_empty() {
            let mut table = FieldTable::default();
            for (key, value) in &message.metadata {
                // Skip reply_to and correlation_id since they are already set as native properties
                if key == "reply_to" || key == "correlation_id" {
                    continue;
                }
                if crate::canonical_message::is_source_metadata_key(key) {
                    continue; // source/provenance keys must not be forwarded
                }
                table.insert(
                    ShortString::from(key.as_str()),
                    lapin::types::AMQPValue::LongString(value.clone().into()),
                );
            }
            properties = properties.with_headers(table);
        }

        let (channel, generation) = self.get_channel().await;
        let publish_fut = channel.basic_publish(
            self.exchange.clone().into(),
            self.queue.clone().into(),
            BasicPublishOptions::default(),
            &message.payload,
            properties,
        );
        // Bound the publish submit. In lapin 4 a publish to a silently-dropped
        // connection can hang without surfacing an error, so cap the wait and
        // treat it as a retryable failure that triggers a reconnect.
        let confirmation_result = match tokio::time::timeout(CONFIRM_TIMEOUT, publish_fut).await {
            Ok(res) => res,
            Err(_) => {
                self.reconnect(generation).await;
                return Err(PublisherError::Retryable(anyhow!(
                    "Timed out submitting AMQP publish"
                )));
            }
        };

        let confirmation = match confirmation_result {
            Ok(c) => c,
            Err(e) => {
                self.reconnect(generation).await;
                return Err(PublisherError::Retryable(anyhow!(
                    "Failed to publish AMQP message: {}",
                    e
                )));
            }
        };

        if !self.delayed_ack {
            // Wait for the broker's publisher confirmation. Bound the wait so a
            // dropped connection (where the confirm future may never resolve)
            // can't hang the producer indefinitely; instead reconnect and retry.
            let confirm = match tokio::time::timeout(CONFIRM_TIMEOUT, confirmation).await {
                Ok(Ok(c)) => c,
                Ok(Err(e)) => {
                    self.reconnect(generation).await;
                    return Err(PublisherError::Retryable(anyhow!(
                        "Failed to get AMQP publisher confirmation: {}",
                        e
                    )));
                }
                Err(_) => {
                    self.reconnect(generation).await;
                    return Err(PublisherError::Retryable(anyhow!(
                        "Timed out waiting for AMQP publisher confirmation"
                    )));
                }
            };
            if let Confirmation::Nack(_) = confirm {
                return Err(PublisherError::Retryable(anyhow::anyhow!(
                    "Broker Nacked the message"
                )));
            }
        }
        Ok(Sent::Ack)
    }

    async fn send_batch(
        &self,
        messages: Vec<CanonicalMessage>,
    ) -> Result<SentBatch, PublisherError> {
        trace!(count = messages.len(), queue = %self.queue, message_ids = ?LazyMessageIds(&messages), "Publishing batch of AMQP messages");
        if self.delayed_ack {
            return crate::traits::send_batch_helper(self, messages, |publisher, message| {
                Box::pin(publisher.send(message))
            })
            .await;
        }

        let (channel, generation) = self.get_channel().await;
        let mut pending_messages = Vec::with_capacity(messages.len());
        let mut pending_confirms = Vec::with_capacity(messages.len());
        let mut failed_messages = Vec::new();

        for message in messages {
            let mut properties = if self.no_persistence {
                BasicProperties::default()
            } else {
                BasicProperties::default().with_delivery_mode(2)
            };
            if let Some(reply_to) = message.metadata.get("reply_to") {
                properties = properties.with_reply_to(reply_to.clone().into());
            }
            if let Some(correlation_id) = message.metadata.get("correlation_id") {
                properties = properties.with_correlation_id(correlation_id.clone().into());
            }

            if !message.metadata.is_empty() {
                let mut table = FieldTable::default();
                for (key, value) in &message.metadata {
                    // Skip reply_to and correlation_id since they are already set as native properties
                    if key == "reply_to" || key == "correlation_id" {
                        continue;
                    }
                    if crate::canonical_message::is_source_metadata_key(key) {
                        continue; // source/provenance keys must not be forwarded
                    }
                    table.insert(
                        ShortString::from(key.clone()),
                        lapin::types::AMQPValue::LongString(value.clone().into()),
                    );
                }
                properties = properties.with_headers(table);
            }

            let publish_fut = channel.basic_publish(
                self.exchange.clone().into(),
                self.queue.clone().into(),
                BasicPublishOptions::default(),
                &message.payload,
                properties,
            );
            match tokio::time::timeout(CONFIRM_TIMEOUT, publish_fut).await {
                Ok(Ok(confirmation)) => {
                    pending_messages.push(message);
                    pending_confirms.push(confirmation);
                }
                Ok(Err(e)) => {
                    failed_messages.push((
                        message,
                        PublisherError::Retryable(
                            anyhow!(e).context("Failed to publish message in batch"),
                        ),
                    ));
                }
                Err(_) => {
                    // In lapin 4 a publish submitted to a silently-dropped
                    // connection can hang without ever surfacing an error. Bound
                    // the submit so the batch fails fast, reconnects, and retries.
                    failed_messages.push((
                        message,
                        PublisherError::Retryable(anyhow!(
                            "Timed out submitting AMQP publish in batch"
                        )),
                    ));
                }
            }
        }

        if !pending_confirms.is_empty() {
            // Bound the whole batch, not each confirmation separately. A broker
            // reset can otherwise stall for batch_size * CONFIRM_TIMEOUT before
            // the route can reconnect and retry.
            match tokio::time::timeout(CONFIRM_TIMEOUT, join_all(pending_confirms)).await {
                Ok(confirmations) => {
                    for (message, confirmation) in pending_messages.into_iter().zip(confirmations) {
                        match confirmation {
                            Ok(confirm) => {
                                if let Confirmation::Nack(_) = confirm {
                                    failed_messages.push((
                                        message,
                                        PublisherError::Retryable(anyhow::anyhow!(
                                            "Broker Nacked the message"
                                        )),
                                    ));
                                }
                            }
                            Err(e) => {
                                failed_messages.push((
                                    message,
                                    PublisherError::Retryable(anyhow::anyhow!(
                                        "Publisher confirmation failed: {}",
                                        e
                                    )),
                                ));
                            }
                        }
                    }
                }
                Err(_) => {
                    failed_messages.extend(pending_messages.into_iter().map(|message| {
                        (
                            message,
                            PublisherError::Retryable(anyhow::anyhow!(
                                "Timed out waiting for AMQP batch publisher confirmations"
                            )),
                        )
                    }));
                }
            }
        }

        if !failed_messages.is_empty() {
            self.reconnect(generation).await;
        }

        if failed_messages.is_empty() {
            Ok(SentBatch::Ack)
        } else {
            Ok(SentBatch::Partial {
                responses: None,
                failed: failed_messages,
            })
        }
    }

    async fn status(&self) -> EndpointStatus {
        let state = self.state.read().await;
        let conn_status = state.connection.status();
        let chan_status = state.channel.status();
        let healthy = conn_status.connected() && chan_status.connected();
        let error = if !healthy {
            Some(format!(
                "Connection: '{:?}', Channel: '{:?}'",
                conn_status, chan_status
            ))
        } else {
            None
        };
        EndpointStatus {
            healthy,
            error,
            target: if self.exchange.is_empty() {
                self.queue.clone()
            } else {
                self.exchange.clone()
            },
            details: serde_json::json!({ "queue": self.queue, "delayed_ack": self.delayed_ack }),
            ..Default::default()
        }
    }

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

pub struct AmqpConsumer {
    _conn: Connection,
    consumer: Consumer,
    channel: Channel,
    queue: String,
    is_poisoned: Arc<AtomicBool>,
    reply_confirms_selected: Arc<tokio::sync::OnceCell<()>>,
    prefetch: u16,
}

impl AmqpConsumer {
    pub async fn new(config: &AmqpConfig) -> anyhow::Result<Self> {
        let queue_or_exchange = config
            .queue
            .as_deref()
            .ok_or_else(|| anyhow!("Queue name is required for AMQP consumer"))?;
        let conn = create_amqp_connection(config).await?;
        let channel = with_setup_timeout("create_channel", conn.create_channel()).await?;

        let is_subscriber = config.subscribe_mode;

        let queue_name = if is_subscriber {
            // Subscriber mode: Declare Fanout exchange and temporary queue
            let exchange_name = config.exchange.as_deref().unwrap_or(queue_or_exchange);
            info!(exchange = %exchange_name, "Declaring AMQP Fanout exchange for subscriber");
            with_setup_timeout(
                "exchange_declare",
                channel.exchange_declare(
                    exchange_name.into(),
                    ExchangeKind::Fanout,
                    ExchangeDeclareOptions {
                        durable: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                ),
            )
            .await?;

            let id = fast_uuid_v7::gen_id_string();
            let queue_name_str = format!("{}-{}-{}", APP_NAME, queue_or_exchange, id);
            let queue = with_setup_timeout(
                "queue_declare",
                channel.queue_declare(
                    queue_name_str.clone().into(),
                    QueueDeclareOptions {
                        exclusive: true,
                        auto_delete: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                ),
            )
            .await?;
            let q_name = queue.name().as_str().to_string();

            info!(queue = %q_name, exchange = %exchange_name, "Binding temporary queue to exchange");
            with_setup_timeout(
                "queue_bind",
                channel.queue_bind(
                    q_name.clone().into(),
                    exchange_name.into(),
                    "".into(),
                    QueueBindOptions::default(),
                    FieldTable::default(),
                ),
            )
            .await?;
            q_name
        } else {
            // Consumer mode: Declare durable queue
            info!(queue = %queue_or_exchange, "Declaring AMQP queue");
            with_setup_timeout(
                "queue_declare",
                channel.queue_declare(
                    queue_or_exchange.into(),
                    QueueDeclareOptions {
                        durable: !config.no_persistence,
                        ..Default::default()
                    },
                    FieldTable::default(),
                ),
            )
            .await?;
            queue_or_exchange.to_string()
        };

        // Set prefetch count. This acts as a buffer and is crucial for concurrent processing.
        // We'll get the concurrency from the route config, but for now, let's use a reasonable default
        // that can be overridden by a new method.
        let prefetch_count = config.prefetch_count.unwrap_or(100);
        with_setup_timeout(
            "basic_qos",
            channel.basic_qos(prefetch_count, BasicQosOptions::default()),
        )
        .await?;

        let consumer_tag = if is_subscriber {
            format!("{}_sub_{}", APP_NAME, fast_uuid_v7::gen_id_str())
        } else {
            format!("{}_amqp_consumer", APP_NAME)
        };
        info!(queue = %queue_name, consumer_tag = %consumer_tag, "Starting AMQP consumer");

        let consumer = with_setup_timeout(
            "basic_consume",
            channel.basic_consume(
                queue_name.clone().into(),
                consumer_tag.into(),
                BasicConsumeOptions::default(),
                FieldTable::default(),
            ),
        )
        .await?;

        Ok(Self {
            _conn: conn,
            consumer,
            channel,
            queue: queue_name,
            is_poisoned: Arc::new(AtomicBool::new(false)),
            reply_confirms_selected: Arc::new(tokio::sync::OnceCell::new()),
            prefetch: prefetch_count,
        })
    }
}

async fn create_amqp_connection(config: &AmqpConfig) -> anyhow::Result<Connection> {
    info!(url = %config.url, "Connecting to AMQP broker");
    let mut url = url::Url::parse(&config.url).context("Failed to parse AMQP URL")?;

    if let (Some(user), Some(pass)) = (&config.username, &config.password) {
        url.set_username(user)
            .map_err(|_| anyhow!("Failed to set username on AMQP URL"))?;
        url.set_password(Some(pass))
            .map_err(|_| anyhow!("Failed to set password on AMQP URL"))?;
    }

    if !url.query_pairs().any(|(k, _)| k == "heartbeat") {
        url.query_pairs_mut().append_pair("heartbeat", "15");
    }
    let conn_uri = url.to_string();

    let mut last_error: Option<anyhow::Error> = None;
    for attempt in 1..=5 {
        // Avoid logging credentials embedded in URLs.
        info!(attempt = attempt, "Attempting to connect to AMQP broker");
        let conn_props = ConnectionProperties::default();
        // Bound the connect itself: a hung TLS/AMQP handshake against a broker
        // that is restarting must not stall the reconnect indefinitely.
        let result = if config.tls.required {
            let tls_config = build_tls_config(config).await?;
            let builder = DefaultConnectionBuilder::new()?
                .with_uri_str(conn_uri.clone())
                .with_properties(conn_props)
                .with_tls_config(tls_config);
            with_setup_timeout("connect", builder.connect()).await
        } else {
            with_setup_timeout("connect", Connection::connect(&conn_uri, conn_props)).await
        };

        match result {
            Ok(conn) => return Ok(conn),
            Err(e) => {
                last_error = Some(e);
                // No need to back off after the final attempt.
                if attempt < 5 {
                    tokio::time::sleep(Duration::from_secs(attempt * 2)).await; // Exponential backoff
                }
            }
        }
    }
    Err(anyhow!(
        "Failed to connect to AMQP after multiple attempts: {:?}",
        last_error.unwrap()
    ))
}

async fn build_tls_config(config: &AmqpConfig) -> anyhow::Result<OwnedTLSConfig> {
    // For AMQP, cert_chain is the CA file.
    let ca_file = config.tls.ca_file.clone();

    let identity = if let Some(cert_file) = &config.tls.cert_file {
        // For lapin, client identity is provided via a PKCS12 file.
        // The `cert_file` is assumed to be the PKCS12 bundle. The `key_file` is not used.
        let der = tokio::fs::read(cert_file).await?;
        let password = config.tls.cert_password.clone().unwrap_or_default();
        Some(OwnedIdentity::PKCS12 { der, password })
    } else {
        None
    };

    Ok(OwnedTLSConfig {
        identity,
        cert_chain: ca_file,
    })
}

fn delivery_to_canonical_message(delivery: &lapin::message::Delivery) -> CanonicalMessage {
    let mut message_id = Some(delivery.delivery_tag as u128);
    if let Some(amqp_id) = delivery.properties.message_id().as_ref() {
        if let Ok(uuid) = Uuid::parse_str(amqp_id.as_str()) {
            message_id = Some(uuid.as_u128());
        } else if let Ok(val) = amqp_id.as_str().parse::<u128>() {
            message_id = Some(val);
        }
    }

    let mut canonical_message = CanonicalMessage::new(delivery.data.clone(), message_id);

    if let Some(amqp_id) = delivery.properties.message_id().as_ref() {
        canonical_message
            .metadata
            .insert("amqp_message_id".to_string(), amqp_id.to_string());
    }
    if let Some(correlation_id) = delivery.properties.correlation_id().as_ref() {
        canonical_message
            .metadata
            .insert("correlation_id".to_string(), correlation_id.to_string());
    }
    if let Some(reply_to) = delivery.properties.reply_to().as_ref() {
        canonical_message
            .metadata
            .insert("reply_to".to_string(), reply_to.to_string());
    }

    // Source-position cursor keys (useful for dlt-style pull consumers).
    // Opt-in via the MQB_SOURCE_METADATA env var; off by default.
    if crate::canonical_message::source_metadata_enabled() {
        canonical_message.metadata.insert(
            "mqb.src.amqp_routing_key".to_string(),
            delivery.routing_key.to_string(),
        );
        canonical_message.metadata.insert(
            "mqb.src.amqp_exchange".to_string(),
            delivery.exchange.to_string(),
        );
        canonical_message.metadata.insert(
            "mqb.src.amqp_delivery_tag".to_string(),
            delivery.delivery_tag.to_string(),
        );
    }

    if let Some(headers) = delivery.properties.headers().as_ref() {
        for (key, value) in headers.inner().iter() {
            // Keep the framework-generated cursor keys above authoritative: never
            // let an inbound header spoof a reserved `mqb.src.*` value.
            if crate::canonical_message::is_source_metadata_key(key.as_str()) {
                continue;
            }
            let value_str = match value {
                lapin::types::AMQPValue::LongString(s) => s.to_string(),
                lapin::types::AMQPValue::ShortString(s) => s.to_string(),
                lapin::types::AMQPValue::Boolean(b) => b.to_string(),
                lapin::types::AMQPValue::LongInt(i) => i.to_string(),
                _ => continue,
            };
            canonical_message
                .metadata
                .insert(key.to_string(), value_str);
        }
    }
    canonical_message
}

#[async_trait]
impl MessageConsumer for AmqpConsumer {
    // AMQP acks each delivery individually (`BasicAckOptions::default()` =>
    // multiple: false; nacks via BasicNack), and already acks a batch's deliveries
    // concurrently via buffer_unordered. Out-of-order acks across batches are
    // therefore safe: un-acked deliveries are redelivered on reconnect, so this is
    // NOT a cumulative-ack transport despite historically sharing the sequencer.
    fn commit_requires_order(&self) -> bool {
        false
    }
    async fn receive_batch(&mut self, max_messages: usize) -> Result<ReceivedBatch, ConsumerError> {
        if self.is_poisoned.load(Ordering::Relaxed) {
            return Err(ConsumerError::Connection(anyhow::anyhow!(
                "AMQP consumer is poisoned due to a previous commit failure."
            )));
        }

        if max_messages == 0 {
            return Ok(ReceivedBatch {
                messages: Vec::new(),
                commit: Box::new(|_| Box::pin(async { Ok(()) })),
            });
        }

        // 1. Wait for the first message. This blocks until a message is available,
        //    but each wait is bounded so we can periodically verify the broker
        //    connection is still alive. In lapin 4 a dropped connection does not
        //    reliably terminate the consumer stream, so without this health check
        //    `next()` would block forever after the broker goes away and the route
        //    would never see the Connection error it needs to recreate the consumer.
        let first_delivery = loop {
            match tokio::time::timeout(CONSUMER_HEALTH_POLL, self.consumer.next()).await {
                Ok(Some(Ok(delivery))) => break delivery,
                Ok(Some(Err(e))) => return Err(ConsumerError::Connection(anyhow::anyhow!(e))),
                Ok(None) => {
                    return Err(ConsumerError::Connection(anyhow::anyhow!(
                        "AMQP consumer stream ended unexpectedly"
                    )))
                }
                Err(_) => {
                    // No message arrived within the poll window. If the connection
                    // or channel has dropped, surface a Connection error so the route
                    // recreates the consumer; otherwise keep waiting for a message.
                    if !self._conn.status().connected() || !self.channel.status().connected() {
                        return Err(ConsumerError::Connection(anyhow::anyhow!(
                            "AMQP connection lost while waiting for messages"
                        )));
                    }
                }
            }
        };

        let mut messages = Vec::with_capacity(max_messages);
        let mut ackers = Vec::with_capacity(max_messages);
        let mut reply_infos = Vec::with_capacity(max_messages);

        let msg = delivery_to_canonical_message(&first_delivery);
        reply_infos.push((
            msg.metadata.get("reply_to").cloned(),
            msg.metadata.get("correlation_id").cloned(),
        ));
        messages.push(msg);
        ackers.push(first_delivery.acker);

        // 2. Greedily consume more messages if they are already buffered, up to max_messages.
        while messages.len() < max_messages {
            match self.consumer.try_next().now_or_never() {
                Some(Ok(Some(delivery))) => {
                    let msg = delivery_to_canonical_message(&delivery);
                    reply_infos.push((
                        msg.metadata.get("reply_to").cloned(),
                        msg.metadata.get("correlation_id").cloned(),
                    ));
                    messages.push(msg);
                    ackers.push(delivery.acker);
                }
                Some(Ok(None)) => break, // Stream ended
                Some(Err(e)) => {
                    // An error occurred. Propagate it immediately.
                    return Err(ConsumerError::Connection(anyhow::anyhow!(e)));
                }
                None => break, // Stream is pending (no messages ready immediately)
            }
        }

        // 3. Create a commit function that acks all received messages.
        let messages_len = messages.len();
        trace!(count = messages_len, queue = %self.queue, message_ids = ?LazyMessageIds(&messages), "Received batch of AMQP messages");
        let channel = self.channel.clone();
        let is_poisoned = self.is_poisoned.clone();
        let reply_confirms_selected = self.reply_confirms_selected.clone();
        let commit: BatchCommitFunc = Box::new(move |dispositions: Vec<MessageDisposition>| {
            Box::pin(async move {
                if dispositions.len() != reply_infos.len() {
                    tracing::error!(
                        expected = reply_infos.len(),
                        actual = dispositions.len(),
                        "AMQP batch commit received mismatched disposition count"
                    );
                    return Err(anyhow::anyhow!(
                        "AMQP batch commit received mismatched disposition count: expected {}, got {}",
                        reply_infos.len(),
                        dispositions.len()
                    ));
                }

                let commit_op = async {
                    handle_replies(
                        &channel,
                        &reply_confirms_selected,
                        &reply_infos,
                        &dispositions,
                    )
                    .await?;
                    handle_dispositions(ackers, dispositions).await
                };

                let result = match tokio::time::timeout(Duration::from_secs(5), commit_op).await {
                    Ok(res) => res,
                    Err(_) => Err(anyhow::anyhow!("AMQP commit timed out")),
                };

                if result.is_err() {
                    is_poisoned.store(true, Ordering::Relaxed);
                }
                result
            }) as BoxFuture<'static, anyhow::Result<()>>
        });

        Ok(ReceivedBatch { messages, commit })
    }

    async fn status(&self) -> EndpointStatus {
        let conn_status = self._conn.status();
        let chan_status = self.channel.status();
        let mut healthy = conn_status.connected() && chan_status.connected();
        let mut pending: Option<usize> = None;
        let mut error: Option<String> = None;

        if healthy {
            let passive_declare = self.channel.queue_declare(
                self.queue.clone().into(),
                lapin::options::QueueDeclareOptions {
                    passive: true,
                    ..Default::default()
                },
                lapin::types::FieldTable::default(),
            );
            match tokio::time::timeout(Duration::from_secs(2), passive_declare).await {
                Ok(Ok(q)) => pending = Some(q.message_count() as usize),
                Ok(Err(e)) => {
                    healthy = false;
                    error = Some(e.to_string());
                }
                Err(e) => {
                    healthy = false;
                    error = Some(e.to_string());
                }
            }
        } else {
            error = Some(format!(
                "Connection: '{:?}', Channel: '{:?}'",
                conn_status, chan_status
            ));
        }

        EndpointStatus {
            healthy,
            target: self.queue.clone(),
            pending,
            error,
            capacity: Some(self.prefetch as usize),
            ..Default::default()
        }
    }

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

async fn handle_replies(
    channel: &Channel,
    reply_confirms_selected: &tokio::sync::OnceCell<()>,
    reply_infos: &[(Option<String>, Option<String>)],
    dispositions: &[MessageDisposition],
) -> anyhow::Result<()> {
    for ((reply_to, correlation_id), disposition) in reply_infos.iter().zip(dispositions.iter()) {
        let payload = match (disposition, reply_to) {
            (MessageDisposition::Reply(resp), Some(_)) => Some(resp.payload.clone()),
            (MessageDisposition::Reply(_), None) => {
                tracing::warn!("MessageDisposition::Reply received but no reply_to address found in original message");
                None
            }
            _ => None,
        };

        if let (Some(rt), Some(body)) = (reply_to, payload) {
            // Enable publisher confirms on the channel exactly once. Concurrent commits
            // share this cell and all await the first confirm_select, so none publishes a
            // reply before confirms are active. On error the cell stays uninitialized so a
            // later commit retries.
            reply_confirms_selected
                .get_or_try_init(|| async {
                    tokio::time::timeout(
                        SETUP_TIMEOUT,
                        channel.confirm_select(lapin::options::ConfirmSelectOptions::default()),
                    )
                    .await
                    .map_err(|_| anyhow!("Timed out enabling AMQP reply confirmations"))?
                    .context("Failed to enable AMQP reply confirmations")?;
                    Ok::<(), anyhow::Error>(())
                })
                .await?;

            let mut props = BasicProperties::default();
            if let Some(cid) = correlation_id {
                props = props.with_correlation_id(cid.clone().into());
            }

            // Publish response to the default exchange with the routing key set to reply_to
            let confirmation = tokio::time::timeout(
                CONFIRM_TIMEOUT,
                channel.basic_publish(
                    "".into(), // Default exchange
                    rt.clone().into(),
                    BasicPublishOptions {
                        mandatory: true,
                        ..Default::default()
                    },
                    &body,
                    props,
                ),
            )
            .await
            .map_err(|_| anyhow!("Timed out submitting AMQP reply to {}", rt))?
            .with_context(|| format!("Failed to publish AMQP reply to {}", rt))?;

            let confirmation = tokio::time::timeout(CONFIRM_TIMEOUT, confirmation)
                .await
                .map_err(|_| anyhow!("Timed out waiting for AMQP reply confirmation to {}", rt))?
                .with_context(|| format!("Failed to get AMQP reply confirmation to {}", rt))?;

            match confirmation {
                Confirmation::Ack(None) => {}
                Confirmation::Ack(Some(returned)) | Confirmation::Nack(Some(returned)) => {
                    bail!(
                        "AMQP reply to {} was returned by broker: {} {}",
                        rt,
                        returned.reply_code,
                        returned.reply_text
                    );
                }
                Confirmation::Nack(None) => {
                    bail!("Broker nacked AMQP reply to {}", rt);
                }
                Confirmation::NotRequested => {
                    bail!("AMQP reply confirmation was not requested for {}", rt);
                }
            }
        }
    }
    Ok(())
}

async fn handle_dispositions(
    ackers: Vec<Acker>,
    dispositions: Vec<MessageDisposition>,
) -> anyhow::Result<()> {
    let ackers_len = ackers.len();
    let mut futures = futures::stream::iter(ackers.into_iter().zip(dispositions).map(
        |(acker, disposition)| async move {
            match disposition {
                MessageDisposition::Ack | MessageDisposition::Reply(_) => {
                    acker.ack(BasicAckOptions::default()).await
                }
                MessageDisposition::Nack => {
                    // Nack with requeue. This will return the message to the front of the queue.
                    acker
                        .nack(lapin::options::BasicNackOptions {
                            requeue: true,
                            ..Default::default()
                        })
                        .await
                }
            }
        },
    ))
    .buffer_unordered(ackers_len);

    while let Some(res) = futures.next().await {
        if let Err(e) = res {
            bail!("Failed to ack/nack AMQP message: {}", e);
        }
    }
    Ok(())
}