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

[FIX] more robust watcher in case consumer is recreated while the cluster is flapping #693

Merged
merged 1 commit into from Apr 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 32 additions & 8 deletions jetstream/jsbaseclient_api.ts
Expand Up @@ -15,13 +15,15 @@

import { Empty } from "../nats-base-client/encoders.ts";
import { Codec, JSONCodec } from "../nats-base-client/codec.ts";
import { extend } from "../nats-base-client/util.ts";
import { backoff, delay, extend } from "../nats-base-client/util.ts";
import { NatsConnectionImpl } from "../nats-base-client/nats.ts";
import { checkJsErrorCode } from "./jsutil.ts";
import {
ErrorCode,
JetStreamOptions,
Msg,
NatsConnection,
NatsError,
RequestOptions,
} from "../nats-base-client/core.ts";
import { ApiResponse } from "./jsapi_types.ts";
Expand Down Expand Up @@ -81,7 +83,7 @@ export class BaseApiClient {
async _request(
subj: string,
data: unknown = null,
opts?: RequestOptions,
opts?: Partial<RequestOptions> & { retries?: number },
): Promise<unknown> {
opts = opts || {} as RequestOptions;
opts.timeout = this.timeout;
Expand All @@ -91,12 +93,34 @@ export class BaseApiClient {
a = this.jc.encode(data);
}

const m = await this.nc.request(
subj,
a,
opts,
);
return this.parseJsResponse(m);
let { retries } = opts as {
retries: number;
};

retries = retries || 1;
retries = retries === -1 ? Number.MAX_SAFE_INTEGER : retries;
const bo = backoff();

for (let i = 0; i < retries; i++) {
try {
const m = await this.nc.request(
subj,
a,
opts as RequestOptions,
);
return this.parseJsResponse(m);
} catch (err) {
const ne = err as NatsError;
if (
(ne.code === "503" || ne.code === ErrorCode.Timeout) &&
i + 1 < retries
) {
await delay(bo.backoff(i));
} else {
throw err;
}
}
}
}

async findStream(subject: string): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion jetstream/jsclient.ts
Expand Up @@ -792,7 +792,7 @@ export class JetStreamSubscriptionImpl extends TypedSubscription<JsMsg>

const subj = `${info.api.prefix}.CONSUMER.CREATE.${info.stream}`;

this.js._request(subj, req)
this.js._request(subj, req, { retries: -1 })
.then((v) => {
const ci = v as ConsumerInfo;
this.info!.config = ci.config;
Expand Down