Files
kiri-core/Kafka/Producer.php
T

118 lines
2.3 KiB
PHP
Raw Normal View History

2020-10-09 10:58:37 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-10-09 10:58:37 +08:00
namespace Kafka;
2020-10-28 15:47:39 +08:00
use RdKafka\Conf;
use RdKafka\TopicConf;
2020-10-28 16:01:36 +08:00
use ReflectionException;
2020-10-28 15:47:39 +08:00
use Snowflake\Abstracts\Component;
2020-10-28 16:24:55 +08:00
use Snowflake\Event;
2020-10-28 16:01:36 +08:00
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
2020-10-09 10:58:37 +08:00
2020-10-28 15:47:39 +08:00
/**
* +------------------------------------------------------------------------------
* Kafka protocol since Kafka v0.8
* +------------------------------------------------------------------------------
*
* @package
* @version $_SWANBR_VERSION_$
* @copyright Copyleft
* @author $_SWANBR_AUTHOR_$
* +------------------------------------------------------------------------------
*/
class Producer extends Component
2020-10-09 10:58:37 +08:00
{
2020-10-28 16:01:36 +08:00
private string $_topic = '';
2020-10-28 16:04:52 +08:00
private Conf $conf;
private TopicConf $topicConf;
2020-10-28 16:11:19 +08:00
public function __construct($config = [])
{
2020-10-28 17:50:48 +08:00
parent::__construct($config);
if (!class_exists(Conf::class)) {
return;
}
2020-10-28 16:12:30 +08:00
$this->conf = new Conf();
$this->topicConf = new TopicConf();
2020-10-28 16:11:19 +08:00
}
2020-10-28 16:01:36 +08:00
/**
* @param $servers
* @return Producer
*/
public function setBrokers(string $servers)
{
2020-10-28 16:04:52 +08:00
$this->conf->set('metadata.broker.list', $servers);
2020-10-28 16:01:36 +08:00
return $this;
}
/**
* @param bool $value
* @return $this
*/
public function setAck(bool $value)
{
2020-10-30 01:33:33 +08:00
$this->topicConf->set('request.required.acks', (string)$value);
2020-10-28 16:01:36 +08:00
return $this;
}
/**
* @param $servers
* @return Producer
*/
public function setTopic(string $servers)
{
$this->_topic = $servers;
return $this;
}
2020-10-28 15:47:39 +08:00
/**
* @param $message
* @param null $key
* @param int $timeout
2020-10-28 16:01:36 +08:00
* @throws
2020-10-28 15:47:39 +08:00
*/
2020-10-28 16:44:40 +08:00
public function delivery($message, $key = null, $timeout = 10)
2020-10-28 15:47:39 +08:00
{
2020-10-28 16:04:52 +08:00
if (!$this->conf || !$this->topicConf) {
throw new \Exception('Error. Please set kafka conf.');
}
$this->conf->setErrorCb(function ($kafka, $err, $reason) {
2020-10-28 15:47:39 +08:00
$this->error(sprintf("Kafka error: %s (reason: %s)", rd_kafka_err2str($err), $reason));
});
2020-10-28 16:46:03 +08:00
/** @var \RdKafka\Producer $rk */
2020-10-28 16:25:22 +08:00
$rk = Snowflake::createObject(\RdKafka\Producer::class, [$this->conf]);
2020-10-28 16:46:03 +08:00
if ($rk->getOutQLen() > 0) {
2020-10-28 16:48:10 +08:00
$this->clean($rk, $timeout);
2020-10-28 16:46:03 +08:00
}
2020-10-28 16:04:52 +08:00
$topic = $rk->newTopic($this->_topic, $this->topicConf);
2020-10-28 15:47:39 +08:00
$topic->produce(RD_KAFKA_PARTITION_UA, 0, $message, $key);
2020-10-28 16:48:10 +08:00
$this->clean($rk, $timeout);
}
/**
* @param $rk
* @param $timeout
*/
private function clean($rk, $timeout)
{
2020-10-28 16:49:44 +08:00
for ($length = 0; $length < $rk->getOutQLen(); $length++) {
$rk->poll(0);
}
2020-10-28 16:38:05 +08:00
$rk->flush($timeout);
2020-10-28 15:47:39 +08:00
}
2020-10-28 16:48:10 +08:00
2020-10-09 10:58:37 +08:00
}