From 0bc1755f3a5e3ae5da9efc67a0c210a9472f1008 Mon Sep 17 00:00:00 2001 From: Dimon4eg Date: Fri, 10 May 2019 22:31:21 +0300 Subject: [PATCH] Improve calculateRetryWaitMilliseconds (#63) * improve calculateRetryWaitMilliseconds * update comment --- ixwebsocket/IXWebSocket.cpp | 23 ++++++++++++++--------- ixwebsocket/IXWebSocketErrorInfo.h | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index becc7b9d..40b1c1ad 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -13,16 +13,21 @@ namespace { - uint64_t calculateRetryWaitMilliseconds(uint64_t retry_count) + uint64_t calculateRetryWaitMilliseconds(uint32_t retry_count) { - // This will overflow quite fast for large value of retry_count - // and will become 0, in which case the wait time will be none - // and we'll be constantly retrying to connect. - uint64_t wait_time = ((uint64_t) std::pow(2, retry_count) * 100L); + uint64_t wait_time; - // cap the wait time to 10s, or to retry_count == 10 for which wait_time > 10s - uint64_t tenSeconds = 10 * 1000; - return (wait_time > tenSeconds || retry_count > 10) ? tenSeconds : wait_time; + if (retry_count <= 6) + { + // max wait_time is 6400 ms (2 ^ 6 = 64) + wait_time = ((uint64_t)std::pow(2, retry_count) * 100L); + } + else + { + wait_time = 10 * 1000; // 10 sec + } + + return wait_time; } } @@ -225,7 +230,7 @@ namespace ix void WebSocket::reconnectPerpetuallyIfDisconnected() { - uint64_t retries = 0; + uint32_t retries = 0; WebSocketErrorInfo connectErr; ix::WebSocketInitResult status; using millis = std::chrono::duration; diff --git a/ixwebsocket/IXWebSocketErrorInfo.h b/ixwebsocket/IXWebSocketErrorInfo.h index a8a02864..8b515fac 100644 --- a/ixwebsocket/IXWebSocketErrorInfo.h +++ b/ixwebsocket/IXWebSocketErrorInfo.h @@ -12,7 +12,7 @@ namespace ix { struct WebSocketErrorInfo { - uint64_t retries; + uint32_t retries; double wait_time; int http_status; std::string reason;