|
| 1 | +use alloy_primitives::Address; |
| 2 | +use alloy_rpc_types_mev::EthSendBundle; |
| 3 | +use anyhow::{Error, Result}; |
| 4 | +use async_trait::async_trait; |
| 5 | +use backon::{ExponentialBuilder, Retryable}; |
| 6 | +use rdkafka::producer::{FutureProducer, FutureRecord}; |
| 7 | +use tokio::time::Duration; |
| 8 | +use tracing::{error, info}; |
| 9 | + |
| 10 | +/// A queue to buffer transactions |
| 11 | +#[async_trait] |
| 12 | +pub trait QueuePublisher: Send + Sync { |
| 13 | + async fn publish(&self, bundle: &EthSendBundle, sender: Address) -> Result<()>; |
| 14 | +} |
| 15 | + |
| 16 | +/// A queue to buffer transactions |
| 17 | +pub struct KafkaQueuePublisher { |
| 18 | + producer: FutureProducer, |
| 19 | + topic: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl KafkaQueuePublisher { |
| 23 | + pub fn new(producer: FutureProducer, topic: String) -> Self { |
| 24 | + Self { producer, topic } |
| 25 | + } |
| 26 | + |
| 27 | + pub async fn enqueue_bundle( |
| 28 | + &self, |
| 29 | + bundle: &EthSendBundle, |
| 30 | + sender: Address, |
| 31 | + ) -> Result<(), Error> { |
| 32 | + let key = sender.to_string(); |
| 33 | + let payload = serde_json::to_vec(bundle)?; |
| 34 | + |
| 35 | + let enqueue = || async { |
| 36 | + let record = FutureRecord::to(&self.topic).key(&key).payload(&payload); |
| 37 | + |
| 38 | + match self.producer.send(record, Duration::from_secs(5)).await { |
| 39 | + Ok((partition, offset)) => { |
| 40 | + info!( |
| 41 | + sender = %sender, |
| 42 | + partition = partition, |
| 43 | + offset = offset, |
| 44 | + topic = %self.topic, |
| 45 | + "Successfully enqueued bundle" |
| 46 | + ); |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + Err((err, _)) => { |
| 50 | + error!( |
| 51 | + sender = %sender, |
| 52 | + error = %err, |
| 53 | + topic = %self.topic, |
| 54 | + "Failed to enqueue bundle" |
| 55 | + ); |
| 56 | + Err(anyhow::anyhow!("Failed to enqueue bundle: {}", err)) |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + enqueue |
| 62 | + .retry( |
| 63 | + &ExponentialBuilder::default() |
| 64 | + .with_min_delay(Duration::from_millis(100)) |
| 65 | + .with_max_delay(Duration::from_secs(5)) |
| 66 | + .with_max_times(3), |
| 67 | + ) |
| 68 | + .notify(|err: &anyhow::Error, dur: Duration| { |
| 69 | + info!("retrying to enqueue bundle {:?} after {:?}", err, dur); |
| 70 | + }) |
| 71 | + .await |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[async_trait] |
| 76 | +impl QueuePublisher for KafkaQueuePublisher { |
| 77 | + async fn publish(&self, bundle: &EthSendBundle, sender: Address) -> Result<()> { |
| 78 | + self.enqueue_bundle(bundle, sender).await |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + use rdkafka::config::ClientConfig; |
| 86 | + use tokio::time::{Duration, Instant}; |
| 87 | + |
| 88 | + fn create_test_bundle() -> EthSendBundle { |
| 89 | + EthSendBundle::default() |
| 90 | + } |
| 91 | + |
| 92 | + #[tokio::test] |
| 93 | + async fn test_backoff_retry_logic() { |
| 94 | + // use an invalid broker address to trigger the backoff logic |
| 95 | + let producer = ClientConfig::new() |
| 96 | + .set("bootstrap.servers", "localhost:9999") |
| 97 | + .set("message.timeout.ms", "100") |
| 98 | + .create() |
| 99 | + .expect("Producer creation failed"); |
| 100 | + |
| 101 | + let publisher = KafkaQueuePublisher::new(producer, "tips-ingress".to_string()); |
| 102 | + let bundle = create_test_bundle(); |
| 103 | + let sender = Address::ZERO; |
| 104 | + |
| 105 | + let start = Instant::now(); |
| 106 | + let result = publisher.enqueue_bundle(&bundle, sender).await; |
| 107 | + let elapsed = start.elapsed(); |
| 108 | + |
| 109 | + // the backoff tries at minimum 100ms, so verify we tried at least once |
| 110 | + assert!(result.is_err()); |
| 111 | + assert!(elapsed >= Duration::from_millis(100)); |
| 112 | + } |
| 113 | +} |
0 commit comments