From b4e4cc8ac35d249d5291122f129fb3a451ddad5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 28 Oct 2020 16:48:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Kafka/Producer.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Kafka/Producer.php b/Kafka/Producer.php index 02c6191c..c8c34e21 100644 --- a/Kafka/Producer.php +++ b/Kafka/Producer.php @@ -95,9 +95,6 @@ class Producer extends Component if (!$this->conf || !$this->topicConf) { throw new \Exception('Error. Please set kafka conf.'); } - $this->conf->setDrmSgCb(function ($kafka, $message) { -// $this->debug(var_export($message, true)); - }); $this->conf->setErrorCb(function ($kafka, $err, $reason) { $this->error(sprintf("Kafka error: %s (reason: %s)", rd_kafka_err2str($err), $reason)); }); @@ -105,13 +102,23 @@ class Producer extends Component /** @var \RdKafka\Producer $rk */ $rk = Snowflake::createObject(\RdKafka\Producer::class, [$this->conf]); if ($rk->getOutQLen() > 0) { - $rk->poll(0); - $rk->flush($timeout); + $this->clean($rk, $timeout); } $topic = $rk->newTopic($this->_topic, $this->topicConf); $topic->produce(RD_KAFKA_PARTITION_UA, 0, $message, $key); + $this->clean($rk, $timeout); + } + + + /** + * @param $rk + * @param $timeout + */ + private function clean($rk, $timeout) + { $rk->poll(0); $rk->flush($timeout); } + }