@@ -86,6 +86,8 @@ namespace cppkafka {
86
86
template <typename BufferType>
87
87
class CPPKAFKA_API BufferedProducer {
88
88
public:
89
+ enum class FlushMethod { Sync, // /< Empty the buffer and wait for acks from the broker
90
+ Async }; // /< Empty the buffer and don't wait for acks
89
91
/* *
90
92
* Concrete builder
91
93
*/
@@ -169,6 +171,13 @@ class CPPKAFKA_API BufferedProducer {
169
171
* \remark This method throws cppkafka::HandleException on failure
170
172
*/
171
173
void produce (const Message& message);
174
+
175
+ /* *
176
+ * \brief Flushes all buffered messages and returns immediately.
177
+ *
178
+ * Similar to flush, it will send all messages but will not wait for acks to complete.
179
+ */
180
+ void async_flush ();
172
181
173
182
/* *
174
183
* \brief Flushes the buffered messages.
@@ -220,6 +229,21 @@ class CPPKAFKA_API BufferedProducer {
220
229
*/
221
230
ssize_t get_max_buffer_size () const ;
222
231
232
+ /* *
233
+ * \brief Sets the method used to flush the internal buffer when 'max_buffer_size' is reached.
234
+ * Default is 'Sync'
235
+ *
236
+ * \param method The method
237
+ */
238
+ void set_flush_method (FlushMethod method);
239
+
240
+ /* *
241
+ * \brief Gets the method used to flush the internal buffer.
242
+ *
243
+ * \return The method
244
+ */
245
+ FlushMethod get_flush_method () const ;
246
+
223
247
/* *
224
248
* \brief Get the number of messages not yet acked by the broker
225
249
*
@@ -391,6 +415,7 @@ class CPPKAFKA_API BufferedProducer {
391
415
ProduceFailureCallback produce_failure_callback_;
392
416
FlushFailureCallback flush_failure_callback_;
393
417
ssize_t max_buffer_size_{-1 };
418
+ FlushMethod flush_method_{FlushMethod::Sync};
394
419
std::atomic<size_t > pending_acks_{0 };
395
420
std::atomic<size_t > flushes_in_progress_{0 };
396
421
std::atomic<size_t > total_messages_produced_{0 };
@@ -471,7 +496,7 @@ void BufferedProducer<BufferType>::produce(const Message& message) {
471
496
}
472
497
473
498
template <typename BufferType>
474
- void BufferedProducer<BufferType>::flush () {
499
+ void BufferedProducer<BufferType>::async_flush () {
475
500
CounterGuard<size_t > counter_guard (flushes_in_progress_);
476
501
QueueType flush_queue; // flush from temporary queue
477
502
{
@@ -482,6 +507,11 @@ void BufferedProducer<BufferType>::flush() {
482
507
async_produce (std::move (flush_queue.front ()), false );
483
508
flush_queue.pop_front ();
484
509
}
510
+ }
511
+
512
+ template <typename BufferType>
513
+ void BufferedProducer<BufferType>::flush() {
514
+ async_flush ();
485
515
wait_for_acks ();
486
516
}
487
517
@@ -528,6 +558,17 @@ ssize_t BufferedProducer<BufferType>::get_max_buffer_size() const {
528
558
return max_buffer_size_;
529
559
}
530
560
561
+ template <typename BufferType>
562
+ void BufferedProducer<BufferType>::set_flush_method(FlushMethod method) {
563
+ flush_method_ = method;
564
+ }
565
+
566
+ template <typename BufferType>
567
+ typename BufferedProducer<BufferType>::FlushMethod
568
+ BufferedProducer<BufferType>::get_flush_method() const {
569
+ return flush_method_;
570
+ }
571
+
531
572
template <typename BufferType>
532
573
template <typename BuilderType>
533
574
void BufferedProducer<BufferType>::do_add_message(BuilderType&& builder,
@@ -543,7 +584,12 @@ void BufferedProducer<BufferType>::do_add_message(BuilderType&& builder,
543
584
}
544
585
}
545
586
if (do_flush && (max_buffer_size_ >= 0 ) && (max_buffer_size_ <= (ssize_t )messages_.size ())) {
546
- flush ();
587
+ if (flush_method_ == FlushMethod::Sync) {
588
+ flush ();
589
+ }
590
+ else {
591
+ async_flush ();
592
+ }
547
593
}
548
594
}
549
595
0 commit comments