2022-01-09 14:00:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2022-06-08 17:19:32 +08:00
|
|
|
use Kiri;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
2022-06-08 17:19:32 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Kiri\Pool\Pool;
|
2023-05-20 23:05:38 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2022-06-08 17:19:32 +08:00
|
|
|
use Kiri\Server\Events\OnBeforeShutdown;
|
2023-05-20 23:05:38 +08:00
|
|
|
use ReflectionException;
|
|
|
|
|
use Swoole\Coroutine\Client;
|
2022-01-09 14:00:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class ClientPool extends Component
|
|
|
|
|
{
|
|
|
|
|
|
2023-05-20 23:05:38 +08:00
|
|
|
public int $max;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int $min;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $names = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onBeforeShutdown(): void
|
|
|
|
|
{
|
|
|
|
|
$pool = Kiri::getDi()->get(Pool::class);
|
|
|
|
|
foreach ($this->names as $name) {
|
|
|
|
|
$pool->clean($name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $config
|
|
|
|
|
* @return resource
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function get($config): mixed
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $config['Address'] . '::' . $config['Port'];
|
|
|
|
|
|
|
|
|
|
if (!in_array($coroutineName, $this->names)) {
|
|
|
|
|
$this->names[] = $coroutineName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getPool($config['Address'], $config['Port'])->get($coroutineName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Client|\Swoole\Client $client
|
|
|
|
|
* @param $host
|
|
|
|
|
* @param $port
|
|
|
|
|
* @throws ConfigException|ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function push(Client|\Swoole\Client $client, $host, $port)
|
|
|
|
|
{
|
|
|
|
|
$this->getPool($host, $port)->push($host . '::' . $port, $client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $host
|
|
|
|
|
* @param $port
|
|
|
|
|
* @return Pool
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function getPool($host, $port): Pool
|
|
|
|
|
{
|
|
|
|
|
$pool = Kiri::getDi()->get(Pool::class);
|
2023-05-26 09:20:30 +08:00
|
|
|
$pool->created($host . '::' . $port, 10, function () use ($host, $port) {
|
2023-05-20 23:05:38 +08:00
|
|
|
$client = stream_socket_client("tcp://$host:$port", $errCode, $errMessage, 3);
|
|
|
|
|
if ($client === false) {
|
|
|
|
|
throw new Exception('Connect ' . $host . '::' . $port . ' fail');
|
|
|
|
|
}
|
|
|
|
|
return $client;
|
|
|
|
|
});
|
|
|
|
|
return $pool;
|
|
|
|
|
}
|
2022-01-09 14:00:32 +08:00
|
|
|
|
|
|
|
|
}
|