Skip to content

Commit

Permalink
Use timeval to set up SO_RCVTIMEO (dmlc#1734)
Browse files Browse the repository at this point in the history
Refactor code to use timeval struct
for setting up the SO_RCVTIMEO variable
timeout value for sockets in case of Linux.
Changed SetTimeout API timeout input to seconds.

Co-authored-by: Chao Ma <[email protected]>
  • Loading branch information
mszarma and aksnzhy authored Jul 3, 2020
1 parent 4ddd477 commit f80fc9b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/rpc/network/socket_communicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ bool SocketReceiver::Wait(const char* addr, int num_sender) {
}
// Initialize socket and socket-thread
server_socket_ = new TCPSocket();
server_socket_->SetTimeout(kTimeOut * 60 * 1000); // millsec
server_socket_->SetTimeout(kTimeOut); // seconds
// Bind socket
if (server_socket_->Bind(ip.c_str(), port) == false) {
LOG(FATAL) << "Cannot bind to " << ip << ":" << port;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/network/socket_communicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace dgl {
namespace network {

static constexpr int kMaxTryCount = 1024; // maximal connection: 1024
static constexpr int kTimeOut = 10; // 10 minutes for socket timeout
static constexpr int kTimeOut = 10 * 60; // 10 minutes (in seconds) for socket timeout
static constexpr int kMaxConnection = 1024; // maximal connection: 1024

/*!
Expand Down
13 changes: 11 additions & 2 deletions src/rpc/network/tcp_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,17 @@ bool TCPSocket::SetBlocking(bool flag) {
#endif // _WIN32

void TCPSocket::SetTimeout(int timeout) {
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<char*>(&timeout), sizeof(timeout));
#ifdef _WIN32
timeout = timeout * 1000; // WIN API accepts millsec
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<char*>(&timeout), sizeof(timeout));
#else // !_WIN32
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(tv));
#endif // _WIN32
}

bool TCPSocket::ShutDown(int ways) {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/network/tcp_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TCPSocket {

/*!
* \brief Set timeout for socket
* \param timeout millsec timeout
* \param timeout seconds timeout
*/
void SetTimeout(int timeout);

Expand Down

0 comments on commit f80fc9b

Please sign in to comment.