Repository navigation
[C++] Update public API and simplify client lifecycle state machine - #1300
Conversation
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot
Summary
This PR updates the C++ client public API for consistency (move semantics, sink parameters, noexcept) and simplifies the client lifecycle state machine from 5 states to 3 (CREATED β STARTED β STOPPED).
Findings
-
[Info]
cpp/source/rocketmq/ClientImpl.cppβ The state machine simplification is well-structured. Theshutdown_requested_flag checked at the end ofstart()correctly handles the race where shutdown is requested during startup. Good defensive design. -
[Info]
cpp/source/rocketmq/FifoProducerPartition.cppβ The lock-scope reduction intrySend()(releasingmessages_mtx_before callingproducer_->send()) correctly prevents the deadlock scenario described in the comment. The exception handler that invokes the callback withoperation_canceledis appropriate. -
[Warning]
cpp/source/rocketmq/ClientImpl.cpp:~110β The comment acknowledges that if an exception is thrown during resource allocation instart(), partially-allocated resources may leak sinceshutdown()becomes a no-op when state is stillCREATED. Consider adding RAII guards or a cleanup path for theSTARTEDβ resource allocation failure case to prevent leaks in error scenarios. -
[Info]
cpp/source/client/ReceiveMessageStreamReader.cppβ Theweak_ptrcheck before use is a good fix preventing use-after-free whenClientManagerhas been destructed. -
[Info]
cpp/source/client/RpcClientImpl.cppβ Separating the null check from the channel state check inok()is a clean defensive improvement. -
[Info]
cpp/include/rocketmq/SendCallback.hβ Changing fromconst SendReceipt&toSendReceipt&&(move semantics) is correct sinceSendReceiptcontains aMessageConstSharedPtrthat can be moved efficiently.
Suggestions
-
Resource leak on start() failure: The acknowledged leak in
ClientImpl::start()when exceptions occur during resource allocation could be addressed with a scope guard pattern:// In start(), after resource allocation begins: auto cleanup_on_failure = absl::MakeCleanup([&] { if (state_.load(std::memory_order_relaxed) == State::CREATED) { // Partial cleanup for resources allocated so far } });
-
Consider documenting the noexcept contract: Since several public API methods now swallow exceptions internally (e.g.,
PushConsumer::subscribe,SimpleConsumer::asyncReceive), consider adding@notein the header documentation explaining that exceptions are caught and logged, so callers should check error codes rather than expecting exceptions.
Cross-repo Note
This PR modifies the C++ client API (SendCallback signature change). If the Java/Go/C++ clients share a common gRPC protocol definition, ensure the protocol layer is unaffected. The changes here are purely client-side API ergonomics and should not require proxy-side changes.
Automated review by github-manager-bot
Summary
Update the C++ client public API for consistency and simplify the client lifecycle state machine from 5 states to 3 states (CREATED β STARTED β STOPPED).
Changes
Public API
SendCallback: change signature fromconst SendReceipt&toSendReceipt&&(move semantics)Producer/FifoProducer: sink parameter pattern forMessagePushConsumer: addnoexcepttoshutdown(), swallow exceptions in destructorSimpleConsumer:noexceptshutdown, API updatesLifecycle State Machine
CREATED β STARTED β STOPPEDClientImpl::start()andClientImpl::shutdown()remain virtual β subclasses override with explicit base class callsProducerImpl,PushConsumerImpl,SimpleConsumerImpl) manages its own state transition withcompare_exchange_strongsession_map_.clear()added toClientImpl::shutdown()beforeclient_manager_.reset()to prevent use-after-freeInternal Improvements
RpcClientImpl:need_heartbeat_changed tostd::atomic<bool>ReceiveMessageStreamReader: addweak_ptrcheck before useClientManagerMock: updated for new API signaturesTesting