modify plugin name

This commit is contained in:
2022-03-01 18:46:08 +08:00
parent f17a55f8d0
commit d589e096af
4 changed files with 116 additions and 232 deletions
-219
View File
@@ -1,219 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use Kiri;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException;
use Kiri\Message\Stream;
use Swoole\Client as SwowClient;
/**
* Class Client
* @package Kiri\Http
*/
class AsyncClient extends ClientAbstracts
{
use TSwooleClient;
/**
* @param string $method
* @param $path
* @param array $params
* @return void
* @throws Exception
*/
public function request(string $method, $path, array $params = []): void
{
$this->withMethod($method)
->coroutine(
$this->matchHost($path),
$this->paramEncode($params)
);
}
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
return $this;
}
/**
* @param $url
* @param array|string $data
* @throws Exception 使用swoole协程方式请求
*/
private function coroutine($url, array|string $data = []): void
{
try {
$this->generate_client($data, ...$url);
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
* @throws Exception
*/
private function generate_client($data, $host, $isHttps, $path): void
{
$this->client = new SwowClient(SWOOLE_TCP, FALSE);
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
if (!$this->client->connect($host, $this->getPort())) {
throw new Exception('链接失败');
}
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
$path = $this->setParams($path, $data);
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9');
// $this->withAddedHeader('Accept-Encoding', 'gzip');
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
$this->execute($path, $this->getData()->getContents());
}
/**
* @param string $path
* @param string $content
* @return void
*/
private function execute(string $path, string $content)
{
$array = $this->_parseHeaders($path);
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content . "\r\n\r\n");
$receive = '';
while ($this->client->isConnected()) {
$_tmp = $this->client->recv();
if (empty($_tmp)) {
break;
}
$receive .= $_tmp;
}
[$header, $body] = explode("\r\n\r\n", $receive);
$header = explode("\r\n", $header);
$status = array_shift($header);
$this->setStatusCode(intval(explode(' ', $status)[1]));
$this->parseResponseHeaders($header);
$this->setBody($body);
}
/**
* @param string $path
* @return array
*/
#[Pure] private function _parseHeaders(string $path): array
{
$array = [];
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
if (!empty($this->getHeader())) {
$headers = $this->getHeader();
foreach ($headers as $key => $value) {
$array[] = sprintf('%s: %s', $key, $value);
}
}
return $array;
}
/**
* @param $client
* @param $string
* @return mixed
*/
private function waite(&$client, $string): mixed
{
$tmp = $client->recv();
if (!empty($tmp)) {
return $this->waite($client, $string . $tmp);
}
return $string;
}
private function chunked()
{
}
/**
* @param array $headers
* @return void
*/
private function parseResponseHeaders(array $headers)
{
$array = [];
foreach ($headers as $header) {
[$key, $value] = explode(': ', $header);
$array[$key] = trim($value);
}
$this->setResponseHeader($array);
}
/**
* @param $path
* @param $data
* @return string
*/
private function setParams($path, $data): string
{
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$this->withBody(new Stream($data));
}
}
return $path;
}
/**
*
*/
public function close(): void
{
/** @var SwowClient $client */
$client = $this->client;
if (!$client || !$client->isConnected()) {
return;
}
$client->close();
}
}
+45
View File
@@ -53,6 +53,11 @@ abstract class ClientAbstracts implements IClient
private int $statusCode = 200; private int $statusCode = 200;
protected int $retryNum = 3;
protected int $retryTimeout = 3;
private bool $VERIFYPEER = TRUE; private bool $VERIFYPEER = TRUE;
@@ -73,6 +78,46 @@ abstract class ClientAbstracts implements IClient
protected mixed $client; protected mixed $client;
/**
* @param int $retryNum
* @return $this
*/
public function withRetryNum(int $retryNum): static
{
$this->retryNum = $retryNum;
return $this;
}
/**
* @param int $retryTimeout
* @return $this
*/
public function withRetryTimeout(int $retryTimeout): static
{
$this->retryTimeout = $retryTimeout;
return $this;
}
/**
* @return int
*/
public function getRetryNum(): int
{
return $this->retryNum;
}
/**
* @return int
*/
public function getRetryTimeout(): int
{
return $this->retryTimeout;
}
/** /**
* @param $bool * @param $bool
* @return $this * @return $this
+42 -8
View File
@@ -37,7 +37,7 @@ class CoroutineClient extends ClientAbstracts
} }
$this->withMethod($method) $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $path,
$this->paramEncode($params) $this->paramEncode($params)
); );
} }
@@ -60,13 +60,13 @@ class CoroutineClient extends ClientAbstracts
private function coroutine($url, array|string $data = []): void private function coroutine($url, array|string $data = []): void
{ {
try { try {
$this->generate_client($data, ...$url); $this->generate_client($this->getHost(), $this->isSSL());
if ($this->client->statusCode < 0) { if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg); throw new Exception($this->client->errMsg);
} }
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody()); $this->execute($url, $data);
$this->setResponseHeader($this->client->headers);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]); Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
$this->setStatusCode(-1); $this->setStatusCode(-1);
@@ -76,12 +76,47 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $path
* @param $data * @param $data
* @return void
* @throws Exception
*/
private function execute($path, $data)
{
$this->client->execute($this->setParams($path, $data));
if (in_array($this->client->getStatusCode(), [502, 404])) {
$this->retry($path, $data);
} else {
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
}
}
/**
* @return void
* @throws Exception
*/
private function retry($path, $data)
{
if (Context::increment('retry') <= $this->retryNum) {
sleep($this->retryTimeout);
$this->execute($path, $data);
} else {
Context::remove('retry');
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
}
}
/**
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path
*/ */
private function generate_client($data, $host, $isHttps, $path): void private function generate_client($host, $isHttps): void
{ {
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$this->client = new SwowClient($host, 443, true); $this->client = new SwowClient($host, 443, true);
@@ -94,7 +129,6 @@ class CoroutineClient extends ClientAbstracts
} }
$this->client->setHeaders($this->getHeader()); $this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod())); $this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
} }
+29 -5
View File
@@ -138,11 +138,30 @@ class CurlClient extends ClientAbstracts
private function execute(): void private function execute(): void
{ {
$output = curl_exec($this->client); $output = curl_exec($this->client);
if ($output === FALSE) { if ($output !== FALSE) {
$this->explode($output);
} else {
$this->setStatusCode(curl_errno($this->client)); $this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client)); $this->setBody(curl_error($this->client));
}
}
/**
* @return void
* @throws Exception
*/
private function retry()
{
if (Context::increment('retry') <= $this->retryNum) {
sleep($this->retryTimeout);
$this->execute();
} else { } else {
$this->explode($output); Context::remove('retry');
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
} }
} }
@@ -171,9 +190,14 @@ class CurlClient extends ClientAbstracts
$header = explode("\r\n", $header); $header = explode("\r\n", $header);
$status = explode(' ', array_shift($header)); $status = explode(' ', array_shift($header));
$this->setStatusCode(intval($status[1])); $statusCode = intval($status[1]);
$this->setBody($body); if (in_array($statusCode, [502, 404])) {
$this->setResponseHeader($header); $this->retry();
} else {
$this->setStatusCode($statusCode);
$this->setBody($body);
$this->setResponseHeader($header);
}
} }
/** /**