Improve calculateRetryWaitMilliseconds (#63)

* improve calculateRetryWaitMilliseconds

* update comment
This commit is contained in:
Dimon4eg 2019-05-10 22:31:21 +03:00 committed by dimon4eg
parent 1778874ea8
commit 0bc1755f3a
2 changed files with 15 additions and 10 deletions

View File

@ -13,16 +13,21 @@
namespace 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 uint64_t wait_time;
// 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);
// cap the wait time to 10s, or to retry_count == 10 for which wait_time > 10s if (retry_count <= 6)
uint64_t tenSeconds = 10 * 1000; {
return (wait_time > tenSeconds || retry_count > 10) ? tenSeconds : wait_time; // 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() void WebSocket::reconnectPerpetuallyIfDisconnected()
{ {
uint64_t retries = 0; uint32_t retries = 0;
WebSocketErrorInfo connectErr; WebSocketErrorInfo connectErr;
ix::WebSocketInitResult status; ix::WebSocketInitResult status;
using millis = std::chrono::duration<double, std::milli>; using millis = std::chrono::duration<double, std::milli>;

View File

@ -12,7 +12,7 @@ namespace ix
{ {
struct WebSocketErrorInfo struct WebSocketErrorInfo
{ {
uint64_t retries; uint32_t retries;
double wait_time; double wait_time;
int http_status; int http_status;
std::string reason; std::string reason;