From e746efab9663b0eebb8d5bfed2ac613f644a9b31 Mon Sep 17 00:00:00 2001 From: Garcia Fabien Date: Wed, 25 Sep 2024 10:12:04 +0200 Subject: [PATCH 1/2] [FIX] Memory usage CAO the stream Make sure we keep our stream in check using the maxlen option. According to the documentation # Documentation https://redis.io/docs/latest/commands/xadd/ ## Capped streams XADD incorporates the same semantics as the XTRIM command - refer to its documentation page for more information. This allows adding new entries and keeping the stream's size in check with a single call to XADD, effectively capping the stream with an arbitrary threshold. Although exact trimming is possible and is the default, due to the internal representation of steams it is more efficient to add an entry and trim stream with XADD using almost exact trimming (the ~ argument). For example, calling XADD in the following form: ``` XADD mystream MAXLEN ~ 1000 * ... entry fields here ... ``` Will add a new entry but will also evict old entries so that the stream will contain only 1000 entries, or at most a few tens more. --- Gemfile.lock | 2 +- lib/redis_stream/publisher.rb | 2 +- lib/redis_stream/version.rb | 2 +- spec/dummy_redis.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c97e6ca..18886e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - redis_stream (0.1.3) + redis_stream (0.1.5) GEM remote: https://rubygems.org/ diff --git a/lib/redis_stream/publisher.rb b/lib/redis_stream/publisher.rb index 932ac57..9df78b3 100644 --- a/lib/redis_stream/publisher.rb +++ b/lib/redis_stream/publisher.rb @@ -10,7 +10,7 @@ def initialize(stream_key) def publish(name, data = {}) data = {"name" => name, "json" => JSON.generate(data)} - RedisStream.client.xadd(@stream_key, data) + RedisStream.client.xadd(@stream_key, data, maxlen: 1000, approximate: true) end end end diff --git a/lib/redis_stream/version.rb b/lib/redis_stream/version.rb index 381e447..125fd8d 100644 --- a/lib/redis_stream/version.rb +++ b/lib/redis_stream/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RedisStream - VERSION = "0.1.4" + VERSION = "0.1.5" end diff --git a/spec/dummy_redis.rb b/spec/dummy_redis.rb index c35a81a..4fa75e6 100644 --- a/spec/dummy_redis.rb +++ b/spec/dummy_redis.rb @@ -8,7 +8,7 @@ def xadd(stream, data, approximate: nil, maxlen: nil, nomkstream: nil, id: "*") @streams[stream] << data end - def xreadgroup(group, consumer, streams, condition, count: 1, block: 5000) + def xreadgroup(group, consumer, streams, condition, count: 1, block: 5000, noack: false) @streams[streams] ||= [] @streams[streams].map.with_index do |data, index| [streams, {index => data}] From b06a43f81eda8a18a815d684441c24101f11679b Mon Sep 17 00:00:00 2001 From: Garcia Fabien Date: Wed, 25 Sep 2024 10:34:20 +0200 Subject: [PATCH 2/2] formatting --- Gemfile.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile.lock b/Gemfile.lock index a369be1..18886e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: redis_stream (0.1.5) + GEM remote: https://rubygems.org/ specs: