Skip to content

Commit

Permalink
transport(tcp): set timeout for blocking recv sockets (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz authored Apr 12, 2024
1 parent 9f17c56 commit d7d6b99
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/faabric/transport/tcp/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <sys/socket.h>

namespace faabric::transport::tcp {

const int SocketTimeoutMs = 3000;

class Socket
{
public:
Expand Down
3 changes: 3 additions & 0 deletions include/faabric/transport/tcp/SocketOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ bool isNonBlocking(int connFd);

// Enable busy polling for non-blocking sockets
void setBusyPolling(int connFd);

// Set timeout for blocking sockets
void setTimeoutMs(int connFd, int timeoutMs);
}
4 changes: 4 additions & 0 deletions src/transport/tcp/RecvSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ void RecvSocket::setSocketOptions(int connFd)
// TODO: not clear if this helps or not
// setBusyPolling(connFd);
#else
// Set the socket as blocking
if (isNonBlocking(connFd)) {
setBlocking(connFd);
}

// Set the timeout
setTimeoutMs(connFd, SocketTimeoutMs);
#endif
}

Expand Down
16 changes: 16 additions & 0 deletions src/transport/tcp/SocketOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ void setBusyPolling(int connFd)
throw std::runtime_error("Error setting kernel busy poll");
}
}

void setTimeoutMs(int connFd, int timeoutMs)
{
struct timeval timeVal;
timeVal.tv_sec = timeoutMs / 1000;
timeVal.tv_usec = 0;

int ret = ::setsockopt(
connFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeVal, sizeof(timeVal));
if (ret == -1) {
SPDLOG_ERROR("Error setting recv timeout for socket {}: {}",
connFd,
std::strerror(errno));
throw std::runtime_error("Error setting recv timeout");
}
}
}
2 changes: 2 additions & 0 deletions tests/test/transport/test_tcp_sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ TEST_CASE("Test setting socket options", "[transport]")
setBusyPolling(conn);
setNonBlocking(conn);
setBlocking(conn);
setTimeoutMs(conn, SocketTimeoutMs);

REQUIRE(!isNonBlocking(conn));

Expand All @@ -87,6 +88,7 @@ TEST_CASE("Test setting socket options", "[transport]")
REQUIRE_THROWS(setBusyPolling(conn));
REQUIRE_THROWS(setNonBlocking(conn));
REQUIRE_THROWS(setBlocking(conn));
REQUIRE_THROWS(setTimeoutMs(conn, SocketTimeoutMs));
}

TEST_CASE("Test send/recv one message using raw TCP sockets", "[transport]")
Expand Down

0 comments on commit d7d6b99

Please sign in to comment.