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\Annotation\Inject;
|
|
|
|
|
use Kiri\Events\EventProvider;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Kiri\Pool\Alias;
|
|
|
|
|
use Kiri\Pool\Pool;
|
2022-06-08 17:19:32 +08:00
|
|
|
use Kiri\Server\Events\OnBeforeShutdown;
|
2022-01-09 14:00:32 +08:00
|
|
|
use Swoole\Client;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class ClientPool extends Component
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const POOL_NAME = 'rpc.client.pool';
|
|
|
|
|
|
|
|
|
|
use Alias;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int $max;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int $min;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int $waite;
|
|
|
|
|
|
|
|
|
|
|
2022-06-08 17:19:32 +08:00
|
|
|
#[Inject(EventProvider::class)]
|
|
|
|
|
public EventProvider $provider;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $names = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->provider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onBeforeShutdown()
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->names as $name) {
|
|
|
|
|
$this->getPool()->clean($name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 14:00:32 +08:00
|
|
|
/**
|
|
|
|
|
* @param $config
|
|
|
|
|
* @param callable $callback
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function get($config, callable $callback): mixed
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name(self::POOL_NAME . '::' . $config['Address'] . '::' . $config['Port'], true);
|
|
|
|
|
|
|
|
|
|
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
|
|
|
|
|
|
2022-06-08 17:19:32 +08:00
|
|
|
$this->names[] = $coroutineName;
|
|
|
|
|
|
2022-01-09 14:00:32 +08:00
|
|
|
return $this->getPool()->get($coroutineName, $callback, $pool['min'] ?? 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Swoole\Coroutine\Client|Client $client
|
|
|
|
|
* @param $host
|
|
|
|
|
* @param $port
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function push(\Swoole\Coroutine\Client|Client $client, $host, $port)
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name(self::POOL_NAME . '::' . $host . '::' . $port, true);
|
|
|
|
|
|
|
|
|
|
$this->getPool()->push($coroutineName, $client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Pool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getPool(): Pool
|
|
|
|
|
{
|
|
|
|
|
return Kiri::getDi()->get(Pool::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|