Add bounded in-memory queue for live-state Redis writes
This commit is contained in:
1 parent
92c2a94940
commit
8363e36f6a
3 files changed
+63
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<Op>& 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;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef REDIS_LIVE_QUEUE_INCLUDED
|
||||
#define REDIS_LIVE_QUEUE_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
class RedisLiveQueue
|
||||
{
|
||||
public:
|
||||
enum class OpType { HSET, DEL, EXPIRE };
|
||||
|
||||
struct Op {
|
||||
OpType op;
|
||||
std::string key;
|
||||
std::vector<std::pair<std::string, std::string>> 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<Op>& out);
|
||||
|
||||
uint64_t droppedCount(void) const { return m_dropped; }
|
||||
size_t size(void) const { return m_q.size(); }
|
||||
|
||||
private:
|
||||
std::deque<Op> m_q;
|
||||
size_t m_max;
|
||||
uint64_t m_dropped = 0;
|
||||
};
|
||||
|
||||
#endif /* REDIS_LIVE_QUEUE_INCLUDED */
|
||||
Reference in new issue
Block a user