Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport(tcp): set timeout for blocking recv sockets #415

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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