2022-01-09 14:00:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2022-09-23 18:55:46 +08:00
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
2022-12-12 17:31:12 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-05-20 23:05:38 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Swoole\Client;
|
|
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
|
|
|
|
|
trait TraitTransporter
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-09-23 18:55:46 +08:00
|
|
|
/**
|
|
|
|
|
* @var RpcManager
|
|
|
|
|
*/
|
2023-05-20 23:05:38 +08:00
|
|
|
#[Container(RpcManager::class)]
|
2022-09-23 18:55:46 +08:00
|
|
|
public RpcManager $manager;
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 14:00:32 +08:00
|
|
|
protected array $config;
|
|
|
|
|
|
|
|
|
|
|
2022-09-23 18:55:46 +08:00
|
|
|
/**
|
2023-05-20 23:05:38 +08:00
|
|
|
* @param resource $client
|
2022-09-23 18:55:46 +08:00
|
|
|
* @param $content
|
|
|
|
|
* @return string|bool
|
|
|
|
|
*/
|
2023-10-24 17:22:31 +08:00
|
|
|
protected function request(mixed $client, $content): string|bool
|
2022-09-23 18:55:46 +08:00
|
|
|
{
|
2023-10-24 17:22:31 +08:00
|
|
|
socket_write($client, \msgpack_pack($content), mb_strlen($content));
|
2023-05-20 23:05:38 +08:00
|
|
|
|
|
|
|
|
socket_read($client, 1024);
|
|
|
|
|
|
2023-10-24 17:22:31 +08:00
|
|
|
return \msgpack_unpack($client->recv());
|
2022-09-23 18:55:46 +08:00
|
|
|
}
|
2022-01-09 14:00:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-23 18:55:46 +08:00
|
|
|
* @param string $service
|
2022-01-09 14:00:32 +08:00
|
|
|
* @return $this
|
2022-09-23 18:55:46 +08:00
|
|
|
* @throws RpcServiceException
|
2022-01-09 14:00:32 +08:00
|
|
|
*/
|
2023-10-24 17:22:31 +08:00
|
|
|
protected function get_consul(string $service): static
|
2022-01-09 14:00:32 +08:00
|
|
|
{
|
2022-09-23 18:55:46 +08:00
|
|
|
if (empty($service)) {
|
|
|
|
|
throw new RpcServiceException('You need set rpc service name if used.');
|
|
|
|
|
}
|
|
|
|
|
$sf = $this->manager->getServices($service);
|
|
|
|
|
if (empty($sf) || !is_array($sf)) {
|
|
|
|
|
throw new RpcServiceException('You need set rpc service name if used.');
|
|
|
|
|
}
|
|
|
|
|
$this->config = $this->_loadRand($sf);
|
2022-01-09 14:00:32 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-23 18:55:46 +08:00
|
|
|
* @param $services
|
|
|
|
|
* @return array
|
2022-01-09 14:00:32 +08:00
|
|
|
*/
|
2022-09-23 18:55:46 +08:00
|
|
|
#[ArrayShape(['Address' => "mixed", 'Port' => "mixed"])]
|
2023-10-24 17:22:31 +08:00
|
|
|
protected function _loadRand($services): array
|
2022-01-09 14:00:32 +08:00
|
|
|
{
|
2022-09-23 18:55:46 +08:00
|
|
|
$array = [];
|
|
|
|
|
foreach ($services as $value) {
|
|
|
|
|
$value['Weight'] = $value['Weights']['Passing'];
|
|
|
|
|
$array[] = $value;
|
|
|
|
|
}
|
|
|
|
|
if (count($array) < 2) {
|
|
|
|
|
$luck = $array[0];
|
|
|
|
|
} else {
|
2023-07-31 23:09:00 +08:00
|
|
|
$luck = LotteryDraw::luck($array, 'Weight');
|
2022-09-23 18:55:46 +08:00
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'Address' => $luck['TaggedAddresses']['wan_ipv4']['Address'],
|
|
|
|
|
'Port' => $luck['TaggedAddresses']['wan_ipv4']['Port']
|
|
|
|
|
];
|
2022-01-09 14:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-23 18:55:46 +08:00
|
|
|
|
2022-01-09 14:00:32 +08:00
|
|
|
/**
|
|
|
|
|
* @return Client|Coroutine\Client
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-10-24 17:22:31 +08:00
|
|
|
protected function newClient(): Coroutine\Client|Client
|
2022-01-09 14:00:32 +08:00
|
|
|
{
|
2022-05-04 03:53:37 +08:00
|
|
|
if (Context::inCoroutine()) {
|
|
|
|
|
$client = new Coroutine\Client(SWOOLE_SOCK_TCP);
|
|
|
|
|
} else {
|
|
|
|
|
$client = new Client(SWOOLE_SOCK_TCP);
|
|
|
|
|
}
|
2022-01-09 14:00:32 +08:00
|
|
|
[$host, $port] = [$this->config['Address'], $this->config['Port']];
|
|
|
|
|
if (!$client->isConnected() && !$client->connect($host, $port, 60)) {
|
|
|
|
|
throw new Exception('connect fail.');
|
|
|
|
|
}
|
|
|
|
|
return $client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|