-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kim Pepper <[email protected]>
- Loading branch information
Showing
10 changed files
with
212 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\HttpClient; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use OpenSearch\Client; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Builds an OpenSearch client using Guzzle. | ||
*/ | ||
class GuzzleHttpClientFactory implements HttpClientFactoryInterface | ||
{ | ||
public function __construct( | ||
protected int $maxRetries = 0, | ||
protected ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(array $options): ClientInterface | ||
{ | ||
if (!isset($options['base_uri'])) { | ||
throw new \InvalidArgumentException('The base_uri option is required.'); | ||
} | ||
// Set default configuration. | ||
$defaults = [ | ||
'headers' => [ | ||
'Accept' => 'application/json', | ||
'Content-Type' => 'application/json', | ||
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION), | ||
], | ||
]; | ||
|
||
// Merge the default options with the provided options. | ||
$config = array_merge_recursive($defaults, $options); | ||
|
||
$stack = HandlerStack::create(); | ||
|
||
// Handle retries if max_retries is set. | ||
if ($this->maxRetries > 0) { | ||
$decider = new GuzzleRetryDecider($this->maxRetries, $this->logger); | ||
$stack->push(Middleware::retry($decider(...))); | ||
} | ||
|
||
$config['handler'] = $stack; | ||
|
||
return new GuzzleClient($config); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace OpenSearch\HttpClient; | ||
|
||
use GuzzleHttp\Exception\ConnectException; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Retry decider for Guzzle HTTP Client. | ||
*/ | ||
class GuzzleRetryDecider | ||
{ | ||
public function __construct( | ||
protected ?int $maxRetries = 0, | ||
protected ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
public function __invoke(int $retries, ?RequestInterface $request, ?ResponseInterface $response, $exception): bool | ||
{ | ||
if ($retries >= $this->maxRetries) { | ||
return false; | ||
} | ||
if ($exception instanceof ConnectException) { | ||
$this->logger?->warning( | ||
'Retrying request {retries} of {maxRetries}: {exception}', | ||
[ | ||
'retries' => $retries, | ||
'maxRetries' => $this->maxRetries, | ||
'exception' => $exception->getMessage(), | ||
] | ||
); | ||
return true; | ||
} | ||
if ($response && $response->getStatusCode() >= 500) { | ||
$this->logger?->warning( | ||
'Retrying request {retries} of {maxRetries}: Status code {status}', | ||
[ | ||
'retries' => $retries, | ||
'maxRetries' => $this->maxRetries, | ||
'status' => $response->getStatusCode(), | ||
] | ||
); | ||
return true; | ||
} | ||
// We only retry if there is a 500 or a ConnectException. | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenSearch\Tests\HttpClient; | ||
|
||
use ColinODell\PsrTestLogger\TestLogger; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use OpenSearch\HttpClient\GuzzleRetryDecider; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Test the Guzzle retry decider. | ||
* | ||
* @coversDefaultClass \OpenSearch\HttpClient\GuzzleRetryDecider | ||
*/ | ||
class GuzzleRetryDeciderTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::__invoke | ||
*/ | ||
public function testMaxRetriesDoesNotRetry(): void | ||
{ | ||
$decider = new GuzzleRetryDecider(2); | ||
$this->assertFalse($decider(2, null, null, null)); | ||
} | ||
|
||
public function test500orNoExceptionDoesNotRetry(): void | ||
{ | ||
$decider = new GuzzleRetryDecider(2); | ||
$this->assertFalse($decider(0, null, null, null)); | ||
} | ||
|
||
/** | ||
* @covers ::__invoke | ||
*/ | ||
public function testConnectExceptionRetries(): void | ||
{ | ||
$logger = new TestLogger(); | ||
$decider = new GuzzleRetryDecider(2, $logger); | ||
$this->assertTrue($decider(0, null, null, new ConnectException('Error', $this->createMock(RequestInterface::class)))); | ||
$this->assertTrue($logger->hasWarning([ | ||
'level' => 'warning', | ||
'message' => 'Retrying request {retries} of {maxRetries}: {exception}', | ||
'context' => [ | ||
'retries' => 0, | ||
'maxRetries' => 2, | ||
'exception' => 'Error', | ||
], | ||
])); | ||
} | ||
|
||
public function testStatus500Retries(): void | ||
{ | ||
$logger = new TestLogger(); | ||
$decider = new GuzzleRetryDecider(2, $logger); | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->method('getStatusCode')->willReturn(500); | ||
|
||
$this->assertTrue($decider(0, null, $response, null)); | ||
$this->assertTrue($logger->hasWarning([ | ||
'level' => 'warning', | ||
'message' => 'Retrying request {retries} of {maxRetries}: Status code {status}', | ||
'context' => [ | ||
'retries' => 0, | ||
'maxRetries' => 2, | ||
'status' => 500, | ||
], | ||
])); | ||
} | ||
} |
Oops, something went wrong.