@@ -2,7 +2,9 @@ use alloy_primitives::Address;
22use alloy_rpc_types_mev:: EthSendBundle ;
33use anyhow:: { Error , Result } ;
44use async_trait:: async_trait;
5+ use backon:: { ExponentialBuilder , Retryable } ;
56use rdkafka:: producer:: { FutureProducer , FutureRecord } ;
7+ use tokio:: time:: Duration ;
68use tracing:: { error, info} ;
79
810/// A queue to buffer transactions
@@ -30,33 +32,43 @@ impl KafkaQueuePublisher {
3032 let key = sender. to_string ( ) ;
3133 let payload = serde_json:: to_vec ( bundle) ?;
3234
33- let record = FutureRecord :: to ( & self . topic ) . key ( & key) . payload ( & payload) ;
35+ let enqueue = || async {
36+ let record = FutureRecord :: to ( & self . topic ) . key ( & key) . payload ( & payload) ;
3437
35- match self
36- . producer
37- . send ( record, tokio:: time:: Duration :: from_secs ( 5 ) )
38- . await
39- {
40- Ok ( ( partition, offset) ) => {
41- info ! (
42- sender = %sender,
43- partition = partition,
44- offset = offset,
45- topic = %self . topic,
46- "Successfully enqueued bundle"
47- ) ;
48- Ok ( ( ) )
49- }
50- Err ( ( err, _) ) => {
51- error ! (
52- sender = %sender,
53- error = %err,
54- topic = %self . topic,
55- "Failed to enqueue bundle"
56- ) ;
57- Err ( anyhow:: anyhow!( "Failed to enqueue bundle: {}" , err) )
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+ }
5858 }
59- }
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
6072 }
6173}
6274
0 commit comments