@@ -25,12 +25,14 @@ const ProducerState = Object.freeze({
25
25
} ) ;
26
26
27
27
const CompressionTypes = Object . freeze ( {
28
- None : 'none' ,
28
+ NONE : 'none' ,
29
29
GZIP : 'gzip' ,
30
30
SNAPPY : 'snappy' ,
31
31
LZ4 : 'lz4' ,
32
32
ZSTD : 'zstd' ,
33
- } )
33
+ } ) ;
34
+
35
+ const producerPollIntervalMs = 500 ;
34
36
35
37
class Producer {
36
38
/**
@@ -75,6 +77,14 @@ class Producer {
75
77
*/
76
78
#logger = new DefaultLogger ( ) ;
77
79
80
+ /**
81
+ * Stores the time of the last poll.
82
+ * In case we are producing in a tight loop, the interval timer will not
83
+ * fire, and we won't poll. By maintaining the last poll time, we can
84
+ * poll at the end of send() and sendBatch().
85
+ */
86
+ #lastPollTime = process . hrtime ( ) ;
87
+
78
88
/**
79
89
* @constructor
80
90
* @param {import("../../types/kafkajs").ProducerConfig } kJSConfig
@@ -273,7 +283,8 @@ class Producer {
273
283
return ;
274
284
}
275
285
this . #internalClient. poll ( ) ;
276
- } , 500 ) ;
286
+ this . #lastPollTime = process . hrtime ( ) ;
287
+ } , producerPollIntervalMs ) ;
277
288
278
289
this . #internalClient. on ( 'delivery-report' , this . #deliveryCallback. bind ( this ) ) ;
279
290
@@ -541,6 +552,15 @@ class Producer {
541
552
}
542
553
} ) ) ;
543
554
555
+ /* Poll if we haven't polled in a while. This can be the case if we're producing
556
+ * in a tight loop without awaiting the produce. */
557
+ const elapsed = process . hrtime ( this . #lastPollTime) ;
558
+ const elapsedInNanos = elapsed [ 0 ] * 1e9 + elapsed [ 1 ] ;
559
+ if ( elapsedInNanos > producerPollIntervalMs * 1000 ) {
560
+ this . #lastPollTime = process . hrtime ( ) ;
561
+ this . #internalClient. poll ( ) ;
562
+ }
563
+
544
564
}
545
565
546
566
/* The delivery report will be handled by the delivery-report event handler, and we can simply wait for it here. */
0 commit comments