1 | #pragma once |
2 | |
3 | #include <Poco/Timespan.h> |
4 | #include <Core/Settings.h> |
5 | |
6 | #include <Interpreters/Context.h> |
7 | #include <Poco/Util/AbstractConfiguration.h> |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | struct ConnectionTimeouts |
13 | { |
14 | Poco::Timespan connection_timeout; |
15 | Poco::Timespan send_timeout; |
16 | Poco::Timespan receive_timeout; |
17 | Poco::Timespan tcp_keep_alive_timeout; |
18 | Poco::Timespan http_keep_alive_timeout; |
19 | |
20 | ConnectionTimeouts() = default; |
21 | |
22 | ConnectionTimeouts(const Poco::Timespan & connection_timeout_, |
23 | const Poco::Timespan & send_timeout_, |
24 | const Poco::Timespan & receive_timeout_) |
25 | : connection_timeout(connection_timeout_), |
26 | send_timeout(send_timeout_), |
27 | receive_timeout(receive_timeout_), |
28 | tcp_keep_alive_timeout(0), |
29 | http_keep_alive_timeout(0) |
30 | { |
31 | } |
32 | |
33 | ConnectionTimeouts(const Poco::Timespan & connection_timeout_, |
34 | const Poco::Timespan & send_timeout_, |
35 | const Poco::Timespan & receive_timeout_, |
36 | const Poco::Timespan & tcp_keep_alive_timeout_) |
37 | : connection_timeout(connection_timeout_), |
38 | send_timeout(send_timeout_), |
39 | receive_timeout(receive_timeout_), |
40 | tcp_keep_alive_timeout(tcp_keep_alive_timeout_), |
41 | http_keep_alive_timeout(0) |
42 | { |
43 | } |
44 | ConnectionTimeouts(const Poco::Timespan & connection_timeout_, |
45 | const Poco::Timespan & send_timeout_, |
46 | const Poco::Timespan & receive_timeout_, |
47 | const Poco::Timespan & tcp_keep_alive_timeout_, |
48 | const Poco::Timespan & http_keep_alive_timeout_) |
49 | : connection_timeout(connection_timeout_), |
50 | send_timeout(send_timeout_), |
51 | receive_timeout(receive_timeout_), |
52 | tcp_keep_alive_timeout(tcp_keep_alive_timeout_), |
53 | http_keep_alive_timeout(http_keep_alive_timeout_) |
54 | { |
55 | } |
56 | |
57 | |
58 | static Poco::Timespan saturate(const Poco::Timespan & timespan, const Poco::Timespan & limit) |
59 | { |
60 | if (limit.totalMicroseconds() == 0) |
61 | return timespan; |
62 | else |
63 | return (timespan > limit) ? limit : timespan; |
64 | } |
65 | |
66 | ConnectionTimeouts getSaturated(const Poco::Timespan & limit) const |
67 | { |
68 | return ConnectionTimeouts(saturate(connection_timeout, limit), |
69 | saturate(send_timeout, limit), |
70 | saturate(receive_timeout, limit), |
71 | saturate(tcp_keep_alive_timeout, limit), |
72 | saturate(http_keep_alive_timeout, limit)); |
73 | } |
74 | |
75 | /// Timeouts for the case when we have just single attempt to connect. |
76 | static ConnectionTimeouts getTCPTimeoutsWithoutFailover(const Settings & settings) |
77 | { |
78 | return ConnectionTimeouts(settings.connect_timeout, settings.send_timeout, settings.receive_timeout, settings.tcp_keep_alive_timeout); |
79 | } |
80 | |
81 | /// Timeouts for the case when we will try many addresses in a loop. |
82 | static ConnectionTimeouts getTCPTimeoutsWithFailover(const Settings & settings) |
83 | { |
84 | return ConnectionTimeouts(settings.connect_timeout_with_failover_ms, settings.send_timeout, settings.receive_timeout, settings.tcp_keep_alive_timeout); |
85 | } |
86 | |
87 | static ConnectionTimeouts getHTTPTimeouts(const Context & context) |
88 | { |
89 | const auto & settings = context.getSettingsRef(); |
90 | const auto & config = context.getConfigRef(); |
91 | Poco::Timespan http_keep_alive_timeout{config.getUInt("keep_alive_timeout" , 10), 0}; |
92 | return ConnectionTimeouts(settings.http_connection_timeout, settings.http_send_timeout, settings.http_receive_timeout, settings.tcp_keep_alive_timeout, http_keep_alive_timeout); |
93 | } |
94 | }; |
95 | |
96 | } |
97 | |