Skip to content

Commit 3456ee1

Browse files
authored
Merge pull request #217 from 1261385937/master
fix pr #205
2 parents a63e28e + 9e3491f commit 3456ee1

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

clickhouse/base/socket.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,25 @@ void SetNonBlock(SOCKET fd, bool value) {
114114

115115
void SetTimeout(SOCKET fd, const SocketTimeoutParams& timeout_params) {
116116
#if defined(_unix_)
117-
timeval recv_timeout { .tv_sec = timeout_params.recv_timeout.count(), .tv_usec = 0 };
118-
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
117+
timeval recv_timeout{ timeout_params.recv_timeout.count() / 1000, static_cast<int>(timeout_params.recv_timeout.count() % 1000 * 1000) };
118+
auto recv_ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
119119

120-
timeval send_timeout { .tv_sec = timeout_params.send_timeout.count(), .tv_usec = 0 };
121-
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &send_timeout, sizeof(send_timeout));
120+
timeval send_timeout{ timeout_params.send_timeout.count() / 1000, static_cast<int>(timeout_params.send_timeout.count() % 1000 * 1000) };
121+
auto send_ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &send_timeout, sizeof(send_timeout));
122+
123+
if (recv_ret == -1 || send_ret == -1) {
124+
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to set socket timeout");
125+
}
126+
#else
127+
DWORD recv_timeout = static_cast<DWORD>(timeout_params.recv_timeout.count());
128+
auto recv_ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&recv_timeout, sizeof(DWORD));
129+
130+
DWORD send_timeout = static_cast<DWORD>(timeout_params.send_timeout.count());
131+
auto send_ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&send_timeout, sizeof(DWORD));
132+
133+
if (recv_ret == SOCKET_ERROR || send_ret == SOCKET_ERROR) {
134+
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to set socket timeout");
135+
}
122136
#endif
123137
};
124138

@@ -244,6 +258,10 @@ Socket::Socket(const NetworkAddress& addr, const SocketTimeoutParams& timeout_pa
244258
: handle_(SocketConnect(addr, timeout_params))
245259
{}
246260

261+
Socket::Socket(const NetworkAddress & addr)
262+
: handle_(SocketConnect(addr, SocketTimeoutParams{}))
263+
{}
264+
247265
Socket::Socket(Socket&& other) noexcept
248266
: handle_(other.handle_)
249267
{

clickhouse/base/socket.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ class SocketFactory {
8383

8484

8585
struct SocketTimeoutParams {
86-
const std::chrono::seconds recv_timeout {0};
87-
const std::chrono::seconds send_timeout {0};
86+
std::chrono::milliseconds recv_timeout{ 0 };
87+
std::chrono::milliseconds send_timeout{ 0 };
8888
};
8989

9090
class Socket : public SocketBase {
9191
public:
9292
Socket(const NetworkAddress& addr, const SocketTimeoutParams& timeout_params);
93+
Socket(const NetworkAddress& addr);
9394
Socket(Socket&& other) noexcept;
9495
Socket& operator=(Socket&& other) noexcept;
9596

ut/socket_ut.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ TEST(Socketcase, connecterror) {
1818

1919
std::this_thread::sleep_for(std::chrono::seconds(1));
2020
try {
21-
Socket socket(addr, SocketTimeoutParams {});
21+
Socket socket(addr);
2222
} catch (const std::system_error& e) {
2323
FAIL();
2424
}
2525

2626
std::this_thread::sleep_for(std::chrono::seconds(1));
2727
server.stop();
2828
try {
29-
Socket socket(addr, SocketTimeoutParams {});
29+
Socket socket(addr);
3030
FAIL();
3131
} catch (const std::system_error& e) {
3232
ASSERT_NE(EINPROGRESS,e.code().value());
@@ -43,14 +43,20 @@ TEST(Socketcase, timeoutrecv) {
4343

4444
std::this_thread::sleep_for(std::chrono::seconds(1));
4545
try {
46-
Socket socket(addr, SocketTimeoutParams { .recv_timeout = Seconds(5), .send_timeout = Seconds(5) });
46+
Socket socket(addr, SocketTimeoutParams { Seconds(5), Seconds(5) });
4747

4848
std::unique_ptr<InputStream> ptr_input_stream = socket.makeInputStream();
4949
char buf[1024];
5050
ptr_input_stream->Read(buf, sizeof(buf));
5151

52-
} catch (const std::system_error& e) {
53-
ASSERT_EQ(EAGAIN, e.code().value());
52+
}
53+
catch (const std::system_error& e) {
54+
#if defined(_unix_)
55+
auto expected = EAGAIN;
56+
#else
57+
auto expected = WSAETIMEDOUT;
58+
#endif
59+
ASSERT_EQ(expected, e.code().value());
5460
}
5561

5662
std::this_thread::sleep_for(std::chrono::seconds(1));

0 commit comments

Comments
 (0)