Files
kiri-core/Annotation/Rpc/RpcService.php
T

68 lines
1.2 KiB
PHP
Raw Normal View History

2021-03-23 15:13:33 +08:00
<?php
namespace Annotation\Rpc;
use Annotation\Attribute;
2021-03-23 17:00:17 +08:00
use Exception;
2021-03-23 15:13:33 +08:00
use ReflectionException;
2021-03-23 16:14:05 +08:00
use Rpc\IProducer;
2021-03-23 15:13:33 +08:00
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class RpcClient
* @package Annotation\Rpc
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcService extends Attribute
{
2021-03-23 16:14:05 +08:00
private array $config;
2021-03-23 15:13:33 +08:00
/**
* RpcClient constructor.
* @param string $cmd
* @param string $host
* @param int $port
2021-03-23 16:49:35 +08:00
* @param int $timeout
2021-03-23 15:13:33 +08:00
* @param int $mode
*/
public function __construct(
public string $cmd,
public string $host,
public int $port,
2021-03-23 16:49:35 +08:00
public int $timeout = 1,
2021-03-23 15:13:33 +08:00
public int $mode = SWOOLE_SOCK_TCP6
)
{
2021-03-23 16:49:35 +08:00
$this->config = ['host' => $host, 'port' => $port, 'mode' => $mode, 'timeout' => $timeout];
2021-03-23 15:13:33 +08:00
}
/**
* @param array $handler
* @return mixed
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindClassException
2021-03-23 17:00:17 +08:00
* @throws Exception
2021-03-23 15:13:33 +08:00
*/
public function execute(array $handler): mixed
{
$rpc = Snowflake::app()->getRpc();
2021-03-23 16:14:05 +08:00
$rpc->addProducer($this->cmd, $handler, $this->config);
2021-03-23 17:00:17 +08:00
2021-03-23 17:26:15 +08:00
if ($handler[0] instanceof IProducer) {
$handler[0]->initClient();
}
2021-03-23 16:14:05 +08:00
return parent::execute($handler);
2021-03-23 15:13:33 +08:00
}
}