Skip to content

Commit 34bcfab

Browse files
committed
net: move the constant maxWait out of InterruptibleRecv()
Move `maxWait` out of `InterruptibleRecv()` and rename it to `MAX_WAIT_FOR_IO` so that it can be reused by other code.
1 parent cff65c4 commit 34bcfab

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/netbase.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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 {

src/util/sock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
#define BITCOIN_UTIL_SOCK_H
77

88
#include <compat.h>
9+
#include <util/time.h>
910

1011
#include <chrono>
1112
#include <string>
1213

14+
/**
15+
* Maximum time to wait for I/O readiness.
16+
* It will take up until this time to break off in case of an interruption.
17+
*/
18+
static constexpr auto MAX_WAIT_FOR_IO = 1s;
19+
1320
/**
1421
* RAII helper class that manages a socket. Mimics `std::unique_ptr`, but instead of a pointer it
1522
* contains a socket and closes it automatically when it goes out of scope.

0 commit comments

Comments
 (0)