From d589e096af3c517f56d24fa666baa985cf308378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 1 Mar 2022 18:46:08 +0800 Subject: [PATCH] modify plugin name --- AsyncClient.php | 219 -------------------------------------------- ClientAbstracts.php | 45 +++++++++ CoroutineClient.php | 50 ++++++++-- CurlClient.php | 34 ++++++- 4 files changed, 116 insertions(+), 232 deletions(-) delete mode 100644 AsyncClient.php diff --git a/AsyncClient.php b/AsyncClient.php deleted file mode 100644 index 698c0a0..0000000 --- a/AsyncClient.php +++ /dev/null @@ -1,219 +0,0 @@ -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(); - } -} diff --git a/ClientAbstracts.php b/ClientAbstracts.php index 5a107dd..45b47ac 100644 --- a/ClientAbstracts.php +++ b/ClientAbstracts.php @@ -53,6 +53,11 @@ abstract class ClientAbstracts implements IClient private int $statusCode = 200; + protected int $retryNum = 3; + + protected int $retryTimeout = 3; + + private bool $VERIFYPEER = TRUE; @@ -73,6 +78,46 @@ abstract class ClientAbstracts implements IClient 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 * @return $this diff --git a/CoroutineClient.php b/CoroutineClient.php index 201fa63..219c61c 100644 --- a/CoroutineClient.php +++ b/CoroutineClient.php @@ -37,7 +37,7 @@ class CoroutineClient extends ClientAbstracts } $this->withMethod($method) ->coroutine( - $this->matchHost($path), + $path, $this->paramEncode($params) ); } @@ -60,13 +60,13 @@ class CoroutineClient extends ClientAbstracts private function coroutine($url, array|string $data = []): void { try { - $this->generate_client($data, ...$url); + $this->generate_client($this->getHost(), $this->isSSL()); if ($this->client->statusCode < 0) { throw new Exception($this->client->errMsg); } - $this->setStatusCode($this->client->getStatusCode()); - $this->setBody($this->client->getBody()); - $this->setResponseHeader($this->client->headers); + + $this->execute($url, $data); + } catch (\Throwable $exception) { Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]); $this->setStatusCode(-1); @@ -76,12 +76,47 @@ class CoroutineClient extends ClientAbstracts /** + * @param $path * @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 $isHttps - * @param $path */ - private function generate_client($data, $host, $isHttps, $path): void + private function generate_client($host, $isHttps): void { if ($isHttps || $this->isSSL()) { $this->client = new SwowClient($host, 443, true); @@ -94,7 +129,6 @@ class CoroutineClient extends ClientAbstracts } $this->client->setHeaders($this->getHeader()); $this->client->setMethod(strtoupper($this->getMethod())); - $this->client->execute($this->setParams($path, $data)); } diff --git a/CurlClient.php b/CurlClient.php index 2cde233..44d6e1d 100644 --- a/CurlClient.php +++ b/CurlClient.php @@ -138,11 +138,30 @@ class CurlClient extends ClientAbstracts private function execute(): void { $output = curl_exec($this->client); - if ($output === FALSE) { + if ($output !== FALSE) { + $this->explode($output); + } else { $this->setStatusCode(curl_errno($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 { - $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); $status = explode(' ', array_shift($header)); - $this->setStatusCode(intval($status[1])); - $this->setBody($body); - $this->setResponseHeader($header); + $statusCode = intval($status[1]); + if (in_array($statusCode, [502, 404])) { + $this->retry(); + } else { + $this->setStatusCode($statusCode); + $this->setBody($body); + $this->setResponseHeader($header); + } } /**