@@ -30,10 +30,14 @@ ROCKETMQ_NAMESPACE_BEGIN
3030
3131AsyncReceiveMessageCallback::AsyncReceiveMessageCallback (std::weak_ptr<ProcessQueue> process_queue)
3232 : process_queue_(std::move(process_queue)) {
33- receive_message_later_ = std::bind (&AsyncReceiveMessageCallback::checkThrottleThenReceive, this );
33+
34+ receive_message_later_ = std::bind (
35+ &AsyncReceiveMessageCallback::checkThrottleThenReceive, this , std::placeholders::_1);
3436}
3537
36- void AsyncReceiveMessageCallback::onCompletion (const std::error_code& ec, const ReceiveMessageResult& result) {
38+ void AsyncReceiveMessageCallback::onCompletion (
39+ const std::error_code& ec, std::string& attempt_id, const ReceiveMessageResult& result) {
40+
3741 std::shared_ptr<ProcessQueue> process_queue = process_queue_.lock ();
3842 if (!process_queue) {
3943 SPDLOG_INFO (" Process queue has been destructed." );
@@ -47,31 +51,32 @@ void AsyncReceiveMessageCallback::onCompletion(const std::error_code& ec, const
4751
4852 if (ec == ErrorCode::TooManyRequests) {
4953 SPDLOG_WARN (" Action of receiving message is throttled. Retry after 20ms. Queue={}" , process_queue->simpleName ());
50- receiveMessageLater (std::chrono::milliseconds (20 ));
54+ receiveMessageLater (std::chrono::milliseconds (20 ), attempt_id );
5155 return ;
5256 }
5357
5458 if (ec == ErrorCode::NoContent) {
55- checkThrottleThenReceive ();
59+ checkThrottleThenReceive (" " );
5660 return ;
5761 }
5862
5963 if (ec) {
60- SPDLOG_WARN (" Receive message from {} failed. Cause: {}. Retry after 1 second." , process_queue->simpleName (), ec.message ());
61- receiveMessageLater (std::chrono::seconds (1 ));
64+ SPDLOG_WARN (" Receive message from {} failed. Cause: {}. Retry after 1 second." , process_queue->simpleName (),
65+ ec.message ());
66+ receiveMessageLater (std::chrono::seconds (1 ), attempt_id);
6267 return ;
6368 }
6469
6570 SPDLOG_DEBUG (" Receive messages from broker[host={}] returns with status=FOUND, msgListSize={}, queue={}" ,
6671 result.source_host , result.messages .size (), process_queue->simpleName ());
6772 process_queue->accountCache (result.messages );
6873 consumer->getConsumeMessageService ()->dispatch (process_queue, result.messages );
69- checkThrottleThenReceive ();
74+ checkThrottleThenReceive (" " );
7075}
7176
7277const char * AsyncReceiveMessageCallback::RECEIVE_LATER_TASK_NAME = " receive-later-task" ;
7378
74- void AsyncReceiveMessageCallback::checkThrottleThenReceive () {
79+ void AsyncReceiveMessageCallback::checkThrottleThenReceive (std::string attempt_id ) {
7580 auto process_queue = process_queue_.lock ();
7681 if (!process_queue) {
7782 SPDLOG_WARN (" Process queue should have been destructed" );
@@ -82,14 +87,14 @@ void AsyncReceiveMessageCallback::checkThrottleThenReceive() {
8287 SPDLOG_INFO (" Number of messages in {} exceeds throttle threshold. Receive messages later." ,
8388 process_queue->simpleName ());
8489 process_queue->syncIdleState ();
85- receiveMessageLater (std::chrono::seconds (1 ));
90+ receiveMessageLater (std::chrono::seconds (1 ), attempt_id );
8691 } else {
8792 // Receive message immediately
88- receiveMessageImmediately ();
93+ receiveMessageImmediately (attempt_id );
8994 }
9095}
9196
92- void AsyncReceiveMessageCallback::receiveMessageLater (std::chrono::milliseconds delay) {
97+ void AsyncReceiveMessageCallback::receiveMessageLater (std::chrono::milliseconds delay, std::string& attempt_id ) {
9398 auto process_queue = process_queue_.lock ();
9499 if (!process_queue) {
95100 return ;
@@ -98,17 +103,18 @@ void AsyncReceiveMessageCallback::receiveMessageLater(std::chrono::milliseconds
98103 auto client_instance = process_queue->getClientManager ();
99104 std::weak_ptr<AsyncReceiveMessageCallback> receive_callback_weak_ptr (shared_from_this ());
100105
101- auto task = [receive_callback_weak_ptr]() {
106+ auto task = [receive_callback_weak_ptr, &attempt_id ]() {
102107 auto async_receive_ptr = receive_callback_weak_ptr.lock ();
103108 if (async_receive_ptr) {
104- async_receive_ptr->checkThrottleThenReceive ();
109+ async_receive_ptr->checkThrottleThenReceive (attempt_id );
105110 }
106111 };
107112
108- client_instance->getScheduler ()->schedule (task, RECEIVE_LATER_TASK_NAME, delay, std::chrono::seconds (0 ));
113+ client_instance->getScheduler ()->schedule (
114+ task, RECEIVE_LATER_TASK_NAME, delay, std::chrono::seconds (0 ));
109115}
110116
111- void AsyncReceiveMessageCallback::receiveMessageImmediately () {
117+ void AsyncReceiveMessageCallback::receiveMessageImmediately (std::string& attempt_id ) {
112118 auto process_queue_shared_ptr = process_queue_.lock ();
113119 if (!process_queue_shared_ptr) {
114120 SPDLOG_INFO (" ProcessQueue has been released. Ignore further receive message request-response cycles" );
@@ -121,7 +127,9 @@ void AsyncReceiveMessageCallback::receiveMessageImmediately() {
121127 process_queue_shared_ptr->simpleName ());
122128 return ;
123129 }
124- impl->receiveMessage (process_queue_shared_ptr->messageQueue (), process_queue_shared_ptr->getFilterExpression ());
130+
131+ impl->receiveMessage (process_queue_shared_ptr->messageQueue (),
132+ process_queue_shared_ptr->getFilterExpression (), attempt_id);
125133}
126134
127135ROCKETMQ_NAMESPACE_END
0 commit comments