kafka-ts 1.1.1 → 1.1.2-beta.1

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 {
@@ -43,7 +45,9 @@ class Producer {
43
45
  const { allowTopicAutoCreation } = this.options;
44
46
  const defaultTimestamp = BigInt(Date.now());
45
47
  const topics = new Set(messages.map((message) => message.topic));
46
- await this.metadata.fetchMetadataIfNecessary({ topics, allowTopicAutoCreation });
48
+ await this.lock.acquire([...topics], async () => {
49
+ await this.metadata.fetchMetadataIfNecessary({ topics, allowTopicAutoCreation });
50
+ });
47
51
  const partitionedMessages = messages.map((message) => {
48
52
  message.partition = this.partition(message);
49
53
  return message;
@@ -113,18 +117,26 @@ class Producer {
113
117
  if (error instanceof error_1.KafkaTSApiError && error.errorCode === api_1.API_ERROR.OUT_OF_ORDER_SEQUENCE_NUMBER) {
114
118
  await this.initProducerId();
115
119
  }
120
+ logger_1.log.warn('Reconnecting producer due to an unhandled error', { error });
121
+ try {
122
+ await this.cluster.disconnect();
123
+ await this.cluster.connect();
124
+ }
125
+ catch (error) {
126
+ logger_1.log.warn('Failed to reconnect producer', { error });
127
+ }
116
128
  throw error;
117
129
  }
118
130
  }
119
131
  async close() {
120
132
  await this.cluster.disconnect();
121
133
  }
122
- async ensureConnected() {
134
+ ensureConnected = (0, shared_1.shared)(async () => {
123
135
  await this.cluster.ensureConnected();
124
136
  if (!this.producerId) {
125
137
  await this.initProducerId();
126
138
  }
127
- }
139
+ });
128
140
  async initProducerId() {
129
141
  try {
130
142
  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.1",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",