aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/TimeoutSetter.cpp
blob: b8b7a81470385641b4aca5a93e9b193b05316299 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <IO/TimeoutSetter.h>

#include <Common/logger_useful.h>


namespace DB
{

TimeoutSetter::TimeoutSetter(Poco::Net::StreamSocket & socket_,
    Poco::Timespan send_timeout_,
    Poco::Timespan receive_timeout_,
    bool limit_max_timeout)
    : socket(socket_), send_timeout(send_timeout_), receive_timeout(receive_timeout_)
{
    old_send_timeout = socket.getSendTimeout();
    old_receive_timeout = socket.getReceiveTimeout();

    if (!limit_max_timeout || old_send_timeout > send_timeout)
        socket.setSendTimeout(send_timeout);

    if (!limit_max_timeout || old_receive_timeout > receive_timeout)
        socket.setReceiveTimeout(receive_timeout);
}

TimeoutSetter::TimeoutSetter(Poco::Net::StreamSocket & socket_, Poco::Timespan timeout_, bool limit_max_timeout)
    : TimeoutSetter(socket_, timeout_, timeout_, limit_max_timeout)
{
}

TimeoutSetter::~TimeoutSetter()
{
    if (was_reset)
        return;

    try
    {
        reset();
    }
    catch (...)
    {
        tryLogCurrentException("Client", "TimeoutSetter: Can't reset timeouts");
    }
}

void TimeoutSetter::reset()
{
    bool connected = socket.impl()->initialized();
    if (!connected)
        return;

    socket.setSendTimeout(old_send_timeout);
    socket.setReceiveTimeout(old_receive_timeout);
    was_reset = true;
}

}