Commit 81c477905dded5e810e62b1c5afd56986f8c256e
1 parent
c163e9a4
fix(connection): check every 100ms, not every 100us, for an update
Doing it every 100us consumes a lot of CPU, and was not the intention. Additionally, increase the read interval to 100ms, as 10ms is using quite a lot of CPU for not really a good reason other than reacting quickly on error situations. That really is not the right balance to strike.
Showing
2 changed files
with
3 additions
and
5 deletions
src/Connection.cpp
| ... | ... | @@ -283,9 +283,7 @@ bool TrueMQTT::Client::Impl::Connection::connectToAny() |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | // Check for at most 100ms if there is any activity on the sockets. |
| 286 | - timeval timeout; | |
| 287 | - timeout.tv_sec = 0; | |
| 288 | - timeout.tv_usec = 100; | |
| 286 | + timeval timeout = {0, 100 * 1000}; | |
| 289 | 287 | |
| 290 | 288 | fd_set write_fds; |
| 291 | 289 | FD_ZERO(&write_fds); | ... | ... |
src/Packet.cpp
| ... | ... | @@ -16,7 +16,7 @@ |
| 16 | 16 | |
| 17 | 17 | ssize_t TrueMQTT::Client::Impl::Connection::recv(char *buffer, size_t length) const |
| 18 | 18 | { |
| 19 | - // We idle-check every 10ms if we are requested to stop or if there was | |
| 19 | + // We idle-check every 100ms if we are requested to stop or if there was | |
| 20 | 20 | // an error. This is to prevent the recv() call from blocking forever. |
| 21 | 21 | while (true) |
| 22 | 22 | { |
| ... | ... | @@ -24,7 +24,7 @@ ssize_t TrueMQTT::Client::Impl::Connection::recv(char *buffer, size_t length) co |
| 24 | 24 | fd_set read_fds; |
| 25 | 25 | FD_ZERO(&read_fds); |
| 26 | 26 | FD_SET(m_socket, &read_fds); |
| 27 | - timeval timeout = {0, 10}; | |
| 27 | + timeval timeout = {0, 100 * 1000}; | |
| 28 | 28 | size_t ret = select(m_socket + 1, &read_fds, nullptr, nullptr, &timeout); |
| 29 | 29 | |
| 30 | 30 | if (m_state == State::SOCKET_ERROR || m_state == State::STOP) | ... | ... |