@@ -244,7 +244,7 @@ Baton Consumer::Subscribe(std::vector<std::string> topics) {
244
244
return Baton (RdKafka::ERR_NO_ERROR);
245
245
}
246
246
247
- NodeKafka::Message* Consumer::Consume () {
247
+ NodeKafka::Message* Consumer::Consume (int timeout_ms ) {
248
248
NodeKafka::Message* m;
249
249
250
250
if (IsConnected ()) {
@@ -254,7 +254,7 @@ NodeKafka::Message* Consumer::Consume() {
254
254
} else {
255
255
RdKafka::KafkaConsumer* consumer =
256
256
dynamic_cast <RdKafka::KafkaConsumer*>(m_client);
257
- m = new NodeKafka::Message (consumer->consume (1000 ));
257
+ m = new NodeKafka::Message (consumer->consume (timeout_ms ));
258
258
259
259
if (m->ConsumerShouldStop ()) {
260
260
Unsubscribe ();
@@ -720,39 +720,64 @@ NAN_METHOD(Consumer::NodeSubscribeSync) {
720
720
NAN_METHOD (Consumer::NodeConsumeLoop) {
721
721
Nan::HandleScope scope;
722
722
723
- if (info.Length () < 1 ) {
723
+ if (info.Length () < 2 ) {
724
724
// Just throw an exception
725
725
return Nan::ThrowError (" Invalid number of parameters" );
726
726
}
727
727
728
- if (!info[0 ]->IsFunction ()) {
728
+ if (!info[0 ]->IsNumber ()) {
729
+ return Nan::ThrowError (" Need to specify a timeout" );
730
+ }
731
+
732
+ if (!info[1 ]->IsFunction ()) {
729
733
return Nan::ThrowError (" Need to specify a callback" );
730
734
}
731
735
736
+ int timeout_ms;
737
+ Nan::Maybe<uint32_t > maybeTimeout =
738
+ Nan::To<uint32_t >(info[0 ].As <v8::Number>());
739
+
740
+ if (maybeTimeout.IsNothing ()) {
741
+ timeout_ms = 1000 ;
742
+ } else {
743
+ timeout_ms = static_cast <int >(maybeTimeout.FromJust ());
744
+ }
745
+
732
746
Consumer* consumer = ObjectWrap::Unwrap<Consumer>(info.This ());
733
747
734
- v8::Local<v8::Function> cb = info[0 ].As <v8::Function>();
748
+ v8::Local<v8::Function> cb = info[1 ].As <v8::Function>();
735
749
736
750
Nan::Callback *callback = new Nan::Callback (cb);
737
- Nan::AsyncQueueWorker (new Workers::ConsumerConsumeLoop (callback, consumer));
751
+ Nan::AsyncQueueWorker (
752
+ new Workers::ConsumerConsumeLoop (callback, consumer, timeout_ms));
738
753
739
754
info.GetReturnValue ().Set (Nan::Null ());
740
755
}
741
756
742
757
NAN_METHOD (Consumer::NodeConsume) {
743
758
Nan::HandleScope scope;
744
759
745
- if (info.Length () < 1 ) {
760
+ if (info.Length () < 2 ) {
746
761
// Just throw an exception
747
762
return Nan::ThrowError (" Invalid number of parameters" );
748
763
}
749
764
750
- if (info[0 ]->IsNumber ()) {
751
- if (!info[1 ]->IsFunction ()) {
765
+ int timeout_ms;
766
+ Nan::Maybe<uint32_t > maybeTimeout =
767
+ Nan::To<uint32_t >(info[0 ].As <v8::Number>());
768
+
769
+ if (maybeTimeout.IsNothing ()) {
770
+ timeout_ms = 1000 ;
771
+ } else {
772
+ timeout_ms = static_cast <int >(maybeTimeout.FromJust ());
773
+ }
774
+
775
+ if (info[1 ]->IsNumber ()) {
776
+ if (!info[2 ]->IsFunction ()) {
752
777
return Nan::ThrowError (" Need to specify a callback" );
753
778
}
754
779
755
- v8::Local<v8::Number> numMessagesNumber = info[0 ].As <v8::Number>();
780
+ v8::Local<v8::Number> numMessagesNumber = info[1 ].As <v8::Number>();
756
781
Nan::Maybe<uint32_t > numMessagesMaybe = Nan::To<uint32_t >(numMessagesNumber); // NOLINT
757
782
758
783
uint32_t numMessages;
@@ -764,21 +789,22 @@ NAN_METHOD(Consumer::NodeConsume) {
764
789
765
790
Consumer* consumer = ObjectWrap::Unwrap<Consumer>(info.This ());
766
791
767
- v8::Local<v8::Function> cb = info[1 ].As <v8::Function>();
792
+ v8::Local<v8::Function> cb = info[2 ].As <v8::Function>();
768
793
Nan::Callback *callback = new Nan::Callback (cb);
769
794
Nan::AsyncQueueWorker (
770
- new Workers::ConsumerConsumeNum (callback, consumer, numMessages));
795
+ new Workers::ConsumerConsumeNum (callback, consumer, numMessages, timeout_ms )); // NOLINT
771
796
772
797
} else {
773
- if (!info[0 ]->IsFunction ()) {
798
+ if (!info[1 ]->IsFunction ()) {
774
799
return Nan::ThrowError (" Need to specify a callback" );
775
800
}
776
801
777
802
Consumer* consumer = ObjectWrap::Unwrap<Consumer>(info.This ());
778
803
779
- v8::Local<v8::Function> cb = info[0 ].As <v8::Function>();
804
+ v8::Local<v8::Function> cb = info[1 ].As <v8::Function>();
780
805
Nan::Callback *callback = new Nan::Callback (cb);
781
- Nan::AsyncQueueWorker (new Workers::ConsumerConsume (callback, consumer));
806
+ Nan::AsyncQueueWorker (
807
+ new Workers::ConsumerConsume (callback, consumer, timeout_ms));
782
808
}
783
809
784
810
info.GetReturnValue ().Set (Nan::Null ());
0 commit comments