Merge pull request #1104 from limingxinleo/1.1-guzzle

Fixed guzzle will be retried when the response has the correct status  code 2XX
This commit is contained in:
李铭昕 2019-12-06 18:48:09 +08:00 committed by GitHub
commit 5d0a55d474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 2 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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;
}
}