Skip to content

Commit 4f834db

Browse files
committed
breaking(services): the queue property now if set to an empty string means that the service/endpoint/group will not be in a queue. Clients that specifically assign queue names should experience no changes. And clients that didn't specify a value for the property (so the property was undefined) should also not experience a change and should see their service/endpoint/group use the q group. If explicetly set to an empty string ``, the service will NOT be part of a queue.
breaking(services): the metadata configuration specified when creating the service is now stored read-only, any type of edit on the internal copy will result in a `TypeError` as the metdata object is frozen. Signed-off-by: Alberto Ricart <[email protected]>
1 parent 5ab062e commit 4f834db

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

services/src/service.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class ServiceGroupImpl implements ServiceGroup {
162162
} else if (parent instanceof ServiceGroupImpl) {
163163
const sg = parent as ServiceGroupImpl;
164164
this.srv = sg.srv;
165-
if (queue === "" && sg.queue !== "") {
165+
if (queue === undefined) {
166166
queue = sg.queue;
167167
}
168168
root = sg.subject;
@@ -198,7 +198,10 @@ export class ServiceGroupImpl implements ServiceGroup {
198198
return this.srv._addEndpoint(ne);
199199
}
200200

201-
addGroup(name = "", queue = ""): ServiceGroup {
201+
addGroup(name = "", queue?: string): ServiceGroup {
202+
if (queue === undefined) {
203+
queue = this.queue;
204+
}
202205
return new ServiceGroupImpl(this, name, queue);
203206
}
204207
}
@@ -286,13 +289,18 @@ export class ServiceImpl implements Service {
286289
) {
287290
this.nc = nc;
288291
this.config = Object.assign({}, config);
289-
if (!this.config.queue) {
292+
if (this.config.queue === undefined) {
290293
this.config.queue = "q";
291294
}
292295

296+
// don't allow changing metadata
297+
config.metadata = Object.freeze(config.metadata || {});
298+
293299
// this will throw if no name
294300
validateName("name", this.config.name);
295-
validateName("queue", this.config.queue);
301+
if (this.config.queue) {
302+
validateName("queue", this.config.queue);
303+
}
296304

297305
// this will throw if not semver
298306
parseSemVer(this.config.version);

services/src/types.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export type Endpoint = {
5151
metadata?: Record<string, string>;
5252
/**
5353
* Optional queue group to run this particular endpoint in. The service's configuration
54-
* queue configuration will be used. See {@link ServiceConfig}.
54+
* queue configuration will be used. See {@link ServiceConfig}. Note that if the queue
55+
* is set to an empty string, it will not be run in a queue.
5556
*/
5657
queue?: string;
5758
};
@@ -81,6 +82,8 @@ export interface ServiceGroup {
8182
* without requiring editing of the service.
8283
* Note that an optional queue can be specified, all endpoints added to
8384
* the group, will use the specified queue unless the endpoint overrides it.
85+
* When not set, it uses the parent group configuration. An empty string
86+
* means no queue.
8487
* see {@link EndpointOptions} and {@link ServiceConfig}.
8588
* @param subject
8689
* @param queue
@@ -208,9 +211,11 @@ export type ServiceConfig = {
208211
*/
209212
metadata?: Record<string, string>;
210213
/**
211-
* Optional queue group to run the service in. By default,
212-
* then queue name is "q". Note that this configuration will
213-
* be the default for all endpoints and groups.
214+
* Optional queue group to run the service in. If not set, default,
215+
* is set to "q". Note that this configuration will
216+
* be the default for all endpoints and groups. If set to an empty
217+
* string, the service subscription will NOT have queue, and will
218+
* not be run in a queue.
214219
*/
215220
queue?: string;
216221
};

services/tests/service_test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,61 @@ Deno.test("service - endpoint default queue group", async () => {
10471047

10481048
await cleanup(ns, nc);
10491049
});
1050+
1051+
Deno.test("service - endpoint no queue group", async () => {
1052+
const { ns, nc } = await setup();
1053+
1054+
const svc = new Svc(nc);
1055+
const srv = await svc.add({
1056+
name: "example",
1057+
version: "0.0.1",
1058+
metadata: { service: "1" },
1059+
// no queue
1060+
queue: "",
1061+
}) as ServiceImpl;
1062+
1063+
// svc config doesn't specify a queue group so we expect q
1064+
srv.addEndpoint("a");
1065+
checkQueueGroup(srv, "a", "");
1066+
1067+
// we add another group, no queue
1068+
const dg = srv.addGroup("G");
1069+
dg.addEndpoint("a");
1070+
checkQueueGroup(srv, "G.a", "");
1071+
1072+
// the above have no queue, no override, and set a queue
1073+
const g = srv.addGroup("g", "qq");
1074+
g.addEndpoint("a");
1075+
checkQueueGroup(srv, "g.a", "qq");
1076+
// override
1077+
g.addEndpoint("b", { queue: "bb" });
1078+
checkQueueGroup(srv, "g.b", "bb");
1079+
// add a subgroup without, should inherit
1080+
const g2 = g.addGroup("g");
1081+
g2.addEndpoint("a");
1082+
checkQueueGroup(srv, "g.g.a", "qq");
1083+
1084+
await cleanup(ns, nc);
1085+
});
1086+
1087+
Deno.test("service - metadata is not editable", async () => {
1088+
const { ns, nc } = await setup();
1089+
1090+
const svc = new Svc(nc);
1091+
const srv = await svc.add({
1092+
name: "example",
1093+
version: "0.0.1",
1094+
metadata: { service: "1", hello: "world" },
1095+
queue: "",
1096+
}) as ServiceImpl;
1097+
1098+
assertThrows(
1099+
() => {
1100+
srv.config.metadata!.hello = "hello";
1101+
},
1102+
TypeError,
1103+
"Cannot assign to read only property",
1104+
);
1105+
1106+
await cleanup(ns, nc);
1107+
});

0 commit comments

Comments
 (0)