7
7
* of the MIT license. See the LICENSE.txt file for details.
8
8
*/
9
9
10
- #define THREADED_CONSUME
11
-
12
10
#include < string>
13
11
#include < vector>
14
12
@@ -35,26 +33,12 @@ KafkaConsumer::KafkaConsumer(Conf* gconfig, Conf* tconfig):
35
33
36
34
m_gconfig->set (" default_topic_conf" , m_tconfig, errstr);
37
35
38
- consume_callback = nullptr ;
39
-
40
- uv_async_init (Nan::GetCurrentEventLoop (), &consume_async, ConsumeMessage);
41
- consume_async.data = this ;
42
-
43
- uv_mutex_init (&consume_messages_lock);
36
+ m_consume_loop = nullptr ;
44
37
}
45
38
46
39
KafkaConsumer::~KafkaConsumer () {
47
40
// We only want to run this if it hasn't been run already
48
41
Disconnect ();
49
-
50
- if (consume_callback != nullptr ) {
51
- consume_callback->Reset ();
52
- consume_callback = nullptr ;
53
- }
54
-
55
- uv_mutex_destroy (&consume_messages_lock);
56
-
57
- uv_close ((uv_handle_t *) &consume_async, NULL );
58
42
}
59
43
60
44
Baton KafkaConsumer::Connect () {
@@ -108,6 +92,11 @@ Baton KafkaConsumer::Disconnect() {
108
92
}
109
93
}
110
94
95
+ if (m_consume_loop != nullptr ) {
96
+ delete m_consume_loop;
97
+ m_consume_loop = nullptr ;
98
+ }
99
+
111
100
m_is_closing = false ;
112
101
113
102
return Baton (err);
@@ -492,88 +481,6 @@ std::string KafkaConsumer::Name() {
492
481
return std::string (m_client->name ());
493
482
}
494
483
495
- void KafkaConsumer::ConsumeLoop (void *arg) {
496
- KafkaConsumer* consumer = (KafkaConsumer*)arg;
497
-
498
- bool looping = true ;
499
-
500
- while (consumer->IsConnected () && looping) {
501
- Baton b = consumer->Consume (consumer->consume_timeout_ms );
502
- RdKafka::ErrorCode ec = b.err ();
503
- if (ec == RdKafka::ERR_NO_ERROR) {
504
- RdKafka::Message *message = b.data <RdKafka::Message*>();
505
- switch (message->err ()) {
506
-
507
- case RdKafka::ERR_NO_ERROR: {
508
- // message is deleted after it's passed to the main event loop
509
-
510
- scoped_mutex_lock lock (consumer->consume_messages_lock );
511
- consumer->consume_messages .push_back (message);
512
-
513
- uv_async_send (&consumer->consume_async );
514
-
515
- break ;
516
- }
517
-
518
- case RdKafka::ERR__TIMED_OUT:
519
- case RdKafka::ERR__TIMED_OUT_QUEUE:
520
- case RdKafka::ERR__PARTITION_EOF: {
521
- delete message;
522
-
523
- // no need to wait here because the next Consume() call will handle the timeout?
524
-
525
- break ;
526
- }
527
-
528
- default :
529
- // Unknown error. We need to break out of this
530
- // SetErrorBaton(b);
531
- // TODO: pass error along?
532
-
533
- looping = false ;
534
- break ;
535
- }
536
-
537
- } else if (ec == RdKafka::ERR_UNKNOWN_TOPIC_OR_PART || ec == RdKafka::ERR_TOPIC_AUTHORIZATION_FAILED) {
538
- // bus.SendWarning(ec);
539
-
540
- } else {
541
- // Unknown error. We need to break out of this
542
- // SetErrorBaton(b);
543
- looping = false ;
544
- }
545
- }
546
- }
547
-
548
- void KafkaConsumer::ConsumeMessage (uv_async_t * handle) {
549
- Nan::HandleScope scope;
550
-
551
- KafkaConsumer* consumer = (KafkaConsumer*)handle->data ;
552
-
553
- std::vector<RdKafka::Message*> message_queue;
554
- // std::vector<RdKafka::ErrorCode> warning_queue;
555
-
556
- {
557
- scoped_mutex_lock lock (consumer->consume_messages_lock );
558
- // Copy the vector and empty it
559
- consumer->consume_messages .swap (message_queue);
560
- // m_asyncwarning.swap(warning_queue);
561
- }
562
-
563
- for (unsigned int i = 0 ; i < message_queue.size (); i++) {
564
- RdKafka::Message* message = message_queue[i];
565
- v8::Local<v8::Value> argv[] = {
566
- Nan::Null (),
567
- Conversion::Message::ToV8Object (message),
568
- Nan::Null (),
569
- };
570
-
571
- delete message;
572
-
573
- consumer->consume_callback ->Call (3 , argv);
574
- }
575
- }
576
-
577
484
Nan::Persistent<v8::Function> KafkaConsumer::constructor;
578
485
579
486
void KafkaConsumer::Init (v8::Local<v8::Object> exports) {
@@ -676,9 +583,6 @@ void KafkaConsumer::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
676
583
677
584
KafkaConsumer* consumer = new KafkaConsumer (gconfig, tconfig);
678
585
679
- v8::Local<v8::Object> context = v8::Local<v8::Object>::Cast (info[0 ]);
680
- consumer->consume_context .Reset (context);
681
-
682
586
// Wrap it
683
587
consumer->Wrap (info.This ());
684
588
@@ -1186,25 +1090,19 @@ NAN_METHOD(KafkaConsumer::NodeConsumeLoop) {
1186
1090
1187
1091
KafkaConsumer* consumer = ObjectWrap::Unwrap<KafkaConsumer>(info.This ());
1188
1092
1189
- v8::Local<v8::Function> cb = info[ 2 ]. As <v8::Function>();
1190
-
1191
- Nan::Callback *callback = new Nan::Callback (cb);
1093
+ if (consumer-> m_consume_loop != nullptr ) {
1094
+ return Nan::ThrowError ( " Consume was already called " );
1095
+ }
1192
1096
1193
- #ifdef THREADED_CONSUME
1194
- if (consumer->consume_callback != nullptr ) {
1195
- return Nan::ThrowError (" Consume was already called once" );
1097
+ if (!consumer->IsConnected ()) {
1098
+ return Nan::ThrowError (" Connect before calling consume" );
1196
1099
}
1197
1100
1198
- consumer->consume_timeout_ms = timeout_ms;
1199
- consumer->consume_retry_read_ms = retry_read_ms;
1200
- consumer->consume_callback = callback;
1101
+ v8::Local<v8::Function> cb = info[2 ].As <v8::Function>();
1201
1102
1202
- uv_thread_create (&consumer->consume_event_loop , KafkaConsumer::ConsumeLoop, (void *)consumer);
1203
-
1204
- #else
1205
- Nan::AsyncQueueWorker (
1206
- new Workers::KafkaConsumerConsumeLoop (callback, consumer, timeout_ms, retry_read_ms));
1207
- #endif
1103
+ Nan::Callback *callback = new Nan::Callback (cb);
1104
+
1105
+ consumer->m_consume_loop = new Workers::KafkaConsumerConsumeLoop (callback, consumer, timeout_ms, retry_read_ms);
1208
1106
1209
1107
info.GetReturnValue ().Set (Nan::Null ());
1210
1108
}
@@ -1259,8 +1157,8 @@ NAN_METHOD(KafkaConsumer::NodeConsume) {
1259
1157
v8::Local<v8::Function> cb = info[1 ].As <v8::Function>();
1260
1158
Nan::Callback *callback = new Nan::Callback (cb);
1261
1159
1262
- Nan::AsyncQueueWorker (
1263
- new Workers::KafkaConsumerConsume (callback, consumer, timeout_ms));
1160
+ Nan::AsyncQueueWorker (
1161
+ new Workers::KafkaConsumerConsume (callback, consumer, timeout_ms));
1264
1162
}
1265
1163
1266
1164
info.GetReturnValue ().Set (Nan::Null ());
0 commit comments