Description
XINFO GROUPS returns XInfoGroup.Lag field as 0 when redis reports nil for the Lag.
The redis nil error seems to be ignored on purpose here.
https://github.com/redis/go-redis/blob/master/command.go#L2151
This makes differentiating that actually lag is zero or it can not be computed.
Expected Behavior
I would expect to see a differentiable value from 0 when it is invalid.
Current Behavior
It returns 0 when redis reports nil for the Lag.
Possible Solution
We can return -1 instead of 0 when redis reports nil for the Lag.
Steps to Reproduce
Here you can find a description when redis reports nil for the lag.
One or more entries between the group's last-delivered-id and the stream's last-generated-id were deleted
https://redis.io/docs/latest/commands/xinfo-groups/
A code example to reproduce:
r, err := redis.ParseURL("redis://:@127.0.0.1:6379")
panicOnError(err)
client := redis.NewClient(r)
ctx := context.Background()
client.FlushDB(ctx)
client.XAdd(ctx, &redis.XAddArgs{Stream: "s", ID: "0-1", Values: []string{"foo", "1"}})
client.XAdd(ctx, &redis.XAddArgs{Stream: "s", ID: "0-2", Values: []string{"foo", "2"}})
client.XAdd(ctx, &redis.XAddArgs{Stream: "s", ID: "0-3", Values: []string{"foo", "3"}})
err = client.XGroupCreate(ctx, "s", "g", "0").Err()
panicOnError(err)
err = client.XReadGroup(ctx, &redis.XReadGroupArgs{Group: "g", Consumer: "c", Streams: []string{"s", ">"}, Count: 1, Block: -1, NoAck: false}).Err()
panicOnError(err)
client.XDel(ctx, "s", "0-2")
result, err := client.XInfoGroups(context.Background(), "s").Result()
panicOnError(err)
for _, group := range result {
fmt.Printf("%+v\n", group)
}
The output it prints:
{Name:g Consumers:1 Pending:1 LastDeliveredID:0-1 EntriesRead:1 Lag:0}
What I expected to see:
{Name:g Consumers:1 Pending:1 LastDeliveredID:0-1 EntriesRead:1 Lag:-1}
Possible Implementation
The code here can be changed to
group.Lag, err = rd.ReadInt()
// lag: the number of entries in the stream that are still waiting to be delivered
// to the group's consumers, or a NULL(Nil) when that number can't be determined.
if err != nil {
err == Nil {
group.Lag = -1
} else {
return err
}
}