Skip to content
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

Fix Pull Consumer not sending request timeout #3942

Merged
merged 1 commit into from Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/consumer.go
Expand Up @@ -2781,6 +2781,17 @@ func (o *consumer) nextWaiting(sz int) *waitingRequest {
} else if o.srv.gateway.enabled && o.srv.hasGatewayInterest(wr.acc.Name, wr.interest) {
return o.waiting.pop()
}
} else {
// We do check for expiration in `processWaiting`, but it is possible to hit the expiry here, and not there.
hdr := []byte(fmt.Sprintf("NATS/1.0 408 Request Timeout\r\n%s: %d\r\n%s: %d\r\n\r\n", JSPullRequestPendingMsgs, wr.n, JSPullRequestPendingBytes, wr.b))
o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0))
o.waiting.removeCurrent()
if o.node != nil {
o.removeClusterPendingRequest(wr.reply)
}
wr.recycle()
continue

}
if wr.interest != wr.reply {
const intExpT = "NATS/1.0 408 Interest Expired\r\n%s: %d\r\n%s: %d\r\n\r\n"
Expand Down
57 changes: 57 additions & 0 deletions server/jetstream_test.go
Expand Up @@ -16732,6 +16732,63 @@ func TestJetStreamDisabledHealthz(t *testing.T) {
t.Fatalf("Expected healthz to return error if JetStream is disabled, got status: %s", hs.Status)
}

func TestJetStreamPullTimeout(t *testing.T) {
s := RunBasicJetStreamServer(t)
defer s.Shutdown()

nc, js := jsClientConnect(t, s)
defer nc.Close()

_, err := js.AddStream(&nats.StreamConfig{
Name: "TEST",
})
require_NoError(t, err)

_, err = js.AddConsumer("TEST", &nats.ConsumerConfig{
Durable: "pr",
AckPolicy: nats.AckExplicitPolicy,
})
require_NoError(t, err)

const numMessages = 1000
// Send messages in small intervals.
go func() {
for i := 0; i < numMessages; i++ {
time.Sleep(time.Millisecond * 10)
sendStreamMsg(t, nc, "TEST", "data")
}
}()

// Prepare manual Pull Request.
req := &JSApiConsumerGetNextRequest{Batch: 200, NoWait: false, Expires: time.Millisecond * 100}
jreq, _ := json.Marshal(req)

subj := fmt.Sprintf(JSApiRequestNextT, "TEST", "pr")
reply := "_pr_"
var got atomic.Int32
nc.PublishRequest(subj, reply, jreq)

// Manually subscribe to inbox subject and send new request only if we get `408 Request Timeout`.
sub, _ := nc.Subscribe(reply, func(msg *nats.Msg) {
if msg.Header.Get("Status") == "408" && msg.Header.Get("Description") == "Request Timeout" {
nc.PublishRequest(subj, reply, jreq)
nc.Flush()
} else {
got.Add(1)
msg.Ack()
}
})
defer sub.Unsubscribe()

// Check if we're not stuck.
checkFor(t, time.Second*30, time.Second*1, func() error {
if got.Load() < int32(numMessages) {
return fmt.Errorf("expected %d messages", numMessages)
}
return nil
})
}

func TestJetStreamPullMaxBytes(t *testing.T) {
s := RunBasicJetStreamServer(t)
defer s.Shutdown()
Expand Down