@@ -53,8 +53,8 @@ use super::{
53
53
} ;
54
54
use crate :: { errors:: ConsumerError , metrics:: Metrics } ;
55
55
56
- const MAX_CONCURRENT_TASKS : usize = 32 ;
57
- const BATCH_SIZE : usize = 100 ;
56
+ const MAX_CONCURRENT_TASKS : usize = 30 ;
57
+ const BATCH_SIZE : usize = 30 ;
58
58
59
59
#[ derive( Debug ) ]
60
60
enum ProcessResult {
@@ -91,7 +91,6 @@ impl BlockExecutor {
91
91
& self ,
92
92
token : & CancellationToken ,
93
93
) -> Result < ( ) , ConsumerError > {
94
- let mut join_set = JoinSet :: new ( ) ;
95
94
tracing:: info!(
96
95
"Starting consumer with max concurrent tasks: {}" ,
97
96
MAX_CONCURRENT_TASKS
@@ -101,26 +100,18 @@ impl BlockExecutor {
101
100
let queue = NatsQueue :: BlockImporter ( self . message_broker . clone ( ) ) ;
102
101
103
102
while !token. is_cancelled ( ) {
104
- tokio:: select! {
105
- msg_result = queue. subscribe( BATCH_SIZE ) => {
106
- let mut messages = msg_result?;
107
- while let Some ( msg) = messages. next( ) . await {
108
- let msg = msg?;
109
- self . spawn_processing_tasks( msg, & mut join_set, )
110
- . await ?;
111
- }
112
- }
113
- Some ( result) = join_set. join_next( ) => {
103
+ let mut messages = queue. subscribe ( BATCH_SIZE ) . await ?;
104
+ while let Some ( msg) = messages. next ( ) . await {
105
+ let mut join_set = JoinSet :: new ( ) ;
106
+ let msg = msg?;
107
+ self . spawn_processing_tasks ( msg, & mut join_set) . await ?;
108
+ // Wait for all spawned tasks to complete before processing next message
109
+ while let Some ( result) = join_set. join_next ( ) . await {
114
110
Self :: handle_task_result ( result, & telemetry) . await ?;
115
111
}
116
112
}
117
113
}
118
114
119
- // Wait for all tasks to finish
120
- while let Some ( result) = join_set. join_next ( ) . await {
121
- Self :: handle_task_result ( result, & telemetry) . await ?;
122
- }
123
-
124
115
tracing:: info!( "Stopping broker ..." ) ;
125
116
shutdown_broker_with_timeout ( & self . message_broker ) . await ;
126
117
tracing:: info!( "Broker stopped successfully!" ) ;
@@ -138,13 +129,20 @@ impl BlockExecutor {
138
129
let payload = msg. payload ( ) ;
139
130
let msg_payload = MsgPayload :: decode_json ( & payload) ?. arc ( ) ;
140
131
let packets = Self :: build_packets ( & msg_payload) ;
132
+
141
133
join_set. spawn ( {
142
134
let semaphore = semaphore. clone ( ) ;
143
135
let packets = packets. clone ( ) ;
144
136
let msg_payload = msg_payload. clone ( ) ;
145
137
async move {
146
138
let _permit = semaphore. acquire ( ) . await ?;
147
139
let result = handle_stores ( & db, & packets, & msg_payload) . await ;
140
+ if result. is_ok ( ) {
141
+ msg. ack ( ) . await . map_err ( |e| {
142
+ tracing:: error!( "Failed to ack message: {:?}" , e) ;
143
+ ConsumerError :: MessageBrokerClient ( e)
144
+ } ) ?;
145
+ }
148
146
Ok :: < _ , ConsumerError > ( ProcessResult :: Store ( result) )
149
147
}
150
148
} ) ;
@@ -162,11 +160,6 @@ impl BlockExecutor {
162
160
}
163
161
} ) ;
164
162
165
- msg. ack ( ) . await . map_err ( |e| {
166
- tracing:: error!( "Failed to ack message: {:?}" , e) ;
167
- ConsumerError :: MessageBrokerClient ( e)
168
- } ) ?;
169
-
170
163
Ok ( ( ) )
171
164
}
172
165
0 commit comments