diff --git a/src/common/MQClientErrorContainer.cpp b/src/common/MQClientErrorContainer.cpp index 2fac383a5..a9e0e5d31 100644 --- a/src/common/MQClientErrorContainer.cpp +++ b/src/common/MQClientErrorContainer.cpp @@ -14,25 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "MQClientErrorContainer.h" namespace rocketmq { -MQClientErrorContainer* MQClientErrorContainer::s_instance = nullptr; -MQClientErrorContainer* MQClientErrorContainer::instance() { - if (!s_instance) - s_instance = new MQClientErrorContainer(); - return s_instance; -} +thread_local std::string MQClientErrorContainer::t_err; -void MQClientErrorContainer::setErr(std::string str) { - boost::lock_guard lock(this->mutex); - this->m_err = str; +void MQClientErrorContainer::setErr(const std::string& str) { + t_err = str; } -std::string MQClientErrorContainer::getErr() { - boost::lock_guard lock(this->mutex); - return this->m_err; +const std::string& MQClientErrorContainer::getErr() { + return t_err; } + } // namespace rocketmq diff --git a/src/common/MQClientErrorContainer.h b/src/common/MQClientErrorContainer.h index 9c9240711..3e09f0a71 100644 --- a/src/common/MQClientErrorContainer.h +++ b/src/common/MQClientErrorContainer.h @@ -14,28 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef __MQ_CLIENT_ERROR_CONTAINER_H__ +#define __MQ_CLIENT_ERROR_CONTAINER_H__ -#ifndef __MQCLIENTERRORCONTAINER_H_INCLUDE__ -#define __MQCLIENTERRORCONTAINER_H_INCLUDE__ -#include -#include -#include +#include #include namespace rocketmq { + class MQClientErrorContainer { public: - static MQClientErrorContainer* instance(); - - void setErr(std::string str); - - std::string getErr(); + static void setErr(const std::string& str); + static const std::string& getErr(); private: - std::string m_err; - static MQClientErrorContainer* s_instance; - boost::mutex mutex; + static thread_local std::string t_err; }; + } // namespace rocketmq -#endif +#endif // __MQ_CLIENT_ERROR_CONTAINER_H__ diff --git a/src/extern/CErrorMessage.cpp b/src/extern/CErrorMessage.cpp index 1e858ade5..420fbe261 100644 --- a/src/extern/CErrorMessage.cpp +++ b/src/extern/CErrorMessage.cpp @@ -25,7 +25,7 @@ extern "C" { using namespace rocketmq; const char* GetLatestErrorMessage() { - return MQClientErrorContainer::instance()->getErr().c_str(); + return MQClientErrorContainer::getErr().c_str(); } #ifdef __cplusplus diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp index 792800db1..28b43d46c 100644 --- a/src/extern/CProducer.cpp +++ b/src/extern/CProducer.cpp @@ -100,7 +100,7 @@ int StartProducer(CProducer* producer) { try { ((DefaultMQProducer*)producer)->start(); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_START_FAILED; } return OK; @@ -156,7 +156,7 @@ int SendMessageSync(CProducer* producer, CMessage* msg, CSendResult* result) { strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1); result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0; } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_SEND_SYNC_FAILED; } return OK; @@ -219,7 +219,7 @@ int SendMessageAsync(CProducer* producer, delete cSendCallback; cSendCallback = NULL; } - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_SEND_ASYNC_FAILED; } return OK; @@ -249,7 +249,7 @@ int SendMessageOnewayOrderly(CProducer* producer, CMessage* msg, QueueSelectorCa SelectMessageQueue selectMessageQueue(selector); defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_SEND_ONEWAY_FAILED; } return OK; @@ -276,7 +276,7 @@ int SendMessageOrderlyAsync(CProducer* producer, } catch (exception& e) { printf("%s\n", e.what()); // std::count<setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_SEND_ORDERLYASYNC_FAILED; } return OK; @@ -303,7 +303,7 @@ int SendMessageOrderly(CProducer* producer, strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1); result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0; } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PRODUCER_SEND_ORDERLY_FAILED; } return OK; diff --git a/src/extern/CPullConsumer.cpp b/src/extern/CPullConsumer.cpp index 0e1c13b7a..198c11e7e 100644 --- a/src/extern/CPullConsumer.cpp +++ b/src/extern/CPullConsumer.cpp @@ -49,7 +49,7 @@ int StartPullConsumer(CPullConsumer* consumer) { try { ((DefaultMQPullConsumer*)consumer)->start(); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PULLCONSUMER_START_FAILED; } return OK; @@ -152,7 +152,7 @@ int FetchSubscriptionMessageQueues(CPullConsumer* consumer, const char* topic, C } catch (MQException& e) { *size = 0; *mqs = NULL; - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PULLCONSUMER_FETCH_MQ_FAILED; } return OK; @@ -177,7 +177,7 @@ CPullResult Pull(CPullConsumer* consumer, try { cppPullResult = ((DefaultMQPullConsumer*)consumer)->pull(messageQueue, subExpression, offset, maxNums); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); cppPullResult.pullStatus = BROKER_TIMEOUT; } @@ -232,7 +232,7 @@ int ReleasePullResult(CPullResult pullResult) { try { delete ((PullResult*)pullResult.pData); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return NULL_POINTER; } } diff --git a/src/extern/CPushConsumer.cpp b/src/extern/CPushConsumer.cpp index e45116bed..621eabde7 100644 --- a/src/extern/CPushConsumer.cpp +++ b/src/extern/CPushConsumer.cpp @@ -108,7 +108,7 @@ int StartPushConsumer(CPushConsumer* consumer) { try { ((DefaultMQPushConsumer*)consumer)->start(); } catch (exception& e) { - MQClientErrorContainer::instance()->setErr(string(e.what())); + MQClientErrorContainer::setErr(string(e.what())); return PUSHCONSUMER_START_FAILED; } return OK;