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 rdkafka consumer hanging when disconnecting #13144

Merged
merged 5 commits into from
Dec 1, 2022
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export class RdkafkaConsumer extends RdkafkaBase implements IConsumer {
this.zooKeeperClient = new this.consumerOptions.zooKeeperClientConstructor(zooKeeperEndpoint);
}

let consumer: kafkaTypes.KafkaConsumer | undefined;

const options: kafkaTypes.ConsumerGlobalConfig = {
"metadata.broker.list": this.endpoints.kafka.join(","),
"socket.keepalive.enable": true,
Expand All @@ -108,12 +110,14 @@ export class RdkafkaConsumer extends RdkafkaBase implements IConsumer {
"fetch.min.bytes": 1,
"fetch.max.bytes": 1024 * 1024,
"offset_commit_cb": true,
"rebalance_cb": this.consumerOptions.optimizedRebalance ? this.rebalance.bind(this) : true,
"rebalance_cb": this.consumerOptions.optimizedRebalance ?
(err: kafkaTypes.LibrdKafkaError, assignments: kafkaTypes.Assignment[]) => this.rebalance(consumer, err, assignments) :
true,
...this.consumerOptions.additionalOptions,
...this.sslOptions,
};

const consumer: kafkaTypes.KafkaConsumer = this.consumer =
consumer = this.consumer =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the build is failing because it says consumer.subscribe() in line 127 below and consumer.consume() could possibly be undefined

src/rdkafkaConsumer.ts:127:4 - error TS2532: Object is possibly 'undefined'.

new this.kafka.KafkaConsumer(options, { "auto.offset.reset": "latest" });

consumer.setDefaultConsumeTimeout(this.consumerOptions.consumeTimeout);
Expand Down Expand Up @@ -435,8 +439,8 @@ export class RdkafkaConsumer extends RdkafkaBase implements IConsumer {
* The default node-rdkafka consumer rebalance callback with the addition
* of continuing from the last seen offset for assignments that have not changed
*/
private rebalance(err: kafkaTypes.LibrdKafkaError, assignments: kafkaTypes.Assignment[]) {
if (!this.consumer) {
private rebalance(consumer: kafkaTypes.KafkaConsumer | undefined, err: kafkaTypes.LibrdKafkaError, assignments: kafkaTypes.Assignment[]) {
if (!consumer) {
return;
}

Expand All @@ -452,13 +456,13 @@ export class RdkafkaConsumer extends RdkafkaBase implements IConsumer {
}
}

this.consumer.assign(assignments);
consumer.assign(assignments);
} else if (err.code === this.kafka.CODES.ERRORS.ERR__REVOKE_PARTITIONS) {
this.consumer.unassign();
consumer.unassign();
}
} catch (ex) {
if (this.consumer.isConnected()) {
this.consumer.emit("rebalance.error", ex);
if (consumer.isConnected()) {
consumer.emit("rebalance.error", ex);
}
}
}
Expand Down