1#include "TimeoutSetter.h"
2
3#include <common/logger_useful.h>
4
5namespace DB
6{
7TimeoutSetter::TimeoutSetter(Poco::Net::StreamSocket & socket_,
8 const Poco::Timespan & send_timeout_,
9 const Poco::Timespan & receive_timeout_,
10 bool limit_max_timeout)
11 : socket(socket_), send_timeout(send_timeout_), receive_timeout(receive_timeout_)
12{
13 old_send_timeout = socket.getSendTimeout();
14 old_receive_timeout = socket.getReceiveTimeout();
15
16 if (!limit_max_timeout || old_send_timeout > send_timeout)
17 socket.setSendTimeout(send_timeout);
18
19 if (!limit_max_timeout || old_receive_timeout > receive_timeout)
20 socket.setReceiveTimeout(receive_timeout);
21}
22
23TimeoutSetter::TimeoutSetter(Poco::Net::StreamSocket & socket_, const Poco::Timespan & timeout_, bool limit_max_timeout)
24 : TimeoutSetter(socket_, timeout_, timeout_, limit_max_timeout)
25{
26}
27
28TimeoutSetter::~TimeoutSetter()
29{
30 try
31 {
32 socket.setSendTimeout(old_send_timeout);
33 socket.setReceiveTimeout(old_receive_timeout);
34 }
35 catch (std::exception & e)
36 {
37 // Sometimes catched on macos
38 LOG_ERROR(&Logger::get("Client"), std::string{"TimeoutSetter: Can't reset timeouts: "} + e.what());
39 }
40}
41}
42