-
Notifications
You must be signed in to change notification settings - Fork 43
[ingress] introduce buffer queue #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use alloy_primitives::Address; | ||
| use alloy_rpc_types_mev::EthSendBundle; | ||
| use anyhow::{Error, Result}; | ||
| use async_trait::async_trait; | ||
| use backon::{ExponentialBuilder, Retryable}; | ||
| use rdkafka::producer::{FutureProducer, FutureRecord}; | ||
| use tokio::time::Duration; | ||
| use tracing::{error, info}; | ||
|
|
||
| /// A queue to buffer transactions | ||
| #[async_trait] | ||
| pub trait QueuePublisher: Send + Sync { | ||
| async fn publish(&self, bundle: &EthSendBundle, sender: Address) -> Result<()>; | ||
| } | ||
|
|
||
| /// A queue to buffer transactions | ||
| pub struct KafkaQueuePublisher { | ||
| producer: FutureProducer, | ||
| topic: String, | ||
| } | ||
|
|
||
| impl KafkaQueuePublisher { | ||
| pub fn new(producer: FutureProducer, topic: String) -> Self { | ||
| Self { producer, topic } | ||
| } | ||
|
|
||
| pub async fn enqueue_bundle( | ||
| &self, | ||
| bundle: &EthSendBundle, | ||
| sender: Address, | ||
| ) -> Result<(), Error> { | ||
| let key = sender.to_string(); | ||
| let payload = serde_json::to_vec(bundle)?; | ||
|
|
||
| let enqueue = || async { | ||
| let record = FutureRecord::to(&self.topic).key(&key).payload(&payload); | ||
|
|
||
| match self.producer.send(record, Duration::from_secs(5)).await { | ||
| Ok((partition, offset)) => { | ||
| info!( | ||
| sender = %sender, | ||
| partition = partition, | ||
| offset = offset, | ||
| topic = %self.topic, | ||
| "Successfully enqueued bundle" | ||
| ); | ||
| Ok(()) | ||
| } | ||
| Err((err, _)) => { | ||
| error!( | ||
| sender = %sender, | ||
| error = %err, | ||
| topic = %self.topic, | ||
| "Failed to enqueue bundle" | ||
| ); | ||
| Err(anyhow::anyhow!("Failed to enqueue bundle: {}", err)) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| enqueue | ||
| .retry( | ||
| &ExponentialBuilder::default() | ||
| .with_min_delay(Duration::from_millis(100)) | ||
| .with_max_delay(Duration::from_secs(5)) | ||
| .with_max_times(3), | ||
|
Comment on lines
+64
to
+66
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could tune these params later? |
||
| ) | ||
| .notify(|err: &anyhow::Error, dur: Duration| { | ||
| info!("retrying to enqueue bundle {:?} after {:?}", err, dur); | ||
| }) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl QueuePublisher for KafkaQueuePublisher { | ||
| async fn publish(&self, bundle: &EthSendBundle, sender: Address) -> Result<()> { | ||
| self.enqueue_bundle(bundle, sender).await | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use rdkafka::config::ClientConfig; | ||
| use tokio::time::{Duration, Instant}; | ||
|
|
||
| fn create_test_bundle() -> EthSendBundle { | ||
| EthSendBundle::default() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_backoff_retry_logic() { | ||
| // use an invalid broker address to trigger the backoff logic | ||
| let producer = ClientConfig::new() | ||
| .set("bootstrap.servers", "localhost:9999") | ||
| .set("message.timeout.ms", "100") | ||
| .create() | ||
| .expect("Producer creation failed"); | ||
|
|
||
| let publisher = KafkaQueuePublisher::new(producer, "tips-ingress".to_string()); | ||
| let bundle = create_test_bundle(); | ||
| let sender = Address::ZERO; | ||
|
|
||
| let start = Instant::now(); | ||
| let result = publisher.enqueue_bundle(&bundle, sender).await; | ||
| let elapsed = start.elapsed(); | ||
|
|
||
| // the backoff tries at minimum 100ms, so verify we tried at least once | ||
| assert!(result.is_err()); | ||
| assert!(elapsed >= Duration::from_millis(100)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ services: | |
| command: | | ||
| sh -c " | ||
| kafka-topics --create --if-not-exists --topic tips-audit --bootstrap-server kafka:29092 --partitions 3 --replication-factor 1 | ||
| kafka-topics --create --if-not-exists --topic tips-ingress --bootstrap-server kafka:29092 --partitions 3 --replication-factor 1 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add a retention policy (time or size based)? |
||
| kafka-topics --list --bootstrap-server kafka:29092 | ||
| " | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add some retry logic with a backoff to this function? Maybe we can pull in this crate:
https://crates.io/crates/backon