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

doc(jestream): clarified consumer backoff - also added a test #117

Merged
merged 1 commit into from
Nov 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jetstream/src/jsapi_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ export interface ConsumerUpdateConfig {
*/
"inactive_threshold"?: Nanos;
/**
* List of durations in nanoseconds format that represents a retry timescale for
* NaK'd messages or those being normally retried
* List of durations in nanoseconds that represents a retry timescale for
* the redelivery of messages
*/
"backoff"?: Nanos[];
/**
Expand Down
59 changes: 57 additions & 2 deletions jetstream/tests/jetstream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from "@nats-io/nats-core";
import {
assert,
assertAlmostEquals,
assertEquals,
assertExists,
assertInstanceOf,
Expand Down Expand Up @@ -707,7 +708,8 @@ Deno.test("jetstream - backoff", async () => {
const { stream, subj } = await initStream(nc);
const jsm = await jetstreamManager(nc);

const backoff = [nanos(250), nanos(1000), nanos(3000)];
const ms = [250, 1000, 3000];
const backoff = ms.map((n) => nanos(n));
const ci = await jsm.consumers.add(stream, {
durable_name: "me",
ack_policy: AckPolicy.Explicit,
Expand All @@ -723,10 +725,11 @@ Deno.test("jetstream - backoff", async () => {
const js = jetstream(nc);
await js.publish(subj);

const when: number[] = [];
const c = await js.consumers.get(stream, "me");
const iter = await c.consume({
callback: (m) => {
console.log(m.info.redeliveryCount);
when.push(Date.now());
if (m.info.redeliveryCount === 4) {
iter.stop();
}
Expand All @@ -735,6 +738,58 @@ Deno.test("jetstream - backoff", async () => {

await iter.closed();

const offset = when.map((n, idx) => {
const p = idx > 0 ? idx - 1 : 0;
return n - when[p];
});

offset.slice(1).forEach((n, idx) => {
assertAlmostEquals(n, ms[idx], 20);
});

await cleanup(ns, nc);
});

Deno.test("jetstream - redelivery", async () => {
const { ns, nc } = await setup(jetstreamServerConf({}));
if (await notCompatible(ns, nc, "2.7.2")) {
return;
}

const { stream, subj } = await initStream(nc);
const jsm = await jetstreamManager(nc);

const ci = await jsm.consumers.add(stream, {
durable_name: "me",
ack_policy: AckPolicy.Explicit,
max_deliver: 4,
ack_wait: nanos(1000),
});

assertEquals(ci.config.max_deliver, 4);

const js = jetstream(nc);
await js.publish(subj);

const c = await js.consumers.get(stream, "me");

let redeliveries = 0;
const iter = await c.consume({
callback: (m) => {
if (m.redelivered) {
redeliveries++;
}
if (m.info.redeliveryCount === 4) {
setTimeout(() => {
iter.stop();
}, 2000);
}
},
});

await iter.closed();
assertEquals(redeliveries, 3);

await cleanup(ns, nc);
});

Expand Down