变更
This commit is contained in:
+6
-6
@@ -47,7 +47,7 @@ abstract class ClientAbstracts implements IClient
|
|||||||
private int $port = 80;
|
private int $port = 80;
|
||||||
|
|
||||||
|
|
||||||
private array $_responseHeader = [];
|
private ?array $_responseHeader = [];
|
||||||
|
|
||||||
|
|
||||||
private int $statusCode = 200;
|
private int $statusCode = 200;
|
||||||
@@ -58,7 +58,7 @@ abstract class ClientAbstracts implements IClient
|
|||||||
protected int $retryTimeout = 0;
|
protected int $retryTimeout = 0;
|
||||||
|
|
||||||
|
|
||||||
private bool $VERIFYPEER = TRUE;
|
private bool $verifyPeer = TRUE;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,7 +124,7 @@ abstract class ClientAbstracts implements IClient
|
|||||||
*/
|
*/
|
||||||
public function withVerifyPeer($bool): static
|
public function withVerifyPeer($bool): static
|
||||||
{
|
{
|
||||||
$this->VERIFYPEER = $bool;
|
$this->verifyPeer = $bool;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ abstract class ClientAbstracts implements IClient
|
|||||||
*/
|
*/
|
||||||
public function getVerifyPeer(): bool
|
public function getVerifyPeer(): bool
|
||||||
{
|
{
|
||||||
return $this->VERIFYPEER;
|
return $this->verifyPeer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -167,9 +167,9 @@ abstract class ClientAbstracts implements IClient
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $responseHeader
|
* @param null|array $responseHeader
|
||||||
*/
|
*/
|
||||||
public function setResponseHeader(array $responseHeader): void
|
public function setResponseHeader(?array $responseHeader): void
|
||||||
{
|
{
|
||||||
$this->_responseHeader = $responseHeader;
|
$this->_responseHeader = $responseHeader;
|
||||||
}
|
}
|
||||||
|
|||||||
+137
-124
@@ -12,6 +12,7 @@ namespace Kiri;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Abstracts\Logger;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Swoole\Coroutine\Http\Client as SwowClient;
|
use Swoole\Coroutine\Http\Client as SwowClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,144 +22,156 @@ use Swoole\Coroutine\Http\Client as SwowClient;
|
|||||||
class CoroutineClient extends ClientAbstracts
|
class CoroutineClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
|
|
||||||
use TSwooleClient;
|
use TSwooleClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function request(string $method, $path, array $params = []): void
|
public function request(string $method, $path, array $params = []): void
|
||||||
{
|
{
|
||||||
if (!str_starts_with($path, '/')) {
|
if (!str_starts_with($path, '/')) {
|
||||||
$path = '/' . $path;
|
$path = '/' . $path;
|
||||||
}
|
}
|
||||||
$this->withMethod($method)
|
$this->withMethod($method)
|
||||||
->coroutine(
|
->coroutine(
|
||||||
$path,
|
$path,
|
||||||
$this->paramEncode($params)
|
$this->paramEncode($params)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function withCAInfo($path): static
|
public function withCAInfo($path): static
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $url
|
* @param $url
|
||||||
* @param array|string $data
|
* @param array|string $data
|
||||||
* @throws Exception 使用swoole协程方式请求
|
* @throws Exception 使用swoole协程方式请求
|
||||||
*/
|
*/
|
||||||
private function coroutine($url, array|string $data = []): void
|
private function coroutine($url, array|string $data = []): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->generate_client($this->getHost(), $this->isSSL());
|
$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->execute($url, $data);
|
$this->execute($url, $data);
|
||||||
|
|
||||||
} 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);
|
||||||
$this->setBody(jTraceEx($exception));
|
$this->setBody(jTraceEx($exception));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param $data
|
* @param $data
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function execute($path, $data)
|
private function execute($path, $data): void
|
||||||
{
|
{
|
||||||
$this->client->execute($this->setParams($path, $data));
|
$this->client->execute($this->setParams($path, $data));
|
||||||
if (in_array($this->client->getStatusCode(), [502, 404])) {
|
if ($this->client->statusCode < 1) {
|
||||||
$this->retry($path, $data);
|
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
||||||
} else {
|
|
||||||
$this->setStatusCode($this->client->getStatusCode());
|
$errMsg = sprintf("%s://%s:%s/%s -> error: %s", $this->isSSL() ? "https" : "http",
|
||||||
$this->setBody($this->client->getBody());
|
$this->getHost(), $this->getPort(), $path, $this->client->errMsg);
|
||||||
$this->setResponseHeader($this->client->headers);
|
|
||||||
}
|
$errMsg .= print_r($data, true);
|
||||||
}
|
|
||||||
|
$logger->error($errMsg);
|
||||||
|
}
|
||||||
|
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
|
* @param $path
|
||||||
* @throws Exception
|
* @param $data
|
||||||
*/
|
* @return void
|
||||||
private function retry($path, $data)
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if (Context::increment('retry') <= $this->retryNum) {
|
private function retry($path, $data): void
|
||||||
sleep($this->retryTimeout);
|
{
|
||||||
|
if (Context::increment('retry') <= $this->retryNum) {
|
||||||
|
sleep($this->retryTimeout);
|
||||||
|
|
||||||
$this->execute($path, $data);
|
$this->execute($path, $data);
|
||||||
} else {
|
} else {
|
||||||
Context::remove('retry');
|
Context::remove('retry');
|
||||||
|
|
||||||
$this->setStatusCode(curl_errno($this->client));
|
$this->setStatusCode(curl_errno($this->client));
|
||||||
$this->setBody(curl_error($this->client));
|
$this->setBody(curl_error($this->client));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $host
|
* @param $host
|
||||||
* @param $isHttps
|
* @param $isHttps
|
||||||
*/
|
*/
|
||||||
private function generate_client($host, $isHttps): 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);
|
||||||
} else {
|
} else {
|
||||||
$this->client = new SwowClient($host, $this->getPort(), false);
|
$this->client = new SwowClient($host, $this->getPort(), false);
|
||||||
}
|
}
|
||||||
$this->client->set($this->settings());
|
$this->client->set($this->settings());
|
||||||
if (!empty($this->getAgent())) {
|
if (!empty($this->getAgent())) {
|
||||||
$this->withAddedHeader('User-Agent', $this->getAgent());
|
$this->withAddedHeader('User-Agent', $this->getAgent());
|
||||||
}
|
}
|
||||||
$this->client->setHeaders($this->getHeader());
|
$this->client->setHeaders($this->getHeader());
|
||||||
$this->client->setMethod(strtoupper($this->getMethod()));
|
$this->client->setMethod(strtoupper($this->getMethod()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $path
|
* @param $path
|
||||||
* @param $data
|
* @param $data
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function setParams($path, $data): string
|
private function setParams($path, $data): string
|
||||||
{
|
{
|
||||||
$content = $this->getData()->getContents();
|
$content = $this->getData()->getContents();
|
||||||
if (!empty($content)) {
|
if (!empty($content)) {
|
||||||
$this->client->setData($content);
|
$this->client->setData($content);
|
||||||
}
|
}
|
||||||
if ($this->isGet()) {
|
if ($this->isGet()) {
|
||||||
if (!empty($data)) $path .= '?' . $data;
|
if (!empty($data)) $path .= '?' . $data;
|
||||||
} else {
|
} else {
|
||||||
$data = $this->mergeParams($data);
|
$data = $this->mergeParams($data);
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
$this->client->setData($data);
|
$this->client->setData($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function close(): void
|
public function close(): void
|
||||||
{
|
{
|
||||||
$this->client->close();
|
$this->client->close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user