1414#include < util/time.h>
1515
1616#include < atomic>
17+ #include < chrono>
1718#include < cstdint>
1819#include < functional>
1920#include < limits>
@@ -360,9 +361,6 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
360361{
361362 int64_t curTime = GetTimeMillis ();
362363 int64_t endTime = curTime + timeout;
363- // Maximum time to wait for I/O readiness. It will take up until this time
364- // (in millis) to break off in case of an interruption.
365- const int64_t maxWait = 1000 ;
366364 while (len > 0 && curTime < endTime) {
367365 ssize_t ret = sock.Recv (data, len, 0 ); // Optimistically try the recv first
368366 if (ret > 0 ) {
@@ -373,10 +371,11 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
373371 } else { // Other error or blocking
374372 int nErr = WSAGetLastError ();
375373 if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
376- // Only wait at most maxWait milliseconds at a time, unless
374+ // Only wait at most MAX_WAIT_FOR_IO at a time, unless
377375 // we're approaching the end of the specified total timeout
378- int timeout_ms = std::min (endTime - curTime, maxWait);
379- if (!sock.Wait (std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
376+ const auto remaining = std::chrono::milliseconds{endTime - curTime};
377+ const auto timeout = std::min (remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
378+ if (!sock.Wait (timeout, Sock::RECV)) {
380379 return IntrRecvError::NetworkError;
381380 }
382381 } else {
0 commit comments