diff options
| author | Grant Limberg <[email protected]> | 2021-06-02 11:44:00 -0700 |
|---|---|---|
| committer | Grant Limberg <[email protected]> | 2021-06-02 11:44:00 -0700 |
| commit | 47154fa6234a7af69f209824c8869d8d07f0895c (patch) | |
| tree | 81a3edf0e31005b5e056b88c97a8059148218410 /controller/PostgreSQL.hpp | |
| parent | c2efdcabc5ab62b307cf758c032bf35db87b97db (diff) | |
transiton to libpqxx & connection pool for central controllers
Diffstat (limited to 'controller/PostgreSQL.hpp')
| -rw-r--r-- | controller/PostgreSQL.hpp | 73 |
1 files changed, 67 insertions, 6 deletions
diff --git a/controller/PostgreSQL.hpp b/controller/PostgreSQL.hpp index 1870b5af..689a29bb 100644 --- a/controller/PostgreSQL.hpp +++ b/controller/PostgreSQL.hpp @@ -20,6 +20,9 @@ #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4 +#include "ConnectionPool.hpp" +#include <pqxx/pqxx> + #include <memory> #include <redis++/redis++.h> @@ -31,14 +34,65 @@ namespace ZeroTier { struct RedisConfig; + +class PostgresConnection : public Connection { +public: + virtual ~PostgresConnection() { + } + + std::shared_ptr<pqxx::connection> c; + int a; +}; + + +class PostgresConnFactory : public ConnectionFactory { +public: + PostgresConnFactory(std::string &connString) + : m_connString(connString) + { + } + + virtual std::shared_ptr<Connection> create() { + auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection()); + c->c = std::make_shared<pqxx::connection>(m_connString); + return std::static_pointer_cast<Connection>(c); + } +private: + std::string m_connString; +}; + +class PostgreSQL; + +class MemberNotificationReceiver : public pqxx::notification_receiver { +public: + MemberNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel); + virtual ~MemberNotificationReceiver() {} + + virtual void operator() (const std::string &payload, int backendPid); +private: + PostgreSQL *_psql; +}; + +class NetworkNotificationReceiver : public pqxx::notification_receiver { +public: + NetworkNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel); + virtual ~NetworkNotificationReceiver() {}; + + virtual void operator() (const std::string &payload, int packend_pid); +private: + PostgreSQL *_psql; +}; + /** * A controller database driver that talks to PostgreSQL * * This is for use with ZeroTier Central. Others are free to build and use it - * but be aware taht we might change it at any time. + * but be aware that we might change it at any time. */ class PostgreSQL : public DB { + friend class MemberNotificationReceiver; + friend class NetworkNotificationReceiver; public: PostgreSQL(const Identity &myId, const char *path, int listenPort, RedisConfig *rc); virtual ~PostgreSQL(); @@ -56,15 +110,22 @@ protected: { inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); } }; + void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) { + DB::_memberChanged(old, memberConfig, notifyListeners); + } + + void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) { + DB::_memberChanged(old, networkConfig, notifyListeners); + } private: - void initializeNetworks(PGconn *conn); - void initializeMembers(PGconn *conn); + void initializeNetworks(); + void initializeMembers(); void heartbeat(); void membersDbWatcher(); - void _membersWatcher_Postgres(PGconn *conn); + void _membersWatcher_Postgres(); void networksDbWatcher(); - void _networksWatcher_Postgres(PGconn *conn); + void _networksWatcher_Postgres(); void _membersWatcher_Redis(); void _networksWatcher_Redis(); @@ -81,7 +142,7 @@ private: NO_OVERRIDE = 1 }; - PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE ); + std::shared_ptr<ConnectionPool<PostgresConnection> > _pool; const Identity _myId; const Address _myAddress; |
