2017-12-27 17:58:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Poco/Timespan.h>
|
2019-03-22 12:08:30 +00:00
|
|
|
#include <Core/Settings.h>
|
2017-12-27 17:58:52 +00:00
|
|
|
|
2019-03-29 18:10:03 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
2017-12-27 17:58:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ConnectionTimeouts
|
|
|
|
{
|
|
|
|
Poco::Timespan connection_timeout;
|
|
|
|
Poco::Timespan send_timeout;
|
|
|
|
Poco::Timespan receive_timeout;
|
2018-10-22 23:02:57 +00:00
|
|
|
Poco::Timespan tcp_keep_alive_timeout;
|
2019-03-29 18:10:03 +00:00
|
|
|
Poco::Timespan http_keep_alive_timeout;
|
2017-12-27 17:58:52 +00:00
|
|
|
|
|
|
|
ConnectionTimeouts() = default;
|
|
|
|
|
2018-10-22 23:02:57 +00:00
|
|
|
ConnectionTimeouts(const Poco::Timespan & connection_timeout_,
|
|
|
|
const Poco::Timespan & send_timeout_,
|
|
|
|
const Poco::Timespan & receive_timeout_)
|
|
|
|
: connection_timeout(connection_timeout_),
|
|
|
|
send_timeout(send_timeout_),
|
|
|
|
receive_timeout(receive_timeout_),
|
2019-03-29 18:10:03 +00:00
|
|
|
tcp_keep_alive_timeout(0),
|
|
|
|
http_keep_alive_timeout(0)
|
2018-10-22 23:02:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-27 17:58:52 +00:00
|
|
|
ConnectionTimeouts(const Poco::Timespan & connection_timeout_,
|
|
|
|
const Poco::Timespan & send_timeout_,
|
2018-10-22 18:09:55 +00:00
|
|
|
const Poco::Timespan & receive_timeout_,
|
2018-10-22 23:02:57 +00:00
|
|
|
const Poco::Timespan & tcp_keep_alive_timeout_)
|
2017-12-27 17:58:52 +00:00
|
|
|
: connection_timeout(connection_timeout_),
|
|
|
|
send_timeout(send_timeout_),
|
2018-10-22 18:09:55 +00:00
|
|
|
receive_timeout(receive_timeout_),
|
2019-03-29 18:10:03 +00:00
|
|
|
tcp_keep_alive_timeout(tcp_keep_alive_timeout_),
|
|
|
|
http_keep_alive_timeout(0)
|
2017-12-27 17:58:52 +00:00
|
|
|
{
|
|
|
|
}
|
2019-03-29 18:10:03 +00:00
|
|
|
ConnectionTimeouts(const Poco::Timespan & connection_timeout_,
|
|
|
|
const Poco::Timespan & send_timeout_,
|
|
|
|
const Poco::Timespan & receive_timeout_,
|
|
|
|
const Poco::Timespan & tcp_keep_alive_timeout_,
|
|
|
|
const Poco::Timespan & http_keep_alive_timeout_)
|
|
|
|
: connection_timeout(connection_timeout_),
|
|
|
|
send_timeout(send_timeout_),
|
|
|
|
receive_timeout(receive_timeout_),
|
|
|
|
tcp_keep_alive_timeout(tcp_keep_alive_timeout_),
|
|
|
|
http_keep_alive_timeout(http_keep_alive_timeout_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-27 17:58:52 +00:00
|
|
|
|
|
|
|
static Poco::Timespan saturate(const Poco::Timespan & timespan, const Poco::Timespan & limit)
|
|
|
|
{
|
|
|
|
if (limit.totalMicroseconds() == 0)
|
|
|
|
return timespan;
|
|
|
|
else
|
|
|
|
return (timespan > limit) ? limit : timespan;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionTimeouts getSaturated(const Poco::Timespan & limit) const
|
|
|
|
{
|
|
|
|
return ConnectionTimeouts(saturate(connection_timeout, limit),
|
|
|
|
saturate(send_timeout, limit),
|
2018-10-22 18:09:55 +00:00
|
|
|
saturate(receive_timeout, limit),
|
2019-03-29 18:10:03 +00:00
|
|
|
saturate(tcp_keep_alive_timeout, limit),
|
|
|
|
saturate(http_keep_alive_timeout, limit));
|
2017-12-27 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 20:25:18 +00:00
|
|
|
/// Timeouts for the case when we have just single attempt to connect.
|
|
|
|
static ConnectionTimeouts getTCPTimeoutsWithoutFailover(const Settings & settings)
|
2017-12-27 17:58:52 +00:00
|
|
|
{
|
2018-10-22 23:02:57 +00:00
|
|
|
return ConnectionTimeouts(settings.connect_timeout, settings.send_timeout, settings.receive_timeout, settings.tcp_keep_alive_timeout);
|
2017-12-27 17:58:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 20:25:18 +00:00
|
|
|
/// Timeouts for the case when we will try many addresses in a loop.
|
|
|
|
static ConnectionTimeouts getTCPTimeoutsWithFailover(const Settings & settings)
|
|
|
|
{
|
2018-10-22 23:02:57 +00:00
|
|
|
return ConnectionTimeouts(settings.connect_timeout_with_failover_ms, settings.send_timeout, settings.receive_timeout, settings.tcp_keep_alive_timeout);
|
2018-03-12 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 18:10:03 +00:00
|
|
|
static ConnectionTimeouts getHTTPTimeouts(const Context & context)
|
2017-12-27 17:58:52 +00:00
|
|
|
{
|
2019-03-29 18:10:03 +00:00
|
|
|
const auto & settings = context.getSettingsRef();
|
|
|
|
const auto & config = context.getConfigRef();
|
|
|
|
Poco::Timespan http_keep_alive_timeout{config.getUInt("keep_alive_timeout", 10), 0};
|
|
|
|
return ConnectionTimeouts(settings.http_connection_timeout, settings.http_send_timeout, settings.http_receive_timeout, http_keep_alive_timeout);
|
2017-12-27 17:58:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|