-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to XREAD last element #19
Comments
Hello, any update on this? |
Hi. Sorry this got lost in the pile of incoming notifications 😓 Wow, this is a really new feature 😮 Currently, gtrs code is as follows: for {
entry = read(lastId)
chan <- entry
lastId = entry.Id
} So it works both when you specify
Yet I also don't understand this comment from oranagra.... If I use $ after +, it means that I can skip an entry that was added in-between reading + and $ 🤷🏻 Can you please try it out? If not, I'll publish a fix to what the issue is 🙂 |
Hello, sorry for the long delay. I'll try it today or tomorrow. |
The problem that "+" don't works in xread, only in xrange/xrevrange. So I can read only all records "0-0" or only new records "$". Any other id will result to: |
I guess it was added later, so you probably need to update your dependencies. I also know I can pass last id from xrevrange to gtrs consumer, but I'm not sure how to make that id inclusive (i.e. include the specified id in stream after start listening). |
The only way it will work right now. stream := "main"
id := LastMessageID(ctx, redis, stream)
stream := gtrs.NewConsumer[Data](ctx, redis, gtrs.StreamIDs{stream: id}) func LastMessageID(ctx context.Context, client redis.Cmdable, stream string) string {
messages, err := client.XRevRangeN(ctx, stream, "+", "-", 1).Result()
if err != nil {
return "$", nil
}
// If there are no messages, we can only wait for the next one
if len(messages) == 0 {
return "$", nil
}
id, err := strconv.Atoi(str.LeftPart(messages[0].ID, "-"))
if err != nil {
return "$", nil
}
// We are looking for the closest entry before this one.
// We assume that no one will have time to send 1 million records in 1 ms.
return fmt.Sprintf("%d-%d", id-1, 99999), nil
} |
As far as I understand, gtrs consumer reads data via xread, I would like to be able to read the last element of the stream. As far as I understand, this cannot be done now, right?
redis/redis#7388
Reads all records:
Reads only new records:
The text was updated successfully, but these errors were encountered: