Files
kiri-rpc/src/ClientPool.php
T

77 lines
1.3 KiB
PHP
Raw Normal View History

2021-10-28 18:24:46 +08:00
<?php
namespace Kiri\Rpc;
2021-10-28 18:46:14 +08:00
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Context;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Kiri\Pool\Alias;
2021-10-28 18:24:46 +08:00
use Kiri\Pool\Pool;
2021-10-28 18:46:14 +08:00
use Swoole\Client;
2021-10-28 18:24:46 +08:00
/**
*
*/
2021-10-28 18:46:14 +08:00
class ClientPool extends Component
2021-10-28 18:24:46 +08:00
{
const POOL_NAME = 'rpc.client.pool';
2021-10-28 18:46:14 +08:00
use Alias;
2021-10-28 18:24:46 +08:00
public int $max;
public int $min;
public int $waite;
2021-10-28 18:46:14 +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['ServiceAddress'] . '::' . $config['ServicePort'], true);
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
2021-10-28 18:57:07 +08:00
return $this->getPool()->get($coroutineName, $callback, $pool['min'] ?? 1);
2021-10-28 18:46:14 +08:00
}
/**
* @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);
}
2021-10-28 18:24:46 +08:00
}