9
9
10
10
#include < string>
11
11
#include < vector>
12
+ #include < math.h>
12
13
13
14
#include " src/workers.h"
14
15
#include " src/admin.h"
@@ -151,23 +152,30 @@ v8::Local<v8::Object> AdminClient::NewInstance(v8::Local<v8::Value> arg) {
151
152
rd_kafka_event_t * PollForEvent (
152
153
rd_kafka_queue_t * topic_rkqu,
153
154
rd_kafka_event_type_t event_type,
154
- int max_tries,
155
155
int timeout_ms) {
156
- // Establish what attempt we are on
157
- int attempt = 0 ;
156
+ // Initiate exponential timeout
157
+ int attempts = 1 ;
158
+ int exp_timeout_ms = timeout_ms;
159
+ if (timeout_ms > 2000 ) {
160
+ // measure optimal number of attempts
161
+ attempts = log10 (timeout_ms / 1000 ) / log10 (2 ) + 1 ;
162
+ // measure initial exponential timeout based on attempts
163
+ exp_timeout_ms = timeout_ms / (pow (2 , attempts) - 1 );
164
+ }
158
165
159
166
rd_kafka_event_t * event_response = nullptr ;
160
167
161
168
// Poll the event queue until we get it
162
169
do {
163
170
// free previously fetched event
164
171
rd_kafka_event_destroy (event_response);
165
- // Increment attempt counter
166
- attempt = attempt + 1 ;
167
- event_response = rd_kafka_queue_poll (topic_rkqu, timeout_ms);
172
+ // poll and update attempts and exponential timeout
173
+ event_response = rd_kafka_queue_poll (topic_rkqu, exp_timeout_ms);
174
+ attempts = attempts - 1 ;
175
+ exp_timeout_ms = 2 * exp_timeout_ms;
168
176
} while (
169
177
rd_kafka_event_type (event_response) != event_type &&
170
- attempt < max_tries );
178
+ attempts > 0 );
171
179
172
180
// If this isn't the type of response we want, or if we do not have a response
173
181
// type, bail out with a null
@@ -204,8 +212,7 @@ Baton AdminClient::CreateTopic(rd_kafka_NewTopic_t* topic, int timeout_ms) {
204
212
rd_kafka_event_t * event_response = PollForEvent (
205
213
topic_rkqu,
206
214
RD_KAFKA_EVENT_CREATETOPICS_RESULT,
207
- 5 ,
208
- 1000 );
215
+ timeout_ms);
209
216
210
217
// Destroy the queue since we are done with it.
211
218
rd_kafka_queue_destroy (topic_rkqu);
@@ -284,8 +291,7 @@ Baton AdminClient::DeleteTopic(rd_kafka_DeleteTopic_t* topic, int timeout_ms) {
284
291
rd_kafka_event_t * event_response = PollForEvent (
285
292
topic_rkqu,
286
293
RD_KAFKA_EVENT_DELETETOPICS_RESULT,
287
- 5 ,
288
- 1000 );
294
+ timeout_ms);
289
295
290
296
// Destroy the queue since we are done with it.
291
297
rd_kafka_queue_destroy (topic_rkqu);
@@ -360,8 +366,7 @@ Baton AdminClient::CreatePartitions(
360
366
rd_kafka_event_t * event_response = PollForEvent (
361
367
topic_rkqu,
362
368
RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT,
363
- 5 ,
364
- 1000 );
369
+ timeout_ms);
365
370
366
371
// Destroy the queue since we are done with it.
367
372
rd_kafka_queue_destroy (topic_rkqu);
@@ -480,7 +485,7 @@ NAN_METHOD(AdminClient::NodeCreateTopic) {
480
485
AdminClient* client = ObjectWrap::Unwrap<AdminClient>(info.This ());
481
486
482
487
// Get the timeout
483
- int timeout = Nan::To<int32_t >(info[2 ]).FromJust ();
488
+ int timeout = Nan::To<int32_t >(info[1 ]).FromJust ();
484
489
485
490
std::string errstr;
486
491
// Get that topic we want to create
@@ -524,7 +529,7 @@ NAN_METHOD(AdminClient::NodeDeleteTopic) {
524
529
Nan::To<v8::String>(info[0 ]).ToLocalChecked ());
525
530
526
531
// Get the timeout
527
- int timeout = Nan::To<int32_t >(info[2 ]).FromJust ();
532
+ int timeout = Nan::To<int32_t >(info[1 ]).FromJust ();
528
533
529
534
// Get that topic we want to create
530
535
rd_kafka_DeleteTopic_t* topic = rd_kafka_DeleteTopic_new (
@@ -563,6 +568,9 @@ NAN_METHOD(AdminClient::NodeCreatePartitions) {
563
568
Nan::Callback *callback = new Nan::Callback (cb);
564
569
AdminClient* client = ObjectWrap::Unwrap<AdminClient>(info.This ());
565
570
571
+ // Get the timeout
572
+ int timeout = Nan::To<int32_t >(info[2 ]).FromJust ();
573
+
566
574
// Get the total number of desired partitions
567
575
int partition_total_count = Nan::To<int32_t >(info[1 ]).FromJust ();
568
576
@@ -585,7 +593,7 @@ NAN_METHOD(AdminClient::NodeCreatePartitions) {
585
593
586
594
// Queue up dat work
587
595
Nan::AsyncQueueWorker (new Workers::AdminClientCreatePartitions (
588
- callback, client, new_partitions, 1000 ));
596
+ callback, client, new_partitions, timeout ));
589
597
590
598
return info.GetReturnValue ().Set (Nan::Null ());
591
599
}
0 commit comments