kafka-ts 1.1.1 → 1.1.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cluster.js CHANGED
@@ -50,6 +50,9 @@ class Cluster {
50
50
  return this.brokerById[nodeId].sendRequest(...args);
51
51
  };
52
52
  async acquireBroker(nodeId) {
53
+ if (!(nodeId in this.brokerMetadata)) {
54
+ throw new error_1.KafkaTSError(`Broker ${nodeId} is not available`);
55
+ }
53
56
  const broker = new broker_1.Broker({
54
57
  clientId: this.options.clientId,
55
58
  sasl: this.options.sasl,
@@ -17,6 +17,8 @@ const metadata_1 = require("../metadata");
17
17
  const delay_1 = require("../utils/delay");
18
18
  const error_1 = require("../utils/error");
19
19
  const lock_1 = require("../utils/lock");
20
+ const logger_1 = require("../utils/logger");
21
+ const shared_1 = require("../utils/shared");
20
22
  const tracer_1 = require("../utils/tracer");
21
23
  const trace = (0, tracer_1.createTracer)('Producer');
22
24
  class Producer {
@@ -113,18 +115,26 @@ class Producer {
113
115
  if (error instanceof error_1.KafkaTSApiError && error.errorCode === api_1.API_ERROR.OUT_OF_ORDER_SEQUENCE_NUMBER) {
114
116
  await this.initProducerId();
115
117
  }
118
+ logger_1.log.warn('Reconnecting producer due to an unhandled error', { error });
119
+ try {
120
+ await this.cluster.disconnect();
121
+ await this.cluster.connect();
122
+ }
123
+ catch (error) {
124
+ logger_1.log.warn('Failed to reconnect producer', { error });
125
+ }
116
126
  throw error;
117
127
  }
118
128
  }
119
129
  async close() {
120
130
  await this.cluster.disconnect();
121
131
  }
122
- async ensureConnected() {
132
+ ensureConnected = (0, shared_1.shared)(async () => {
123
133
  await this.cluster.ensureConnected();
124
134
  if (!this.producerId) {
125
135
  await this.initProducerId();
126
136
  }
127
- }
137
+ });
128
138
  async initProducerId() {
129
139
  try {
130
140
  const result = await this.cluster.sendRequest(api_1.API.INIT_PRODUCER_ID, {
@@ -0,0 +1 @@
1
+ export declare const shared: <F extends () => Promise<any>>(func: F) => () => ReturnType<F>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shared = void 0;
4
+ const shared = (func) => {
5
+ let promise;
6
+ return () => {
7
+ if (!promise) {
8
+ promise = func();
9
+ promise.finally(() => {
10
+ promise = undefined;
11
+ });
12
+ }
13
+ return promise;
14
+ };
15
+ };
16
+ exports.shared = shared;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kafka-ts",
3
- "version": "1.1.1",
3
+ "version": "1.1.2-beta.0",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",