Push live-state events through queued drain to Redis
This commit is contained in:
1 parent
8363e36f6a
commit
97467a4f70
2 files changed
+149
-9
No files matched your search
@@ -2,7 +2,10 @@
|
|||||||
#include <hiredis/hiredis.h>
|
#include <hiredis/hiredis.h>
|
||||||
#include <hiredis/async.h>
|
#include <hiredis/async.h>
|
||||||
#include "RedisAsyncAdapter.h"
|
#include "RedisAsyncAdapter.h"
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
#include "RedisLiveQueue.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -26,6 +29,9 @@ void onAsyncDisconnectThunk(const redisAsyncContext* ac, int status) {
|
|||||||
|
|
||||||
RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {}
|
RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {}
|
||||||
RedisStore::~RedisStore() {
|
RedisStore::~RedisStore() {
|
||||||
|
delete m_drain_timer; m_drain_timer = nullptr;
|
||||||
|
delete m_live_queue; m_live_queue = nullptr;
|
||||||
|
if (m_async) { redisAsyncFree(m_async); m_async = nullptr; }
|
||||||
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
if (m_sync) { redisFree(m_sync); m_sync = nullptr; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +75,12 @@ bool RedisStore::connect(void) {
|
|||||||
if (!connectSync()) return false;
|
if (!connectSync()) return false;
|
||||||
if (!connectAsync()) return false;
|
if (!connectAsync()) return false;
|
||||||
subscribe();
|
subscribe();
|
||||||
|
|
||||||
|
m_live_queue = new RedisLiveQueue(4096);
|
||||||
|
m_drain_timer = new Async::Timer(75, Async::Timer::TYPE_PERIODIC);
|
||||||
|
m_drain_timer->expired.connect(
|
||||||
|
sigc::mem_fun(*this, &RedisStore::drainLiveQueue));
|
||||||
|
|
||||||
std::cout << "RedisStore: connected sync+async, subscribed to "
|
std::cout << "RedisStore: connected sync+async, subscribed to "
|
||||||
<< channelName() << std::endl;
|
<< channelName() << std::endl;
|
||||||
return true;
|
return true;
|
||||||
@@ -208,15 +220,74 @@ void RedisStore::publishConfigChanged(const std::string& scope) {
|
|||||||
if (r) freeReplyObject(r);
|
if (r) freeReplyObject(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RedisStore::pushLiveTalker(uint32_t, const std::string&, const std::string&) {}
|
void RedisStore::pushLiveTalker(uint32_t tg, const std::string& callsign,
|
||||||
void RedisStore::clearLiveTalker(uint32_t) {}
|
const std::string& source) {
|
||||||
void RedisStore::pushLiveClient(const std::string&, const std::string&,
|
if (!m_live_queue) return;
|
||||||
const std::string&, uint32_t) {}
|
RedisLiveQueue::Op op;
|
||||||
void RedisStore::clearLiveClient(const std::string&) {}
|
op.op = RedisLiveQueue::OpType::HSET;
|
||||||
void RedisStore::pushLiveTrunk(const std::string&, const std::string&,
|
op.key = keyFor("live:talker:" + std::to_string(tg));
|
||||||
const std::string&) {}
|
op.fields = {
|
||||||
|
{"callsign", callsign},
|
||||||
|
{"started_at", std::to_string(std::time(nullptr))},
|
||||||
|
{"source", source}
|
||||||
|
};
|
||||||
|
op.ttl_s = 60;
|
||||||
|
m_live_queue->push(std::move(op));
|
||||||
|
}
|
||||||
|
|
||||||
size_t RedisStore::liveQueueSize(void) const { return 0; }
|
void RedisStore::clearLiveTalker(uint32_t tg) {
|
||||||
|
if (!m_live_queue) return;
|
||||||
|
RedisLiveQueue::Op op;
|
||||||
|
op.op = RedisLiveQueue::OpType::DEL;
|
||||||
|
op.key = keyFor("live:talker:" + std::to_string(tg));
|
||||||
|
m_live_queue->push(std::move(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::pushLiveClient(const std::string& callsign,
|
||||||
|
const std::string& ip,
|
||||||
|
const std::string& codecs,
|
||||||
|
uint32_t current_tg) {
|
||||||
|
if (!m_live_queue) return;
|
||||||
|
RedisLiveQueue::Op op;
|
||||||
|
op.op = RedisLiveQueue::OpType::HSET;
|
||||||
|
op.key = keyFor("live:client:" + callsign);
|
||||||
|
op.fields = {
|
||||||
|
{"connected_at", std::to_string(std::time(nullptr))},
|
||||||
|
{"ip", ip},
|
||||||
|
{"codecs", codecs},
|
||||||
|
{"tg", std::to_string(current_tg)}
|
||||||
|
};
|
||||||
|
op.ttl_s = 60;
|
||||||
|
m_live_queue->push(std::move(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::clearLiveClient(const std::string& callsign) {
|
||||||
|
if (!m_live_queue) return;
|
||||||
|
RedisLiveQueue::Op op;
|
||||||
|
op.op = RedisLiveQueue::OpType::DEL;
|
||||||
|
op.key = keyFor("live:client:" + callsign);
|
||||||
|
m_live_queue->push(std::move(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
void RedisStore::pushLiveTrunk(const std::string& section,
|
||||||
|
const std::string& state,
|
||||||
|
const std::string& peer_id) {
|
||||||
|
if (!m_live_queue) return;
|
||||||
|
RedisLiveQueue::Op op;
|
||||||
|
op.op = RedisLiveQueue::OpType::HSET;
|
||||||
|
op.key = keyFor("live:trunk:" + section);
|
||||||
|
op.fields = {
|
||||||
|
{"state", state},
|
||||||
|
{"peer_id", peer_id},
|
||||||
|
{"last_hb", std::to_string(std::time(nullptr))}
|
||||||
|
};
|
||||||
|
op.ttl_s = 60;
|
||||||
|
m_live_queue->push(std::move(op));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t RedisStore::liveQueueSize(void) const {
|
||||||
|
return m_live_queue ? m_live_queue->size() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool RedisStore::connectAsync(void) {
|
bool RedisStore::connectAsync(void) {
|
||||||
if (!m_cfg.unix_socket.empty()) {
|
if (!m_cfg.unix_socket.empty()) {
|
||||||
@@ -255,7 +326,48 @@ void RedisStore::subscribe(void) {
|
|||||||
void RedisStore::scheduleReconnect(void) {}
|
void RedisStore::scheduleReconnect(void) {}
|
||||||
void RedisStore::onAsyncDisconnect(int) {}
|
void RedisStore::onAsyncDisconnect(int) {}
|
||||||
void RedisStore::onPubSubMessage(const std::string&, const std::string&) {}
|
void RedisStore::onPubSubMessage(const std::string&, const std::string&) {}
|
||||||
void RedisStore::drainLiveQueue(Async::Timer*) {}
|
void RedisStore::drainLiveQueue(Async::Timer*) {
|
||||||
|
if (!m_async || !m_live_queue) return;
|
||||||
|
std::vector<RedisLiveQueue::Op> ops;
|
||||||
|
m_live_queue->drain(ops);
|
||||||
|
if (ops.empty()) return;
|
||||||
|
|
||||||
|
for (auto& op : ops) {
|
||||||
|
switch (op.op) {
|
||||||
|
case RedisLiveQueue::OpType::HSET: {
|
||||||
|
std::vector<const char*> argv;
|
||||||
|
std::vector<size_t> argl;
|
||||||
|
argv.push_back("HSET"); argl.push_back(4);
|
||||||
|
argv.push_back(op.key.c_str()); argl.push_back(op.key.size());
|
||||||
|
for (auto& kv : op.fields) {
|
||||||
|
argv.push_back(kv.first.c_str()); argl.push_back(kv.first.size());
|
||||||
|
argv.push_back(kv.second.c_str()); argl.push_back(kv.second.size());
|
||||||
|
}
|
||||||
|
redisAsyncCommandArgv(m_async, nullptr, nullptr,
|
||||||
|
static_cast<int>(argv.size()),
|
||||||
|
argv.data(), argl.data());
|
||||||
|
if (op.ttl_s > 0) {
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr,
|
||||||
|
"EXPIRE %s %d", op.key.c_str(), op.ttl_s);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RedisLiveQueue::OpType::DEL:
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr,
|
||||||
|
"DEL %s", op.key.c_str());
|
||||||
|
break;
|
||||||
|
case RedisLiveQueue::OpType::EXPIRE:
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr,
|
||||||
|
"EXPIRE %s %d", op.key.c_str(), op.ttl_s);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify dashboards once per drain cycle.
|
||||||
|
std::string ch = keyFor("live.changed");
|
||||||
|
redisAsyncCommand(m_async, nullptr, nullptr,
|
||||||
|
"PUBLISH %s tick", ch.c_str());
|
||||||
|
}
|
||||||
void RedisStore::refreshLiveExpire(Async::Timer*) {}
|
void RedisStore::refreshLiveExpire(Async::Timer*) {}
|
||||||
|
|
||||||
std::string RedisStore::keyFor(const std::string& suffix) const {
|
std::string RedisStore::keyFor(const std::string& suffix) const {
|
||||||
|
|||||||
@@ -1081,6 +1081,10 @@ void Reflector::clientDisconnected(Async::FramedTcpConnection *con,
|
|||||||
{
|
{
|
||||||
m_mqtt->onClientDisconnected(client->callsign());
|
m_mqtt->onClientDisconnected(client->callsign());
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->clearLiveClient(client->callsign());
|
||||||
|
}
|
||||||
scheduleNodeListUpdate();
|
scheduleNodeListUpdate();
|
||||||
}
|
}
|
||||||
//Application::app().runTask([=]{ delete client; });
|
//Application::app().runTask([=]{ delete client; });
|
||||||
@@ -1546,6 +1550,10 @@ void Reflector::onTalkerUpdated(uint32_t tg, ReflectorClient* old_talker,
|
|||||||
{
|
{
|
||||||
m_mqtt->onTalkerStop(tg, old_talker->callsign(), false);
|
m_mqtt->onTalkerStop(tg, old_talker->callsign(), false);
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->clearLiveTalker(tg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (new_talker != 0)
|
if (new_talker != 0)
|
||||||
{
|
{
|
||||||
@@ -1565,6 +1573,10 @@ void Reflector::onTalkerUpdated(uint32_t tg, ReflectorClient* old_talker,
|
|||||||
{
|
{
|
||||||
m_mqtt->onTalkerStart(tg, new_talker->callsign(), false);
|
m_mqtt->onTalkerStart(tg, new_talker->callsign(), false);
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->pushLiveTalker(tg, new_talker->callsign(), "local");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// In satellite mode, forward to parent instead of trunk peers
|
// In satellite mode, forward to parent instead of trunk peers
|
||||||
@@ -2682,6 +2694,10 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg,
|
|||||||
{
|
{
|
||||||
m_mqtt->onTalkerStop(tg, old_cs, true);
|
m_mqtt->onTalkerStop(tg, old_cs, true);
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->clearLiveTalker(tg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!new_cs.empty())
|
if (!new_cs.empty())
|
||||||
{
|
{
|
||||||
@@ -2696,6 +2712,10 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg,
|
|||||||
{
|
{
|
||||||
m_mqtt->onTalkerStart(tg, new_cs, true);
|
m_mqtt->onTalkerStart(tg, new_cs, true);
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->pushLiveTalker(tg, new_cs, "trunk");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward trunk talker events to connected satellites
|
// Forward trunk talker events to connected satellites
|
||||||
@@ -2785,6 +2805,10 @@ void Reflector::onClientAuthenticated(const std::string& callsign,
|
|||||||
{
|
{
|
||||||
m_mqtt->onClientConnected(callsign, tg, ip);
|
m_mqtt->onClientConnected(callsign, tg, ip);
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->pushLiveClient(callsign, ip, "", tg);
|
||||||
|
}
|
||||||
scheduleNodeListUpdate();
|
scheduleNodeListUpdate();
|
||||||
} /* Reflector::onClientAuthenticated */
|
} /* Reflector::onClientAuthenticated */
|
||||||
|
|
||||||
@@ -2851,6 +2875,10 @@ void Reflector::onTrunkStateChanged(const std::string& section,
|
|||||||
m_mqtt->onTrunkDown(topic_id, direction);
|
m_mqtt->onTrunkDown(topic_id, direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (m_redis != nullptr)
|
||||||
|
{
|
||||||
|
m_redis->pushLiveTrunk(section, up ? "up" : "down", peer_id);
|
||||||
|
}
|
||||||
} /* Reflector::onTrunkStateChanged */
|
} /* Reflector::onTrunkStateChanged */
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user