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

[CHANGE] [SRV] name and id are used in whatever case they are specified - this means that for monitoring and discovery, names/ids are significant #441

Merged
merged 1 commit into from
Dec 20, 2022
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
13 changes: 7 additions & 6 deletions examples/services/01_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
const nc = await connect({ servers: ["demo.nats.io"] });

// this is a pseudo JSON schema - current requirements is that it is a string
// so more conviniently return an URL to your schemas.
// so more conveniently return an URL to your schemas.
const schema: SchemaInfo = {
request: JSON.stringify({
type: "array",
Expand All @@ -42,7 +42,7 @@ const schema: SchemaInfo = {

// All services have some basic stats that are collected like the
// number of requests processed, etc. Your service can accumulate
// other stats, and agregate them to the stats report that you
// other stats, and aggregate them to the stats report that you
// can retrieve via the monitoring stats() api.
// In this example, the service keeps track of the largest number
// it has seen and defines a custom statsHandler that aggregates
Expand Down Expand Up @@ -206,14 +206,15 @@ await collect(m.stats("max", found[0].id));
// addressed to a specific service instance.

// All monitoring subjects have the format:
// $SRV.<VERB>.<NAME>.<ID>
// $SRV.<VERB>.<name>.<id>
// where the verb can be 'PING', 'INFO', 'STATS' or 'SCHEMA'
// the name is optional but matches a service name you know in UPPERCASE.
// the id is also optional, but you must know it (from ping or one of
// the name is optional but matches a service name.
// The id is also optional, but you must know it (from ping or one of
// other requests that allowed you to discover the service) to
// target the service specifically as we do here:
const stats = JSONCodec<ServiceStats>().decode(
(await nc.request(`$SRV.STATS.MAX.${found[0].id}`)).data,
// note the name of the service matches in case what was specified
(await nc.request(`$SRV.STATS.max.${found[0].id}`)).data,
);
assertEquals(stats.name, "max");
assertEquals(stats.num_requests, 3);
Expand Down
13 changes: 7 additions & 6 deletions nats-base-client/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ export class ServiceImpl extends QueuedIteratorImpl<ServiceMsg>
if (name === "" && id === "") {
return `${pre}.${verb}`;
}
name = name.toUpperCase();
id = id.toUpperCase();
return id !== ""
? `${pre}.${verb}.${name}.${id}`
: `${pre}.${verb}.${name}`;
validName(name);
if (id !== "") {
validName(id);
return `${pre}.${verb}.${name}.${id}`;
}
return `${pre}.${verb}.${name}`;
}

constructor(
Expand All @@ -328,7 +329,7 @@ export class ServiceImpl extends QueuedIteratorImpl<ServiceMsg>
) {
super();
this.nc = nc;
config.name = config?.name?.toUpperCase() || "";
config.name = config?.name || "";
this.config = config;
const n = validName(this.name);
if (n !== "") {
Expand Down
3 changes: 1 addition & 2 deletions tests/helpers/service_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ root.addFlag({
});

function filter<T extends ServiceIdentity>(name: string, responses: T[]): T[] {
const n = name.toUpperCase();
return responses.filter((r) => {
return r.name === n;
return r.name === name;
});
}
function filterExpectingOnly<T extends ServiceIdentity>(
Expand Down
38 changes: 28 additions & 10 deletions tests/service_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ import {
Deno.test("service - control subject", () => {
const test = (verb: ServiceVerb) => {
assertEquals(ServiceImpl.controlSubject(verb), `$SRV.${verb}`);
assertEquals(ServiceImpl.controlSubject(verb, "name"), `$SRV.${verb}.NAME`);
assertEquals(ServiceImpl.controlSubject(verb, "NamE"), `$SRV.${verb}.NamE`);
assertEquals(
ServiceImpl.controlSubject(verb, "name", "id"),
`$SRV.${verb}.NAME.ID`,
ServiceImpl.controlSubject(verb, "nAmE", "Id"),
`$SRV.${verb}.nAmE.Id`,
);
assertEquals(
ServiceImpl.controlSubject(verb, "name", "id", "hello.service"),
`hello.service.${verb}.NAME.ID`,
ServiceImpl.controlSubject(verb, "nAMe", "iD", "hello.service"),
`hello.service.${verb}.nAMe.iD`,
);
};
[ServiceVerb.INFO, ServiceVerb.PING, ServiceVerb.SCHEMA, ServiceVerb.STATS]
Expand Down Expand Up @@ -761,13 +761,31 @@ Deno.test("service - cross platform service test", async () => {
]);

if (!status.success) {
const sc = StringCodec();
// console.log(sc.decode(stdout))
console.log(sc.decode(stderr));
console.log(sc.decode(stdout));
fail(sc.decode(stderr));
fail(StringCodec().decode(stderr));
}
p.close();

await nc.close();
});

Deno.test("service - stats name respects assigned name", async () => {
const { ns, nc } = await setup();
const test = await nc.services.add({
name: "tEsT",
// @ts-ignore: testing
version: "0.0.1",
endpoint: {
subject: "q",
handler: (err, msg) => {
msg.respond();
},
},
});
const stats = await test.stats();
assertEquals(stats.name, "tEsT");
const r = await nc.request(`$SRV.PING.tEsT`);
const si = JSONCodec<ServiceIdentity>().decode(r.data);
assertEquals(si.name, "tEsT");

await cleanup(ns, nc);
});