Skip to content

Commit 65bdcd8

Browse files
committed
feat(pubsub/redis): add streamStartID metadata option
The redis streams pubsub always created new consumer groups starting from entry ID 0, so a consumer group always had to replay the entire stream history before receiving new messages. Added a streamStartID option so callers can set it to $ to only receive messages published after the group is created, while defaulting to 0 to keep existing deployments unaffected. Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
1 parent b57fc94 commit 65bdcd8

5 files changed

Lines changed: 20 additions & 5 deletions

File tree

common/component/redis/redis.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func ParseClientFromProperties(properties map[string]string, componentType metad
139139
settings.RedeliverInterval = 15 * time.Second
140140
settings.QueueDepth = 100
141141
settings.Concurrency = 10
142+
settings.StreamStartID = "0"
142143
}
143144

144145
err := settings.Decode(properties)

common/component/redis/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ type Settings struct {
113113
QueueDepth uint `mapstructure:"queueDepth" mdonly:"pubsub"`
114114
// The number of concurrent workers that are processing messages
115115
Concurrency uint `mapstructure:"concurrency" mdonly:"pubsub"`
116+
// The stream entry ID a new consumer group starts reading from
117+
StreamStartID string `mapstructure:"streamStartID" mdonly:"pubsub"`
116118

117119
// The max len of stream
118120
MaxLenApprox int64 `mapstructure:"maxLenApprox" mdonly:"pubsub"`

pubsub/redis/metadata.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ metadata:
223223
The number of concurrent workers that are processing messages.
224224
example: "15"
225225
default: "10"
226+
- name: streamStartID
227+
required: false
228+
description: |
229+
The stream entry ID a new consumer group starts reading from. Use "0" to
230+
read the entire stream history, or "$" to only receive messages published
231+
after the consumer group is created.
232+
example: "$"
233+
default: "0"
234+
type: string
226235
type: number
227236
- name: redisType
228237
required: false

pubsub/redis/redis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
concurrency = "concurrency"
4040
maxLenApprox = "maxLenApprox"
4141
streamTTL = "streamTTL"
42+
streamStartID = "streamStartID"
4243
)
4344

4445
// redisStreams handles consuming from a Redis stream using
@@ -126,7 +127,7 @@ func (r *redisStreams) Publish(ctx context.Context, req *pubsub.PublishRequest)
126127
}
127128

128129
func (r *redisStreams) CreateConsumerGroup(ctx context.Context, stream string) error {
129-
err := r.client.XGroupCreateMkStream(ctx, stream, r.clientSettings.ConsumerID, "0")
130+
err := r.client.XGroupCreateMkStream(ctx, stream, r.clientSettings.ConsumerID, r.clientSettings.StreamStartID)
130131
// Ignore BUSYGROUP errors
131132
if err != nil && err.Error() != "BUSYGROUP Consumer Group name already exists" {
132133
r.logger.Errorf("redis streams: %s", err)

pubsub/redis/redis_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ func TestPublishWhenClosedIsTerminal(t *testing.T) {
4949

5050
func getFakeProperties() map[string]string {
5151
return map[string]string{
52-
consumerID: "fakeConsumer",
53-
enableTLS: "true",
54-
maxLenApprox: "1000",
55-
streamTTL: "1h",
52+
consumerID: "fakeConsumer",
53+
enableTLS: "true",
54+
maxLenApprox: "1000",
55+
streamTTL: "1h",
56+
streamStartID: "$",
5657
}
5758
}
5859

@@ -73,6 +74,7 @@ func TestParseRedisMetadata(t *testing.T) {
7374
assert.Equal(t, fakeProperties[consumerID], m.ConsumerID)
7475
assert.Equal(t, int64(1000), m.MaxLenApprox)
7576
assert.Equal(t, 1*time.Hour, m.StreamTTL)
77+
assert.Equal(t, "$", m.StreamStartID)
7678
})
7779

7880
// TODO: fix the code to return the error for the missing property to make this test work

0 commit comments

Comments
 (0)