Skip to content

Commit

Permalink
Merge pull request #534 from ripienaar/gaps_npe
Browse files Browse the repository at this point in the history
Avoid some index out of range errors when detecting gaps
  • Loading branch information
ripienaar authored Apr 16, 2024
2 parents 41dff41 + 423da94 commit 2690708
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,26 +861,30 @@ func (s *Stream) DetectGaps(ctx context.Context, progress func(seq uint64, pendi
return err
}

progress(nfo.State.Msgs, 0)
progress(nfo.State.Msgs, nfo.State.Msgs)

if len(nfo.State.Deleted) == 0 {
return nil
}

if len(nfo.State.Deleted) == 1 {
gap(nfo.State.Deleted[0], nfo.State.Deleted[0])
seq := nfo.State.Deleted[0]
gap(seq, seq)
progress(seq, 0)
return nil
}

start := nfo.State.Deleted[0]

for i, seq := range nfo.State.Deleted {
progress(seq, nfo.State.Msgs-seq)

// the last message
// the last deleted message
if i == len(nfo.State.Deleted)-1 {
// if its part of a gap we close it off
if seq-1 == nfo.State.Deleted[i-1] {
gap(start, seq)
progress(seq, 0)
return nil
}

Expand All @@ -893,6 +897,7 @@ func (s *Stream) DetectGaps(ctx context.Context, progress func(seq uint64, pendi
// end and start of a gap
if nfo.State.Deleted[i+1] != seq+1 {
gap(start, seq)
progress(seq, 0)
start = nfo.State.Deleted[i+1]
}
}
Expand Down

0 comments on commit 2690708

Please sign in to comment.