2012-10-22 19:55:19 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
|
#include <statdaemons/PoolBase.h>
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Client/Connection.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
/** Интерфейс для пулов соединений.
|
2012-10-22 19:55:19 +00:00
|
|
|
|
*
|
2012-11-02 20:13:41 +00:00
|
|
|
|
* Использование (на примере обычного ConnectionPool):
|
|
|
|
|
* ConnectionPool pool(...);
|
2012-10-22 19:55:19 +00:00
|
|
|
|
*
|
2012-11-02 20:13:41 +00:00
|
|
|
|
* void thread()
|
|
|
|
|
* {
|
|
|
|
|
* sqxxl::Pool::Entry connection = pool.get();
|
|
|
|
|
* connection->sendQuery("SELECT 'Hello, world!' AS world");
|
|
|
|
|
* }
|
2012-10-22 19:55:19 +00:00
|
|
|
|
*/
|
2012-11-02 20:13:41 +00:00
|
|
|
|
class IConnectionPool : private boost::noncopyable
|
2012-10-22 19:55:19 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2014-06-03 14:32:04 +00:00
|
|
|
|
typedef PoolBase<Connection>::Entry Entry;
|
2014-04-06 23:15:27 +00:00
|
|
|
|
virtual Entry get(Settings * settings = nullptr) = 0;
|
2012-11-19 03:38:13 +00:00
|
|
|
|
virtual ~IConnectionPool() {}
|
2012-11-02 20:13:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef SharedPtr<IConnectionPool> ConnectionPoolPtr;
|
|
|
|
|
typedef std::vector<ConnectionPoolPtr> ConnectionPools;
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
|
|
|
|
|
2012-11-02 20:13:41 +00:00
|
|
|
|
|
|
|
|
|
/** Обычный пул соединений, без отказоустойчивости.
|
|
|
|
|
*/
|
2014-06-03 14:32:04 +00:00
|
|
|
|
class ConnectionPool : public PoolBase<Connection>, public IConnectionPool
|
2012-11-02 20:13:41 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2014-06-03 14:32:04 +00:00
|
|
|
|
typedef IConnectionPool::Entry Entry;
|
|
|
|
|
typedef PoolBase<Connection> Base;
|
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
|
ConnectionPool(unsigned max_connections_,
|
|
|
|
|
const String & host_, UInt16 port_, const String & default_database_,
|
2013-08-10 09:04:45 +00:00
|
|
|
|
const String & user_, const String & password_,
|
2012-10-22 19:55:19 +00:00
|
|
|
|
const DataTypeFactory & data_type_factory_,
|
|
|
|
|
const String & client_name_ = "client",
|
|
|
|
|
Protocol::Compression::Enum compression_ = Protocol::Compression::Enable,
|
|
|
|
|
Poco::Timespan connect_timeout_ = Poco::Timespan(DBMS_DEFAULT_CONNECT_TIMEOUT_SEC, 0),
|
|
|
|
|
Poco::Timespan receive_timeout_ = Poco::Timespan(DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC, 0),
|
|
|
|
|
Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0))
|
2014-06-03 15:23:03 +00:00
|
|
|
|
: Base(max_connections_, &Logger::get("ConnectionPool (" + Poco::Net::SocketAddress(host_, port_).toString() + ")")),
|
2014-06-03 14:32:04 +00:00
|
|
|
|
host(host_), port(port_), default_database(default_database_),
|
2013-08-10 09:04:45 +00:00
|
|
|
|
user(user_), password(password_),
|
|
|
|
|
client_name(client_name_), compression(compression_), data_type_factory(data_type_factory_),
|
2014-06-03 14:32:04 +00:00
|
|
|
|
connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_)
|
2012-10-22 19:55:19 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Выделяет соединение для работы. */
|
2014-06-03 14:32:04 +00:00
|
|
|
|
Entry get(Settings * settings = nullptr) override
|
2012-10-22 19:55:19 +00:00
|
|
|
|
{
|
2014-06-03 14:32:04 +00:00
|
|
|
|
if (settings)
|
|
|
|
|
return Base::get(settings->queue_max_wait_ms.totalMilliseconds());
|
|
|
|
|
else
|
|
|
|
|
return Base::get(-1);
|
2012-10-22 19:55:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-10 17:07:09 +00:00
|
|
|
|
const std::string & getHost() const
|
|
|
|
|
{
|
|
|
|
|
return host;
|
|
|
|
|
}
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
2014-06-03 14:32:04 +00:00
|
|
|
|
protected:
|
|
|
|
|
/** Создает новый объект для помещения в пул. */
|
|
|
|
|
ConnectionPtr allocObject() override
|
|
|
|
|
{
|
|
|
|
|
return new Connection(
|
|
|
|
|
host, port, default_database, user, password,
|
|
|
|
|
data_type_factory, client_name, compression,
|
|
|
|
|
connect_timeout, receive_timeout, send_timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2012-10-22 19:55:19 +00:00
|
|
|
|
String host;
|
|
|
|
|
UInt16 port;
|
|
|
|
|
String default_database;
|
2013-08-10 09:04:45 +00:00
|
|
|
|
String user;
|
|
|
|
|
String password;
|
2012-10-22 19:55:19 +00:00
|
|
|
|
|
|
|
|
|
String client_name;
|
|
|
|
|
Protocol::Compression::Enum compression; /// Сжимать ли данные при взаимодействии с сервером.
|
|
|
|
|
|
|
|
|
|
const DataTypeFactory & data_type_factory;
|
|
|
|
|
|
|
|
|
|
Poco::Timespan connect_timeout;
|
|
|
|
|
Poco::Timespan receive_timeout;
|
|
|
|
|
Poco::Timespan send_timeout;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|