diff --git a/include/cppkafka/utils/buffered_producer.h b/include/cppkafka/utils/buffered_producer.h index 4083e71..b2c3522 100644 --- a/include/cppkafka/utils/buffered_producer.h +++ b/include/cppkafka/utils/buffered_producer.h @@ -87,8 +87,15 @@ template >> class CPPKAFKA_API BufferedProducer { public: - enum class FlushMethod { Sync, ///< Empty the buffer and wait for acks from the broker - Async }; ///< Empty the buffer and don't wait for acks + enum class FlushMethod { + Sync, ///< Empty the buffer and wait for acks from the broker. + Async ///< Empty the buffer and don't wait for acks. + }; + enum class QueueFullNotification { + None, ///< Don't notify + EdgeTriggered, ///< Notify once. Application must call queue_full_trigger_reset() to enable again. + EachOccurence ///< Notify on each occurence. + }; /** * Concrete builder */ @@ -358,6 +365,25 @@ public: * Simple helper to construct a builder object */ Builder make_builder(std::string topic); + + /** + * Set the type of notification when RD_KAFKA_RESP_ERR__QUEUE_FULL is received. + * + * This will call the error callback for this producer. By default this is set to QueueFullNotification::None. + */ + void set_queue_full_notification(QueueFullNotification notification); + + /** + * Get the queue full notification type. + */ + QueueFullNotification get_queue_full_notification() const; + + /** + * Reset the queue full notification trigger. + * + * This function has no effect unless QueueFullNotification == EdgeTriggered. + */ + void queue_full_trigger_reset(); /** * \brief Sets the message produce failure callback @@ -505,6 +531,8 @@ private: std::atomic total_messages_dropped_{0}; int max_number_retries_{0}; bool has_internal_data_{false}; + QueueFullNotification queue_full_notification_{QueueFullNotification::None}; + bool queue_full_trigger_{true}; #ifdef KAFKA_TEST_INSTANCE TestParameters* test_params_; #endif @@ -798,6 +826,22 @@ BufferedProducer::make_builder(std::string topic) { return Builder(std::move(topic)); } +template +void BufferedProducer::set_queue_full_notification(QueueFullNotification notification) { + queue_full_notification_ = notification; +} + +template +typename BufferedProducer::QueueFullNotification +BufferedProducer::get_queue_full_notification() const { + return queue_full_notification_; +} + +template +void BufferedProducer::queue_full_trigger_reset() { + queue_full_trigger_ = true; +} + template void BufferedProducer::set_produce_failure_callback(ProduceFailureCallback callback) { produce_failure_callback_ = std::move(callback); @@ -827,6 +871,9 @@ template template void BufferedProducer::produce_message(BuilderType&& builder) { using builder_type = typename std::decay::type; + bool queue_full_notify = (queue_full_notification_ == QueueFullNotification::None) ? false : + (queue_full_notification_ == QueueFullNotification::EdgeTriggered) ? + queue_full_trigger_ : true; while (true) { try { MessageInternalGuard internal_guard(const_cast(builder)); @@ -840,6 +887,13 @@ void BufferedProducer::produce_message(BuilderType&& buil if (ex.get_error() == RD_KAFKA_RESP_ERR__QUEUE_FULL) { // If the output queue is full, then just poll producer_.poll(); + // Notify application so it can slow-down production + if (queue_full_notify) { + queue_full_notify = queue_full_trigger_ = false; //clear trigger and local state + CallbackInvoker + ("error", get_producer().get_configuration().get_error_callback(), &get_producer()) + (get_producer(), static_cast(ex.get_error().get_error()), ex.what()); + } } else { throw;