Skip to content

Commit

Permalink
Merge 88ea897 into ffe22b2
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Oct 8, 2019
2 parents ffe22b2 + 88ea897 commit f11f2fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 36 deletions.
8 changes: 3 additions & 5 deletions src/Connection/UnprocessedRequestException.php
Expand Up @@ -3,13 +3,11 @@
namespace Amp\Http\Client\Connection;

use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\RequestException;

final class UnprocessedRequestException extends RequestException
final class UnprocessedRequestException extends HttpException
{
public function __construct(Request $request, HttpException $previous)
public function __construct(HttpException $previous)
{
parent::__construct($request, "The request was not processed and can be safely retried", 0, $previous);
parent::__construct("The request was not processed and can be safely retried", 0, $previous);
}
}
15 changes: 5 additions & 10 deletions src/Interceptor/RetryRequests.php
Expand Up @@ -6,11 +6,8 @@
use Amp\Http\Client\ApplicationInterceptor;
use Amp\Http\Client\Client;
use Amp\Http\Client\Connection\UnprocessedRequestException;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\InvalidRequestException;
use Amp\Http\Client\ParseException;
use Amp\Http\Client\Request;
use Amp\Http\Client\TimeoutException;
use Amp\Http\Client\SocketException;
use Amp\Promise;
use function Amp\call;

Expand All @@ -27,23 +24,21 @@ public function __construct(int $retryLimit)
public function request(Request $request, CancellationToken $cancellation, Client $client): Promise
{
return call(function () use ($request, $cancellation, $client) {
$attempts = 1;
$attempt = 1;

do {
try {
return yield $client->request(clone $request, $cancellation);
} catch (UnprocessedRequestException $exception) {
// Request was deemed retryable by connection, so carry on.
} catch (InvalidRequestException | ParseException | TimeoutException $exception) {
// Request is or response is invalid or request timed out, so do not retry.
throw $exception;
} catch (HttpException $exception) {
} catch (SocketException $exception) {
if (!$request->isIdempotent()) {
throw $exception;
}

// Request can safely be retried.
}
} while ($attempts++ <= $this->retryLimit);
} while ($attempt++ <= $this->retryLimit);

throw $exception;
});
Expand Down
15 changes: 14 additions & 1 deletion src/InvalidRequestException.php
Expand Up @@ -2,6 +2,19 @@

namespace Amp\Http\Client;

final class InvalidRequestException extends RequestException
final class InvalidRequestException extends HttpException
{
/** @var Request */
private $request;

public function __construct(Request $request, string $message, int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->request = $request;
}

public function getRequest(): Request
{
return $this->request;
}
}
20 changes: 0 additions & 20 deletions src/RequestException.php

This file was deleted.

0 comments on commit f11f2fd

Please sign in to comment.