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

Move error handling of Consumer.consume timeouts to Node and allow to disable #443

Closed
Closed
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
22 changes: 18 additions & 4 deletions lib/kafka-consumer.js
Expand Up @@ -15,6 +15,7 @@ var util = require('util');
var Kafka = require('../librdkafka');
var KafkaConsumerStream = require('./kafka-consumer-stream');
var LibrdKafkaError = require('./error');
var ErrorCode = LibrdKafkaError.codes;
var TopicPartition = require('./topic-partition');

util.inherits(KafkaConsumer, Client);
Expand Down Expand Up @@ -362,9 +363,13 @@ KafkaConsumer.prototype.unsubscribe = function() {
* @param {KafkaConsumer~readCallback} cb - Callback to return when a message
* is fetched.
*/
KafkaConsumer.prototype.consume = function(number, cb) {
KafkaConsumer.prototype.consume = function(number, options, cb) {
var timeoutMs = this._consumeTimeout || 1000;
var self = this;
if (typeof options === 'function' && !cb) {
cb = options;
options = {};
}

if ((number && typeof number === 'number') || (number && cb)) {

Expand All @@ -374,7 +379,7 @@ KafkaConsumer.prototype.consume = function(number, cb) {
throw new TypeError('Callback must be a function');
}

this._consumeNum(timeoutMs, number, cb);
this._consumeNum(timeoutMs, number, options, cb);
} else {

// See https://github.com/Blizzard/node-rdkafka/issues/220
Expand Down Expand Up @@ -434,14 +439,23 @@ KafkaConsumer.prototype._consumeLoop = function(timeoutMs, cb) {
* @private
* @see consume
*/
KafkaConsumer.prototype._consumeNum = function(timeoutMs, numMessages, cb) {
KafkaConsumer.prototype._consumeNum = function(timeoutMs, numMessages, options, cb) {
var self = this;

this._client.consume(timeoutMs, numMessages, function(err, messages) {
if (err) {
err = LibrdKafkaError.create(err);

if (cb) {
cb(err);
if (
!options.timeoutErrors &&
(ErrorCode.ERR__TIMED_OUT === err.code || ErrorCode.ERR__TIMED_OUT_QUEUE === err.code)
) {
cb(null, []);
} else {
cb(err);

}
}
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/workers.cc
Expand Up @@ -533,6 +533,7 @@ void KafkaConsumerConsumeNum::Execute() {
case RdKafka::ERR__TIMED_OUT:
case RdKafka::ERR__TIMED_OUT_QUEUE:
// Break of the loop if we timed out
SetErrorBaton(b);
looping = false;
break;
case RdKafka::ERR_NO_ERROR:
Expand Down