Files
kiri-core/Kafka/Producer.php
T

125 lines
3.1 KiB
PHP
Raw Normal View History

2020-10-09 10:58:37 +08:00
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +---------------------------------------------------------------------------
// | SWAN [ $_SWANBR_SLOGAN_$ ]
// +---------------------------------------------------------------------------
// | Copyright $_SWANBR_COPYRIGHT_$
// +---------------------------------------------------------------------------
// | Version $_SWANBR_VERSION_$
// +---------------------------------------------------------------------------
// | Licensed ( $_SWANBR_LICENSED_URL_$ )
// +---------------------------------------------------------------------------
// | $_SWANBR_WEB_DOMAIN_$
// +---------------------------------------------------------------------------
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 16:12:30 +08:00
$this->conf = new Conf();
$this->topicConf = new TopicConf();
2020-10-28 16:11:19 +08:00
parent::__construct($config);
}
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-28 16:04:52 +08:00
$this->topicConf->set('request.required.acks', (int)$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:01:36 +08:00
public function delivery($message, $key = null, $timeout = 5)
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->setDrmSgCb(function ($kafka, $message) {
2020-10-28 15:47:39 +08:00
// $this->debug(var_export($message, true));
});
2020-10-28 16:04:52 +08:00
$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:25:22 +08:00
$rk = Snowflake::createObject(\RdKafka\Producer::class, [$this->conf]);
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:10:23 +08:00
$rk->poll($timeout);
2020-10-28 16:24:55 +08:00
$event = Snowflake::app()->getEvent();
2020-10-28 16:27:12 +08:00
$event->on(Event::EVENT_AFTER_REQUEST, [$this, 'onFlush'], [$rk, $timeout]);
}
/**
* @param $rk
* @param $timeout
*/
public function onFlush($rk, $timeout)
{
$this->debug(Event::EVENT_AFTER_REQUEST);
$rk->flush($timeout);
2020-10-28 15:47:39 +08:00
}
2020-10-09 10:58:37 +08:00
}