Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-28209 feat: honor http code 429 and retry-after header #3

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions src/AcrolinxEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Acrolinx\SDK\Models\Response\ProgressResponse;
use Acrolinx\SDK\Models\SignInSuccessData;
use Acrolinx\SDK\Models\SsoSignInOptions;
use Acrolinx\SDK\Utils\AcrolinxLogger;
use Monolog\Logger;
use React\Http\Browser;
use Exception;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -45,17 +47,20 @@ class AcrolinxEndpoint
private $props = null;
private $client;
private $loop;
private $logger;

/**
* AcrolinxEndpoint constructor.
* @param AcrolinxEndPointProperties $props
* @param LoopInterface $loop
* @param $logger
*/
public function __construct(AcrolinxEndPointProperties $props, LoopInterface $loop)
{
$this->props = $props;
$this->client = new Browser($loop);
$this->loop = $loop;
$this->logger = AcrolinxLogger::getInstance('./logs/acrolinx.log', Logger::INFO);
}

/**
Expand Down Expand Up @@ -182,24 +187,58 @@ public function getCapabilities(string $authToken): PromiseInterface
* @param CheckRequest $request
* @return PromiseInterface containing {@see \Acrolinx\SDK\Models\CheckResponse} or Exception
*/
public function check(string $authToken, CheckRequest $request): PromiseInterface
public function check(string $authToken, CheckRequest $request, $attempt = 1): PromiseInterface
{
$deferred = new Deferred();

$this->client->post($this->props->platformUrl . '/api/v1/checking/checks',
$this->getCommonHeaders($authToken), $request->getJson())->then(function (ResponseInterface $response)
use ($deferred) {
use ($request, $authToken, $attempt, $deferred) {
$checkResponse = new CheckResponse($response);
$deferred->resolve($checkResponse);
}, function (Exception $reason) use ($deferred) {
$exception = new AcrolinxServerException($reason->getMessage(), $reason->getCode(),
$reason->getPrevious(), 'Submitting check failed');
$deferred->reject($exception);
}, function (Exception $reason) use ($request, $authToken, $attempt, $deferred) {
$responseCode = $reason->getCode();
$retryAfterExists = $this->getRetryAfter($reason->getResponse());

if ($responseCode == 429 && $attempt <= 5 && $retryAfterExists) {
$this->logger->info('Check failed with 429. Retrying attempt # ' . $attempt);
$retryAfter = $retryAfterExists[0] * 1000;
$retryInterval = $retryAfter * pow(2, $attempt);
$this->loop->addTimer($retryInterval, function () use ($authToken, $request, $attempt, $deferred) {
$this->check($authToken, $request, ++$attempt)->then(function (CheckResponse $checkResponse) use ($deferred) {
$deferred->resolve($checkResponse);
}, function (Exception $reason) use ($deferred) {
$deferred->reject($reason);
});
});
} else {
$exception = new AcrolinxServerException($reason->getMessage(), $reason->getCode(),
$reason->getPrevious(), 'Submitting check failed');
$deferred->reject($exception);
}
});

return $deferred->promise();
}

/**
* Get Retry-After Header.
*
* @param ResponseInterface $response
* @return array|null
*/
private function getRetryAfter(ResponseInterface $response): ?array
{
$retryAfter = $response->getHeaders()['Retry-After'];
if (empty($retryAfter)) {
$retryAfter = $response->getHeaders()['retry-after'];
}
if (empty($retryAfter)) {
$retryAfter = $response->getHeaders()['RETRY-AFTER'];
}
return $retryAfter;
}

/**
* Get supported options for check.
*
Expand Down