From 540f7227e5efe03c8f36f73e75d6c2f83cd3833e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 6 Dec 2019 18:29:07 +0800 Subject: [PATCH 1/2] Fixed guzzle will be retried when the response has the correct status code 2XX --- src/guzzle/src/RetryMiddleware.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/guzzle/src/RetryMiddleware.php b/src/guzzle/src/RetryMiddleware.php index 9e4cfc70c..950902e73 100644 --- a/src/guzzle/src/RetryMiddleware.php +++ b/src/guzzle/src/RetryMiddleware.php @@ -31,7 +31,7 @@ class RetryMiddleware implements MiddlewareInterface public function getMiddleware(): callable { return Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null) { - if ($response && $response->getStatusCode() !== 200 && $retries < $this->retries) { + if (! $this->isOk($response) && $retries < $this->retries) { return true; } return false; @@ -39,4 +39,12 @@ class RetryMiddleware implements MiddlewareInterface return $this->delay; }); } + + /** + * Check the response status is correct. + */ + protected function isOk(?ResponseInterface $response): bool + { + return $response && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; + } } From 8c1f1eee45cfc75d8f4d9dfd1448ddfb532dde2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 6 Dec 2019 18:42:17 +0800 Subject: [PATCH 2/2] Added testing. --- CHANGELOG.md | 4 ++ .../tests/Cases/HandlerStackFactoryTest.php | 39 +++++++++++++++++++ .../tests/Stub/CoroutineHandlerStub.php | 12 +++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ae9ed757..9662f3483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # v1.1.10 - TBD +## Fixed + +- [#1104](https://github.com/hyperf/hyperf/pull/1104) Fixed guzzle will be retried when the response has the correct status code 2xx. + # v1.1.9 - 2019-12-05 ## Added diff --git a/src/guzzle/tests/Cases/HandlerStackFactoryTest.php b/src/guzzle/tests/Cases/HandlerStackFactoryTest.php index 74c0b0cf8..d325111b8 100644 --- a/src/guzzle/tests/Cases/HandlerStackFactoryTest.php +++ b/src/guzzle/tests/Cases/HandlerStackFactoryTest.php @@ -12,6 +12,8 @@ declare(strict_types=1); namespace HyperfTest\Guzzle\Cases; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use GuzzleHttp\HandlerStack; use Hyperf\Di\Container; use Hyperf\Guzzle\CoroutineHandler; @@ -20,6 +22,7 @@ use Hyperf\Guzzle\PoolHandler; use Hyperf\Guzzle\RetryMiddleware; use Hyperf\Pool\SimplePool\PoolFactory; use Hyperf\Utils\ApplicationContext; +use HyperfTest\Guzzle\Stub\CoroutineHandlerStub; use Mockery; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; @@ -108,6 +111,42 @@ class HandlerStackFactoryTest extends TestCase $this->assertEquals(['http_errors', 'allow_redirects', 'cookies', 'prepare_body', 'retry', 'retry_again'], $items); } + public function testRetryMiddleware() + { + $this->setContainer(); + + $factory = new HandlerStackFactory(); + $stack = $factory->create([], ['retry_again' => [RetryMiddleware::class, [1, 10]]]); + $stack->setHandler($stub = new CoroutineHandlerStub(201)); + + $client = new Client([ + 'handler' => $stack, + 'base_uri' => 'http://127.0.0.1:9501', + ]); + + $resposne = $client->get('/'); + $this->assertSame(201, $resposne->getStatusCode()); + $this->assertSame(1, $stub->count); + + $stack = $factory->create([], ['retry' => [RetryMiddleware::class, [1, 10]]]); + $stack->setHandler($stub = new CoroutineHandlerStub(400)); + $client = new Client([ + 'handler' => $stack, + 'base_uri' => 'http://127.0.0.1:9501', + ]); + + $this->expectExceptionCode(400); + $this->expectException(ClientException::class); + $this->expectExceptionMessageRegExp('/400 Bad Request/'); + + try { + $client->get('/'); + } catch (\Throwable $exception) { + $this->assertSame(2, $stub->count); + throw $exception; + } + } + protected function setContainer() { $container = Mockery::mock(Container::class); diff --git a/src/guzzle/tests/Stub/CoroutineHandlerStub.php b/src/guzzle/tests/Stub/CoroutineHandlerStub.php index dfa1db04c..b6dffde05 100644 --- a/src/guzzle/tests/Stub/CoroutineHandlerStub.php +++ b/src/guzzle/tests/Stub/CoroutineHandlerStub.php @@ -17,6 +17,15 @@ use Swoole\Coroutine\Http\Client; class CoroutineHandlerStub extends CoroutineHandler { + public $count = 0; + + protected $statusCode; + + public function __construct($statusCode = 200) + { + $this->statusCode = $statusCode; + } + public function checkStatusCode(Client $client, $request) { return parent::checkStatusCode($client, $request); @@ -33,7 +42,8 @@ class CoroutineHandlerStub extends CoroutineHandler 'headers' => $client->requestHeaders, 'uri' => $path, ]); - $client->statusCode = 200; + $client->statusCode = $this->statusCode; $client->headers = []; + ++$this->count; } }