Refactor Redis & Posgres notification listeners into listener subclass in new CentralDB class

This allows us to interchangeably use different listeners (pgsql, redis, pubsub) depending on configuration values passed into the constructor.
This commit is contained in:
Grant Limberg 2025-08-20 17:04:28 -07:00
parent ebe8fdb08e
commit 95224379aa
13 changed files with 2486 additions and 31 deletions

View file

@ -0,0 +1,76 @@
#ifdef ZT_CONTROLLER_USE_LIBPQ
#ifndef ZT_CONTROLLER_REDIS_LISTENER_HPP
#define ZT_CONTROLLER_REDIS_LISTENER_HPP
#include "DB.hpp"
#include "NotificationListener.hpp"
#include "Redis.hpp"
#include <memory>
#include <redis++/redis++.h>
#include <string>
#include <thread>
namespace ZeroTier {
class RedisListener : public NotificationListener {
public:
RedisListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis);
RedisListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster);
virtual ~RedisListener();
virtual void listen() = 0;
virtual void onNotification(const std::string& payload) override
{
}
void start()
{
_run = true;
_listenThread = std::thread(&RedisListener::listen, this);
}
protected:
std::string _controller_id;
std::shared_ptr<sw::redis::Redis> _redis;
std::shared_ptr<sw::redis::RedisCluster> _cluster;
bool _is_cluster = false;
bool _run = false;
private:
std::thread _listenThread;
};
class RedisNetworkListener : public RedisListener {
public:
RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
RedisNetworkListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
virtual ~RedisNetworkListener();
virtual void listen() override;
virtual void onNotification(const std::string& payload) override;
private:
DB* _db;
};
class RedisMemberListener : public RedisListener {
public:
RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::Redis> redis, DB* db);
RedisMemberListener(std::string controller_id, std::shared_ptr<sw::redis::RedisCluster> cluster, DB* db);
virtual ~RedisMemberListener();
virtual void listen() override;
virtual void onNotification(const std::string& payload) override;
private:
DB* _db;
};
} // namespace ZeroTier
#endif // ZT_CONTROLLER_REDIS_LISTENER_HPP
#endif // ZT_CONTROLLER_USE_LIBPQ