|
18 | 18 | */ |
19 | 19 | #include "HandlerBase.h" |
20 | 20 |
|
| 21 | +#include <chrono> |
| 22 | + |
| 23 | +#include "AsioDefines.h" |
21 | 24 | #include "Backoff.h" |
22 | 25 | #include "ClientConnection.h" |
23 | 26 | #include "ClientImpl.h" |
@@ -63,6 +66,7 @@ void HandlerBase::start() { |
63 | 66 | creationTimer_->async_wait([this, weakSelf](const ASIO_ERROR& error) { |
64 | 67 | auto self = weakSelf.lock(); |
65 | 68 | if (self && !error) { |
| 69 | + LOG_WARN("Cancel the pending reconnection due to the start timeout"); |
66 | 70 | connectionFailed(ResultTimeout); |
67 | 71 | ASIO_ERROR ignored; |
68 | 72 | timer_->cancel(ignored); |
@@ -118,13 +122,21 @@ void HandlerBase::grabCnx(const boost::optional<std::string>& assignedBrokerUrl) |
118 | 122 | } |
119 | 123 | auto self = shared_from_this(); |
120 | 124 | auto cnxFuture = getConnection(client, assignedBrokerUrl); |
121 | | - cnxFuture.addListener([this, self](Result result, const ClientConnectionPtr& cnx) { |
| 125 | + using namespace std::chrono; |
| 126 | + auto before = high_resolution_clock::now(); |
| 127 | + cnxFuture.addListener([this, self, before](Result result, const ClientConnectionPtr& cnx) { |
122 | 128 | if (result == ResultOk) { |
123 | | - LOG_DEBUG(getName() << "Connected to broker: " << cnx->cnxString()); |
124 | | - connectionOpened(cnx).addListener([this, self](Result result, bool) { |
| 129 | + connectionOpened(cnx).addListener([this, self, before](Result result, bool) { |
125 | 130 | // Do not use bool, only Result. |
126 | 131 | reconnectionPending_ = false; |
127 | | - if (result != ResultOk && isResultRetryable(result)) { |
| 132 | + if (result == ResultOk) { |
| 133 | + connectionTimeMs_ = |
| 134 | + duration_cast<milliseconds>(high_resolution_clock::now() - before).count(); |
| 135 | + // Prevent the creationTimer_ from cancelling the timer_ in future |
| 136 | + ASIO_ERROR ignored; |
| 137 | + creationTimer_->cancel(ignored); |
| 138 | + LOG_INFO("Finished connecting to broker after " << connectionTimeMs_ << " ms") |
| 139 | + } else if (isResultRetryable(result)) { |
128 | 140 | scheduleReconnection(); |
129 | 141 | } |
130 | 142 | }); |
@@ -194,8 +206,7 @@ void HandlerBase::scheduleReconnection(const boost::optional<std::string>& assig |
194 | 206 |
|
195 | 207 | void HandlerBase::handleTimeout(const ASIO_ERROR& ec, const boost::optional<std::string>& assignedBrokerUrl) { |
196 | 208 | if (ec) { |
197 | | - LOG_DEBUG(getName() << "Ignoring timer cancelled event, code[" << ec << "]"); |
198 | | - return; |
| 209 | + LOG_INFO(getName() << "Ignoring timer cancelled event, code[" << ec << "]"); |
199 | 210 | } else { |
200 | 211 | epoch_++; |
201 | 212 | grabCnx(assignedBrokerUrl); |
|
0 commit comments