@@ -183,7 +183,7 @@ NetworkAddress::NetworkAddress(const std::string& host, const std::string& port)
183
183
hints.ai_socktype = SOCK_STREAM;
184
184
// using AI_ADDRCONFIG on windows will cause getaddrinfo to return WSAHOST_NOT_FOUND
185
185
// for more information, see https://github.com/ClickHouse/clickhouse-cpp/issues/195
186
- #if defined(_unix_)
186
+ #if defined(_unix_)
187
187
if (!Singleton<LocalNames>()->IsLocalName (host)) {
188
188
// https://linux.die.net/man/3/getaddrinfo
189
189
// If hints.ai_flags includes the AI_ADDRCONFIG flag,
@@ -292,6 +292,17 @@ void Socket::SetTcpNoDelay(bool nodelay) noexcept {
292
292
#endif
293
293
}
294
294
295
+ void Socket::SetSocketTimeoutTime (unsigned int time) noexcept {
296
+ struct timeval time_val = {time , 0 };
297
+ #if defined(_unix_) && defined(_linux_)
298
+ setsockopt (handle_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time_val, sizeof time_val);
299
+ #elif defined(_darwin_)
300
+ setsockopt (handle_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time_val, sizeof time_val);
301
+ #elif defined(_unix_)
302
+ // setsockopt not support SO_RCVTIMEO in BSD.
303
+ #endif
304
+ }
305
+
295
306
std::unique_ptr<InputStream> Socket::makeInputStream () const {
296
307
return std::make_unique<SocketInput>(handle_);
297
308
}
@@ -325,6 +336,9 @@ void NonSecureSocketFactory::setSocketOptions(Socket &socket, const ClientOption
325
336
if (opts.tcp_nodelay ) {
326
337
socket.SetTcpNoDelay (opts.tcp_nodelay );
327
338
}
339
+ if (opts.socket_timeout_sec ) {
340
+ socket.SetSocketTimeoutTime (opts_.socket_timeout_sec );
341
+ }
328
342
}
329
343
330
344
SocketInput::SocketInput (SOCKET s)
0 commit comments