Skip to content

Commit

Permalink
requestMany - JitterTimer is now called 'stall' and RequestStrategy i…
Browse files Browse the repository at this point in the history
…s type alias to simple strings. (#152)

* change(core): requestMany() JitterTimer and its associated `jitter` property  is now called "stall"
* change(core): requestMany() `RequestStrategy` is now a simple string "timer" | "count" | "stall" | "sentinel"

---------

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart authored Nov 21, 2024
1 parent d4643a6 commit 3ced2f5
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 112 deletions.
4 changes: 2 additions & 2 deletions core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-42",
"version": "3.0.0-45",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down Expand Up @@ -35,7 +35,7 @@
"test": "deno test -A --parallel --reload tests/ --import-map=./import_map.json"
},
"imports": {
"@nats-io/nkeys": "jsr:@nats-io/nkeys@~2.0.0-2",
"@nats-io/nkeys": "jsr:@nats-io/nkeys@~2.0.0-3",
"@nats-io/nuid": "jsr:@nats-io/[email protected]"
}
}
4 changes: 2 additions & 2 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-42",
"version": "3.0.0-45",
"files": [
"lib/",
"LICENSE",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"description": "nats-core library - this library implements all the base functionality for NATS javascript clients",
"dependencies": {
"@nats-io/nkeys": "2.0.0-2",
"@nats-io/nkeys": "2.0.0-3",
"@nats-io/nuid": "2.0.1-2"
},
"devDependencies": {
Expand Down
9 changes: 2 additions & 7 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,15 @@ export interface RequestOptions {
reply?: string;
}

export enum RequestStrategy {
Timer = "timer",
Count = "count",
JitterTimer = "jitterTimer",
SentinelMsg = "sentinelMsg",
}
export type RequestStrategy = "timer" | "count" | "stall" | "sentinel";

export interface RequestManyOptions {
strategy: RequestStrategy;
maxWait: number;
headers?: MsgHdrs;
maxMessages?: number;
noMux?: boolean;
jitter?: number;
stall?: number;
}

export interface Stats {
Expand Down
3 changes: 2 additions & 1 deletion core/src/internal_mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export type {
Request,
RequestManyOptions,
RequestOptions,
RequestStrategy,
ReviverFn,
Server,
ServerErrorStatus,
Expand All @@ -126,7 +127,7 @@ export type {
TokenAuth,
UserPass,
} from "./core.ts";
export { createInbox, Match, RequestStrategy, syncIterator } from "./core.ts";
export { createInbox, Match, syncIterator } from "./core.ts";
export { SubscriptionImpl, Subscriptions } from "./protocol.ts";

export type {
Expand Down
2 changes: 1 addition & 1 deletion core/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export {
PermissionViolationError,
ProtocolError,
RequestError,
RequestStrategy,
syncIterator,
TimeoutError,
tokenAuthenticator,
Expand Down Expand Up @@ -88,6 +87,7 @@ export type {
ReconnectStatus,
RequestManyOptions,
RequestOptions,
RequestStrategy,
ReviverFn,
ServerErrorStatus,
ServerInfo,
Expand Down
10 changes: 5 additions & 5 deletions core/src/nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import type {
Subscription,
SubscriptionOptions,
} from "./core.ts";
import { createInbox, RequestStrategy } from "./core.ts";
import { createInbox } from "./core.ts";
import { errors, InvalidArgumentError, TimeoutError } from "./errors.ts";

export class NatsConnectionImpl implements NatsConnection {
Expand Down Expand Up @@ -182,7 +182,7 @@ export class NatsConnectionImpl implements NatsConnection {
return Promise.reject(err);
}

opts.strategy = opts.strategy || RequestStrategy.Timer;
opts.strategy = opts.strategy || "timer";
opts.maxWait = opts.maxWait || 1000;
if (opts.maxWait < 1) {
return Promise.reject(
Expand Down Expand Up @@ -236,19 +236,19 @@ export class NatsConnectionImpl implements NatsConnection {
// push the message
callback(null, msg);
// see if the m request is completed
if (opts.strategy === RequestStrategy.Count) {
if (opts.strategy === "count") {
max--;
if (max === 0) {
cancel();
}
}
if (opts.strategy === RequestStrategy.JitterTimer) {
if (opts.strategy === "stall") {
clearTimers();
timer = setTimeout(() => {
cancel();
}, 300);
}
if (opts.strategy === RequestStrategy.SentinelMsg) {
if (opts.strategy === "sentinel") {
if (msg && msg.data.length === 0) {
cancel();
}
Expand Down
9 changes: 4 additions & 5 deletions core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type {
RequestManyOptions,
RequestOptions,
} from "./core.ts";
import { RequestStrategy } from "./core.ts";
import { errors, RequestError, TimeoutError } from "./errors.ts";

export class BaseRequest {
Expand Down Expand Up @@ -105,22 +104,22 @@ export class RequestMany extends BaseRequest implements Request {
this.cancel(err as Error);
} else {
this.callback(null, msg);
if (this.opts.strategy === RequestStrategy.Count) {
if (this.opts.strategy === "count") {
this.max--;
if (this.max === 0) {
this.cancel();
}
}

if (this.opts.strategy === RequestStrategy.JitterTimer) {
if (this.opts.strategy === "stall") {
clearTimeout(this.timer);
// @ts-ignore: node is not a number
this.timer = setTimeout(() => {
this.cancel();
}, this.opts.jitter || 300);
}, this.opts.stall || 300);
}

if (this.opts.strategy === RequestStrategy.SentinelMsg) {
if (this.opts.strategy === "sentinel") {
if (msg && msg.data.length === 0) {
this.cancel();
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is generated - do not edit
export const version = "3.0.0-42";
export const version = "3.0.0-45";
13 changes: 6 additions & 7 deletions core/tests/basics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
headers,
isIP,
nuid,
RequestStrategy,
syncIterator,
} from "../src/internal_mod.ts";
import type {
Expand Down Expand Up @@ -1036,7 +1035,7 @@ Deno.test("basics - request many count", async () => {
const lock = Lock(5, 2000);

const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Count,
strategy: "count",
maxWait: 2000,
maxMessages: 5,
});
Expand Down Expand Up @@ -1067,7 +1066,7 @@ Deno.test("basics - request many jitter", async () => {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.JitterTimer,
strategy: "stall",
maxWait: 5000,
});
for await (const mer of iter) {
Expand Down Expand Up @@ -1099,7 +1098,7 @@ Deno.test("basics - request many sentinel", async () => {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.SentinelMsg,
strategy: "sentinel",
maxWait: 2000,
});
for await (const mer of iter) {
Expand Down Expand Up @@ -1130,7 +1129,7 @@ Deno.test("basics - request many sentinel - partial response", async () => {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.SentinelMsg,
strategy: "sentinel",
maxWait: 2000,
});
for await (const mer of iter) {
Expand Down Expand Up @@ -1159,7 +1158,7 @@ Deno.test("basics - request many wait for timer - no respone", async () => {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Timer,
strategy: "timer",
maxWait: 2000,
});
for await (const mer of iter) {
Expand Down Expand Up @@ -1189,7 +1188,7 @@ Deno.test("basics - request many waits for timer late response", async () => {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Timer,
strategy: "timer",
maxWait: 2000,
});
for await (const mer of iter) {
Expand Down
28 changes: 11 additions & 17 deletions core/tests/mrequest_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ import type {
NatsConnectionImpl,
QueuedIteratorImpl,
} from "../src/internal_mod.ts";
import {
createInbox,
deferred,
delay,
Empty,
RequestStrategy,
} from "../src/internal_mod.ts";
import { createInbox, deferred, delay, Empty } from "../src/internal_mod.ts";

import { assert, assertEquals, assertRejects, fail } from "jsr:@std/assert";
import { errors } from "../src/errors.ts";
Expand All @@ -47,7 +41,7 @@ async function requestManyCount(noMux = false): Promise<void> {
});

const iter = await nci.requestMany(subj, "hello", {
strategy: RequestStrategy.Count,
strategy: "count",
maxWait: 2000,
maxMessages: 5,
noMux,
Expand Down Expand Up @@ -89,7 +83,7 @@ async function requestManyJitter(noMux = false): Promise<void> {
const start = Date.now();

const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.JitterTimer,
strategy: "stall",
maxWait: 5000,
noMux,
});
Expand Down Expand Up @@ -133,7 +127,7 @@ async function requestManySentinel(

const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.SentinelMsg,
strategy: "sentinel",
maxWait: 2000,
noMux,
});
Expand Down Expand Up @@ -180,7 +174,7 @@ async function requestManyTimerNoResponse(noMux = false): Promise<void> {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Timer,
strategy: "timer",
maxWait: 2000,
noMux,
});
Expand Down Expand Up @@ -219,7 +213,7 @@ async function requestTimerLateResponse(noMux = false): Promise<void> {
let count = 0;
const start = Date.now();
const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Timer,
strategy: "timer",
maxWait: 2000,
noMux,
});
Expand Down Expand Up @@ -250,7 +244,7 @@ async function requestManyStopsOnError(noMux = false): Promise<void> {
const subj = createInbox();

const iter = await nci.requestMany(subj, Empty, {
strategy: RequestStrategy.Timer,
strategy: "timer",
maxWait: 2000,
noMux,
});
Expand Down Expand Up @@ -299,7 +293,7 @@ Deno.test("mreq - pub permission error", async () => {
})().then();

const iter = await nc.requestMany("q", Empty, {
strategy: RequestStrategy.Count,
strategy: "count",
maxMessages: 3,
maxWait: 2000,
});
Expand Down Expand Up @@ -351,7 +345,7 @@ Deno.test("mreq - sub permission error", async () => {
await assertRejects(
async () => {
const iter = await nc.requestMany("q", Empty, {
strategy: RequestStrategy.Count,
strategy: "count",
maxMessages: 3,
maxWait: 2000,
noMux: true,
Expand Down Expand Up @@ -412,9 +406,9 @@ Deno.test("mreq - lost sub permission", async () => {
})().then();

const iter = await nc.requestMany("q", Empty, {
strategy: RequestStrategy.Count,
strategy: "count",
maxMessages: 100,
jitter: 2000,
stall: 2000,
maxWait: 2000,
noMux: true,
}) as QueuedIteratorImpl<Msg>;
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
"./obj",
"./services"
],
"nodeModulesDir": "auto"
"nodeModulesDir": "none"
}
4 changes: 2 additions & 2 deletions jetstream/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/jetstream",
"version": "3.0.0-31",
"version": "3.0.0-32",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down Expand Up @@ -33,6 +33,6 @@
"test": "deno test -A --parallel --reload --trace-leaks --quiet tests/ --import-map=import_map.json"
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-42"
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-45"
}
}
6 changes: 3 additions & 3 deletions jetstream/import_map.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"imports": {
"@nats-io/nkeys": "jsr:@nats-io/nkeys@~2.0.0-2",
"@nats-io/nkeys": "jsr:@nats-io/nkeys@~2.0.0-3",
"@nats-io/nuid": "jsr:@nats-io/nuid@~2.0.1-2",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-42",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-42/internal",
"@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-45",
"@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-45/internal",
"test_helpers": "../test_helpers/mod.ts",
"@std/io": "jsr:@std/[email protected]"
}
Expand Down
4 changes: 2 additions & 2 deletions jetstream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/jetstream",
"version": "3.0.0-31",
"version": "3.0.0-32",
"files": [
"lib/",
"LICENSE",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"description": "jetstream library - this library implements all the base functionality for NATS JetStream for javascript clients",
"dependencies": {
"@nats-io/nats-core": "3.0.0-42"
"@nats-io/nats-core": "3.0.0-45"
},
"devDependencies": {
"@types/node": "^22.7.6",
Expand Down
Loading

0 comments on commit 3ced2f5

Please sign in to comment.