Skip to content

Commit

Permalink
Merge pull request #3038 from nats-io/js_expected_last_seq_zero
Browse files Browse the repository at this point in the history
[CHANGED] JetStream: accept "Nats-Expected-Last-Sequence" with "0"
  • Loading branch information
kozlovic committed Jul 7, 2022
2 parents c49d081 + be8734e commit 7ab2561
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions server/jetstream_test.go
Expand Up @@ -3478,6 +3478,16 @@ func TestJetStreamPublishExpect(t *testing.T) {
t.Fatalf("Expected an error, got %q", resp.Data)
}

// Or if we expect that there are no messages by setting "0" as the expected last seq
m.Header.Set(JSExpectedLastSeq, "0")
resp, err = nc.RequestMsg(m, 100*time.Millisecond)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if pa := getPubAckResponse(resp.Data); pa == nil || pa.Error == nil {
t.Fatalf("Expected an error, got %q", resp.Data)
}

// Now send a message with a message ID and make sure we can match that.
m = nats.NewMsg("foo.bar")
m.Data = []byte("HELLO")
Expand Down
8 changes: 4 additions & 4 deletions server/stream.go
Expand Up @@ -3187,12 +3187,12 @@ func getExpectedStream(hdr []byte) string {
}

// Fast lookup of expected stream.
func getExpectedLastSeq(hdr []byte) uint64 {
func getExpectedLastSeq(hdr []byte) (uint64, bool) {
bseq := getHeader(JSExpectedLastSeq, hdr)
if len(bseq) == 0 {
return 0
return 0, false
}
return uint64(parseInt64(bseq))
return uint64(parseInt64(bseq)), true
}

// Fast lookup of rollups.
Expand Down Expand Up @@ -3472,7 +3472,7 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte,
return errors.New("expected stream does not match")
}
// Expected last sequence.
if seq := getExpectedLastSeq(hdr); seq > 0 && seq != mset.lseq {
if seq, exists := getExpectedLastSeq(hdr); exists && seq != mset.lseq {
mlseq := mset.lseq
mset.clfs++
mset.mu.Unlock()
Expand Down

0 comments on commit 7ab2561

Please sign in to comment.