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

[FEAT] [JS] [CONSUMERS] added name_prefix option to ordered consumer that specifies an user-provided prefix to add to generated consumer names #697

Merged
merged 1 commit into from
May 9, 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
8 changes: 7 additions & 1 deletion jetstream/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "../nats-base-client/util.ts";
import { ConsumerAPI, ConsumerAPIImpl } from "./jsmconsumer_api.ts";
import { nuid } from "../nats-base-client/nuid.ts";
import { isHeartbeatMsg } from "./jsutil.ts";
import { isHeartbeatMsg, minValidation } from "./jsutil.ts";
import { QueuedIteratorImpl } from "../nats-base-client/queued_iterator.ts";
import {
createInbox,
Expand Down Expand Up @@ -948,6 +948,7 @@ export class PullConsumerImpl implements Consumer {
* {@link ConsumerUpdateConfig}
*/
export type OrderedConsumerOptions = {
name_prefix: string;
filterSubjects: string[] | string;
deliver_policy: DeliverPolicy;
opt_start_seq: number;
Expand Down Expand Up @@ -981,6 +982,11 @@ export class OrderedPullConsumerImpl implements Consumer {
this.stream = stream;
this.cursor = { stream_seq: 1, deliver_seq: 0 };
this.namePrefix = nuid.next();
if (typeof opts.name_prefix === "string") {
// make sure the prefix is valid
minValidation("name_prefix", opts.name_prefix);
this.namePrefix = opts.name_prefix + this.namePrefix;
}
this.serial = 0;
this.currentConsumer = null;
this.userCallback = null;
Expand Down
31 changes: 31 additions & 0 deletions jetstream/tests/consumers_ordered_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { initStream } from "./jstest_util.ts";
import {
assert,
assertEquals,
assertExists,
assertRejects,
Expand Down Expand Up @@ -947,3 +948,33 @@ Deno.test("ordered consumers - bind is rejected", async () => {

await cleanup(ns, nc);
});

Deno.test("ordered consumers - name prefix", async () => {
const { ns, nc } = await setup(jetstreamServerConf());

const jsm = await nc.jetstreamManager();
await jsm.streams.add({ name: "A", subjects: ["a"] });

const js = nc.jetstream();
const c = await js.consumers.get("A", { name_prefix: "hello" });
const ci = await c.info(true);
assert(ci.name.startsWith("hello"));

await assertRejects(
() => {
return js.consumers.get("A", { name_prefix: "" });
},
Error,
"name_prefix name required",
);

await assertRejects(
() => {
return js.consumers.get("A", { name_prefix: "one.two" });
},
Error,
"invalid name_prefix name - name_prefix name cannot contain '.'",
);

await cleanup(ns, nc);
});
Loading