Files
kiri-rpc/JsonRpcPoolTransporter.php
T

59 lines
1.2 KiB
PHP
Raw Normal View History

2022-01-09 14:00:32 +08:00
<?php
namespace Kiri\Rpc;
use Kiri\Annotation\Inject;
use Exception;
2022-01-10 11:39:56 +08:00
use Kiri\Message\Response;
use Kiri\Message\Stream;
2022-01-09 14:00:32 +08:00
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Client;
2022-09-23 18:55:46 +08:00
class JsonRpcPoolTransporter implements JsonRpcTransporterInterface
2022-01-09 14:00:32 +08:00
{
use TraitTransporter;
#[Inject(ClientPool::class)]
public ClientPool $pool;
/**
2022-09-23 18:55:46 +08:00
* @param string $content
* @param string $service
* @return string|bool
* @throws ConfigException|RpcServiceException
2022-01-09 14:00:32 +08:00
*/
2022-09-23 18:55:46 +08:00
public function push(string $content, string $service): string|bool
2022-01-09 14:00:32 +08:00
{
2022-09-23 18:55:46 +08:00
$client = $this->get_consul($service)->getClient();
2022-06-08 15:08:07 +08:00
2022-06-08 17:19:32 +08:00
$response = $this->request($client, $content);
2022-01-09 14:00:32 +08:00
$this->pool->push($client, $this->config['Address'], $this->config['Port']);
2022-09-23 18:55:46 +08:00
return $response;
2022-01-09 14:00:32 +08:00
}
/**
* @return Client|\Swoole\Client
* @throws ConfigException
* @throws Exception
*/
private function getClient(): Client|\Swoole\Client
{
$this->config['pool'] = Config::get('rpc.pool', ['max' => 10, 'min' => 1, 'waite' => 60]);
return $this->pool->get($this->config, function () {
return $this->newClient();
});
}
}