diff options
author | Razvan Becheriu <razvan@isc.org> | 2021-03-17 15:28:01 +0100 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2021-03-29 20:10:52 +0200 |
commit | b92b67c53cfbb472d417567a23d77018d7122747 (patch) | |
tree | 7920ea9a0b9e7e5bbfbae28c7c1cdc10336d4b94 /src/lib | |
parent | [#1621] fixed disable network state for connections (diff) | |
download | kea-b92b67c53cfbb472d417567a23d77018d7122747.tar.xz kea-b92b67c53cfbb472d417567a23d77018d7122747.zip |
[#1621] add lazy retrieval for connection IOService
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/database/database_connection.h | 8 | ||||
-rw-r--r-- | src/lib/dhcpsrv/mysql_lease_mgr.cc | 8 | ||||
-rw-r--r-- | src/lib/dhcpsrv/mysql_lease_mgr.h | 4 | ||||
-rw-r--r-- | src/lib/dhcpsrv/pgsql_lease_mgr.cc | 8 | ||||
-rw-r--r-- | src/lib/dhcpsrv/pgsql_lease_mgr.h | 4 | ||||
-rw-r--r-- | src/lib/mysql/mysql_connection.h | 16 | ||||
-rw-r--r-- | src/lib/pgsql/pgsql_connection.h | 16 |
7 files changed, 44 insertions, 20 deletions
diff --git a/src/lib/database/database_connection.h b/src/lib/database/database_connection.h index 6ddefb2972..1dd6eb7c4f 100644 --- a/src/lib/database/database_connection.h +++ b/src/lib/database/database_connection.h @@ -7,6 +7,7 @@ #ifndef DATABASE_CONNECTION_H #define DATABASE_CONNECTION_H +#include <asiolink/io_service.h> #include <cc/data.h> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> @@ -160,6 +161,13 @@ typedef boost::shared_ptr<ReconnectCtl> ReconnectCtlPtr; /// @brief Defines a callback prototype for propagating events upward typedef std::function<bool (ReconnectCtlPtr db_reconnect_ctl)> DbCallback; +/// @brief Callback which returns the IOService that can be used to recover the +/// connection. +typedef std::function<isc::asiolink::IOServicePtr ()> IOServiceAccessCallback; + +/// @brief Pointer to an instance of IOServiceAccessCallbackPtr +typedef boost::shared_ptr<IOServiceAccessCallback> IOServiceAccessCallbackPtr; + /// @brief Common database connection class. /// /// This class provides functions that are common for establishing diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 4e8e78ea0e..f1065c0797 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -1737,9 +1737,9 @@ bool MySqlLeaseStatsQuery::negative_count_ = false; // MySqlLeaseContext Constructor MySqlLeaseContext::MySqlLeaseContext(const DatabaseConnection::ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service, + IOServiceAccessCallbackPtr io_service_access_callback, DbCallback db_reconnect_callback) - : conn_(parameters, io_service, db_reconnect_callback) { + : conn_(parameters, io_service_access_callback, db_reconnect_callback) { } // MySqlLeaseContextAlloc Constructor and Destructor @@ -1882,8 +1882,8 @@ MySqlLeaseMgr::dbReconnect(ReconnectCtlPtr db_reconnect_ctl) { MySqlLeaseContextPtr MySqlLeaseMgr::createContext() const { MySqlLeaseContextPtr ctx(new MySqlLeaseContext(parameters_, - LeaseMgr::getIOService(), - &MySqlLeaseMgr::dbReconnect)); + IOServiceAccessCallbackPtr(new IOServiceAccessCallback(&LeaseMgr::getIOService)), + &MySqlLeaseMgr::dbReconnect)); // Open the database. ctx->conn_.openDatabase(); diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 99709c107d..6b428e4560 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -44,10 +44,10 @@ public: /// @brief Constructor /// /// @param parameters See MySqlLeaseMgr constructor. - /// @param io_service The IOService object, used for all ASIO operations. + /// @param io_service_access_callback The IOService access callback. /// @param db_reconnect_callback The connection recovery callback. MySqlLeaseContext(const db::DatabaseConnection::ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service, + db::IOServiceAccessCallbackPtr io_service_access_callback, db::DbCallback db_reconnect_callback); /// The exchange objects are used for transfer of data to/from the database. diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index 4572d7eb39..da39f2d973 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -1171,9 +1171,9 @@ bool PgSqlLeaseStatsQuery::negative_count_ = false; // PgSqlLeaseContext Constructor PgSqlLeaseContext::PgSqlLeaseContext(const DatabaseConnection::ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service, + IOServiceAccessCallbackPtr io_service_access_callback, DbCallback db_reconnect_callback) - : conn_(parameters, io_service, db_reconnect_callback) { + : conn_(parameters, io_service_access_callback, db_reconnect_callback) { } // PgSqlLeaseContextAlloc Constructor and Destructor @@ -1316,8 +1316,8 @@ PgSqlLeaseMgr::dbReconnect(ReconnectCtlPtr db_reconnect_ctl) { PgSqlLeaseContextPtr PgSqlLeaseMgr::createContext() const { PgSqlLeaseContextPtr ctx(new PgSqlLeaseContext(parameters_, - LeaseMgr::getIOService(), - &PgSqlLeaseMgr::dbReconnect)); + IOServiceAccessCallbackPtr(new IOServiceAccessCallback(&LeaseMgr::getIOService)), + &PgSqlLeaseMgr::dbReconnect)); // Open the database. ctx->conn_.openDatabase(); diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index ad2057e34c..f420058954 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -43,10 +43,10 @@ public: /// @brief Constructor /// /// @param parameters See PgSqlLeaseMgr constructor. - /// @param io_service The IOService object, used for all ASIO operations. + /// @param io_service_access_callback The IOService access callback. /// @param db_reconnect_callback The connection recovery callback. PgSqlLeaseContext(const db::DatabaseConnection::ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service, + db::IOServiceAccessCallbackPtr io_service_access_callback, db::DbCallback db_reconnect_callback); /// The exchange objects are used for transfer of data to/from the database. diff --git a/src/lib/mysql/mysql_connection.h b/src/lib/mysql/mysql_connection.h index 6ac377b0b9..8700289818 100644 --- a/src/lib/mysql/mysql_connection.h +++ b/src/lib/mysql/mysql_connection.h @@ -241,13 +241,14 @@ public: /// Initialize MySqlConnection object with parameters needed for connection. /// /// @param parameters Specify the connection details. - /// @param io_service The IOService object, used for all ASIO operations. + /// @param io_access_callback The IOService access callback. /// @param callback The connection recovery callback. MySqlConnection(const ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service = isc::asiolink::IOServicePtr(), + IOServiceAccessCallbackPtr io_access_callback = IOServiceAccessCallbackPtr(), DbCallback callback = DbCallback()) - : DatabaseConnection(parameters), io_service_(io_service), - callback_(callback) { + : DatabaseConnection(parameters), + io_service_access_callback_(io_access_callback), + io_service_(), callback_(callback) { } /// @brief Destructor @@ -657,6 +658,9 @@ public: /// /// @note The recover function must be run on the IO Service thread. void startRecoverDbConnection() { + if (!io_service_ && io_service_access_callback_) { + io_service_ = (*io_service_access_callback_)(); + } if (callback_ && io_service_) { io_service_->post(std::bind(callback_, reconnectCtl())); } @@ -680,6 +684,10 @@ public: /// and will be from MySqlHostDataSource. MySqlHolder mysql_; + /// @brief Callback which returns the IOService that can be used to recover + /// the connection. + IOServiceAccessCallbackPtr io_service_access_callback_; + /// @brief IOService object, used for all ASIO operations. isc::asiolink::IOServicePtr io_service_; diff --git a/src/lib/pgsql/pgsql_connection.h b/src/lib/pgsql/pgsql_connection.h index 84f6562bcf..da69a7a99b 100644 --- a/src/lib/pgsql/pgsql_connection.h +++ b/src/lib/pgsql/pgsql_connection.h @@ -307,13 +307,14 @@ public: /// Initialize PgSqlConnection object with parameters needed for connection. /// /// @param parameters Specify the connection details. - /// @param io_service The IOService object, used for all ASIO operations. + /// @param io_access_callback The IOService access callback. /// @param callback The connection recovery callback. PgSqlConnection(const ParameterMap& parameters, - const isc::asiolink::IOServicePtr& io_service = isc::asiolink::IOServicePtr(), + IOServiceAccessCallbackPtr io_access_callback = IOServiceAccessCallbackPtr(), DbCallback callback = DbCallback()) - : DatabaseConnection(parameters), io_service_(io_service), - callback_(callback) { + : DatabaseConnection(parameters), + io_service_access_callback_(io_access_callback), + io_service_(), callback_(callback) { } /// @brief Destructor @@ -425,6 +426,9 @@ public: /// /// @note The recover function must be run on the IO Service thread. void startRecoverDbConnection() { + if (!io_service_ && io_service_access_callback_) { + io_service_ = (*io_service_access_callback_)(); + } if (callback_ && io_service_) { io_service_->post(std::bind(callback_, reconnectCtl())); } @@ -451,6 +455,10 @@ public: return (conn_); } + /// @brief Callback which returns the IOService that can be used to recover + /// the connection. + IOServiceAccessCallbackPtr io_service_access_callback_; + /// @brief IOService object, used for all ASIO operations. isc::asiolink::IOServicePtr io_service_; |