mredis: check for pending messages during the init stage

This commit is contained in:
Brian Picciano 2019-07-29 23:31:58 -04:00
parent 4d859c6d02
commit 0b5b5aac00
2 changed files with 17 additions and 3 deletions

View File

@ -128,6 +128,15 @@ func NewStream(r *Redis, opts StreamOpts) *Stream {
} }
} }
func (s *Stream) getNumPending() (int64, error) {
var res []interface{}
err := s.client.Do(radix.Cmd(&res, "XPENDING", s.opts.Key, s.opts.Group))
if err != nil {
return 0, merr.Wrap(err, s.client.cmp.Context())
}
return res[0].(int64), nil
}
func (s *Stream) init() error { func (s *Stream) init() error {
// MKSTREAM is not documented, but will make the stream if it doesn't // MKSTREAM is not documented, but will make the stream if it doesn't
// already exist. Only the most elite redis gurus know of it's // already exist. Only the most elite redis gurus know of it's
@ -139,6 +148,12 @@ func (s *Stream) init() error {
return merr.Wrap(err, s.client.cmp.Context()) return merr.Wrap(err, s.client.cmp.Context())
} }
numPending, err := s.getNumPending()
if err != nil {
return err
}
atomic.StoreInt64(&s.numPending, numPending)
// if we're here it means init succeeded, mark as such and gtfo // if we're here it means init succeeded, mark as such and gtfo
s.hasInit = true s.hasInit = true
return nil return nil

View File

@ -135,11 +135,10 @@ func TestStream(t *T) {
// call XPENDING to see if anything comes back, nothing should. // call XPENDING to see if anything comes back, nothing should.
t.Log("checking for leftover pending entries") t.Log("checking for leftover pending entries")
var xpendingRes []interface{} numPending, err := stream.getNumPending()
err := redis.Do(radix.Cmd(&xpendingRes, "XPENDING", streamKey, group))
if err != nil { if err != nil {
t.Fatalf("error calling XPENDING: %v", err) t.Fatalf("error calling XPENDING: %v", err)
} else if numPending := xpendingRes[0].(int64); numPending != 0 { } else if numPending > 0 {
t.Fatalf("XPENDING says there's %v pending msgs, there should be 0", numPending) t.Fatalf("XPENDING says there's %v pending msgs, there should be 0", numPending)
} }