Revert "改名"

This reverts commit fdf58326
This commit is contained in:
2022-01-08 18:49:07 +08:00
parent 3d89cbd4c6
commit b0ed7650c5
16 changed files with 17 additions and 13 deletions
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace Kiri\Rpc;
use Kiri\Annotation\Inject;
use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Client;
class JsonRpcPoolTransporter implements ClientInterface
{
use TraitTransporter;
#[Inject(ClientPool::class)]
public ClientPool $pool;
const POOL_NAME = 'rpc.client.pool';
/**
*/
public function init()
{
}
/**
* @param RequestInterface $request
* @return ResponseInterface
* @throws Exception
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
$content = $request->getBody()->getContents();
$response = $this->request($client = $this->getClient(), $content, false);
$this->pool->push($client, $this->config['Address'], $this->config['Port']);
return (new Response())->withBody(new Stream($response));
}
/**
* @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();
});
}
}