diff --git a/src/svxlink/reflector/CMakeLists.txt b/src/svxlink/reflector/CMakeLists.txt index 775ed0f..fb6df21 100644 --- a/src/svxlink/reflector/CMakeLists.txt +++ b/src/svxlink/reflector/CMakeLists.txt @@ -36,7 +36,7 @@ set(LIBS asynccpp asyncaudio asynccore svxmisc ${LIBS}) add_executable(svxreflector svxreflector.cpp Reflector.cpp ReflectorClient.cpp TGHandler.cpp TrunkLink.cpp SatelliteLink.cpp SatelliteClient.cpp MqttPublisher.cpp RedisStore.cpp - RedisAsyncAdapter.cpp + RedisAsyncAdapter.cpp RedisLiveQueue.cpp ) target_link_libraries(svxreflector ${LIBS}) set_target_properties(svxreflector PROPERTIES diff --git a/src/svxlink/reflector/RedisLiveQueue.cpp b/src/svxlink/reflector/RedisLiveQueue.cpp new file mode 100644 index 0000000..1354623 --- /dev/null +++ b/src/svxlink/reflector/RedisLiveQueue.cpp @@ -0,0 +1,22 @@ +#include "RedisLiveQueue.h" + +RedisLiveQueue::RedisLiveQueue(size_t max_pending) : m_max(max_pending) {} + +bool RedisLiveQueue::push(Op op) { + if (m_q.size() >= m_max) { + m_q.pop_front(); + ++m_dropped; + } + m_q.push_back(std::move(op)); + return true; +} + +size_t RedisLiveQueue::drain(std::vector& out) { + size_t n = m_q.size(); + out.reserve(out.size() + n); + while (!m_q.empty()) { + out.push_back(std::move(m_q.front())); + m_q.pop_front(); + } + return n; +} diff --git a/src/svxlink/reflector/RedisLiveQueue.h b/src/svxlink/reflector/RedisLiveQueue.h new file mode 100644 index 0000000..aeb082a --- /dev/null +++ b/src/svxlink/reflector/RedisLiveQueue.h @@ -0,0 +1,40 @@ +#ifndef REDIS_LIVE_QUEUE_INCLUDED +#define REDIS_LIVE_QUEUE_INCLUDED + +#include +#include +#include +#include +#include + +class RedisLiveQueue +{ + public: + enum class OpType { HSET, DEL, EXPIRE }; + + struct Op { + OpType op; + std::string key; + std::vector> fields; // HSET + int ttl_s = 0; // EXPIRE + }; + + explicit RedisLiveQueue(size_t max_pending = 4096); + + // Push. On overflow, drops the oldest entry and increments the + // dropped counter. Always returns true (push never fails). + bool push(Op op); + + // Drain everything currently queued into `out`. Returns count moved. + size_t drain(std::vector& out); + + uint64_t droppedCount(void) const { return m_dropped; } + size_t size(void) const { return m_q.size(); } + + private: + std::deque m_q; + size_t m_max; + uint64_t m_dropped = 0; +}; + +#endif /* REDIS_LIVE_QUEUE_INCLUDED */