Commit 5c834f640072f8ed9ea2311c0b8718ffdaf6a33e
1 parent
0647a241
fix(connection): there is no back-off for resolving that fails
In results, it was trying to resolve very often.
Showing
2 changed files
with
9 additions
and
5 deletions
src/Connection.cpp
| @@ -69,7 +69,10 @@ void TrueMQTT::Client::Impl::Connection::runRead() | @@ -69,7 +69,10 @@ void TrueMQTT::Client::Impl::Connection::runRead() | ||
| 69 | switch (m_state) | 69 | switch (m_state) |
| 70 | { | 70 | { |
| 71 | case State::RESOLVING: | 71 | case State::RESOLVING: |
| 72 | - resolve(); | 72 | + if (!resolve()) |
| 73 | + { | ||
| 74 | + m_state = State::BACKOFF; | ||
| 75 | + } | ||
| 73 | break; | 76 | break; |
| 74 | 77 | ||
| 75 | case State::CONNECTING: | 78 | case State::CONNECTING: |
| @@ -192,7 +195,7 @@ void TrueMQTT::Client::Impl::Connection::socketError() | @@ -192,7 +195,7 @@ void TrueMQTT::Client::Impl::Connection::socketError() | ||
| 192 | } | 195 | } |
| 193 | } | 196 | } |
| 194 | 197 | ||
| 195 | -void TrueMQTT::Client::Impl::Connection::resolve() | 198 | +bool TrueMQTT::Client::Impl::Connection::resolve() |
| 196 | { | 199 | { |
| 197 | m_address_current = 0; | 200 | m_address_current = 0; |
| 198 | m_socket = INVALID_SOCKET; | 201 | m_socket = INVALID_SOCKET; |
| @@ -218,7 +221,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | @@ -218,7 +221,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | ||
| 218 | if (error != 0) | 221 | if (error != 0) |
| 219 | { | 222 | { |
| 220 | m_impl.m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, std::string_view(gai_strerror(error))); | 223 | m_impl.m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, std::string_view(gai_strerror(error))); |
| 221 | - return; | 224 | + return false; |
| 222 | } | 225 | } |
| 223 | 226 | ||
| 224 | // Split the list of addresses in two lists, one for IPv4 and one for | 227 | // Split the list of addresses in two lists, one for IPv4 and one for |
| @@ -274,7 +277,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | @@ -274,7 +277,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | ||
| 274 | if (m_addresses.empty()) | 277 | if (m_addresses.empty()) |
| 275 | { | 278 | { |
| 276 | m_impl.m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, ""); | 279 | m_impl.m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, ""); |
| 277 | - return; | 280 | + return false; |
| 278 | } | 281 | } |
| 279 | 282 | ||
| 280 | // Only change the state if no disconnect() has been requested in the mean time. | 283 | // Only change the state if no disconnect() has been requested in the mean time. |
| @@ -282,6 +285,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | @@ -282,6 +285,7 @@ void TrueMQTT::Client::Impl::Connection::resolve() | ||
| 282 | { | 285 | { |
| 283 | m_state = State::CONNECTING; | 286 | m_state = State::CONNECTING; |
| 284 | } | 287 | } |
| 288 | + return true; | ||
| 285 | } | 289 | } |
| 286 | 290 | ||
| 287 | bool TrueMQTT::Client::Impl::Connection::connectToAny() | 291 | bool TrueMQTT::Client::Impl::Connection::connectToAny() |
src/Connection.h
| @@ -39,7 +39,7 @@ private: | @@ -39,7 +39,7 @@ private: | ||
| 39 | // Implemented in Connection.cpp | 39 | // Implemented in Connection.cpp |
| 40 | void runRead(); | 40 | void runRead(); |
| 41 | void runWrite(); | 41 | void runWrite(); |
| 42 | - void resolve(); | 42 | + bool resolve(); |
| 43 | bool tryNextAddress(); | 43 | bool tryNextAddress(); |
| 44 | void connect(addrinfo *address); | 44 | void connect(addrinfo *address); |
| 45 | bool connectToAny(); | 45 | bool connectToAny(); |