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

Delay #18

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/retype-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
contents: write

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v2

- uses: retypeapp/action-build@latest

Expand Down
68 changes: 38 additions & 30 deletions src/ConnectorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,48 @@
use Fansipan\Retry\Delay;
use Fansipan\Retry\GenericRetryStrategy;

/**
* @template T of ConnectorInterface
*/
class ConnectorConfigurator
{
/**
* @var array<array-key, callable(\Psr\Http\Message\RequestInterface, callable): \Psr\Http\Message\ResponseInterface>
* @var array
*/
protected $middleware = [];
private $handlers = [];

/**
* Configure the given connector with options for current request.
*
* @template T of ConnectorInterface
* @param T $connector
* @return T
*/
final public function configure(ConnectorInterface $connector): ConnectorInterface
{
if (empty($this->middleware)) {
return $connector;
}

$clone = clone $connector;

foreach ($this->middleware as $name => $middleware) {
$clone->middleware()->unshift($middleware, \is_string($name) ? $name : '');
foreach ($this->handlers as $handler) {
$handler($clone);
}

return $clone;
}

/**
* Register a configuration handler.
*
* @param \Closure(T): void $handler
* @return static
*/
protected function register(\Closure $handler)
{
$clone = clone $this;

$clone->handlers[] = $handler;

return $clone;
}

/**
* Indicate that a failed request should be retried.
*
Expand All @@ -50,17 +63,14 @@ public function retry(
?RetryStrategyInterface $retryStrategy = null,
bool $throw = true
) {
$strategy = $retryStrategy ?? new GenericRetryStrategy(new Delay(1000, 2.0));

$clone = clone $this;

$clone->middleware['retry_requests'] = new RetryRequests(
$strategy,
$maxRetries,
$throw
);

return $clone;
return $this->register(function (ConnectorInterface $connector) use ($retryStrategy, $maxRetries, $throw) {
$strategy = $retryStrategy ?? new GenericRetryStrategy(new Delay(1000, 2.0));
$middleware = new RetryRequests($strategy, $maxRetries, $throw);
$connector->middleware()->unshift(
$middleware->withClient($connector->client()),
'retry_requests'
);
});
}

/**
Expand All @@ -75,15 +85,13 @@ public function followRedirects(
bool $strict = false,
bool $referer = false
) {
$clone = clone $this;

$clone->middleware['follow_redirects'] = new FollowRedirects(
$max,
$protocols,
$strict,
$referer
);

return $clone;
return $this->register(function (ConnectorInterface $connector) use ($max, $protocols, $strict, $referer) {
$connector->middleware()->unshift(new FollowRedirects(
$max,
$protocols,
$strict,
$referer
), 'follow_redirects');
});
}
}
49 changes: 33 additions & 16 deletions src/Middleware/RetryRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,49 @@
use Fansipan\Contracts\RetryStrategyInterface;
use Fansipan\Exception\RequestRetryFailedException;
use Fansipan\Retry\RetryContext;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class RetryRequests
{
/**
* @var \Fansipan\Retry\RetryContext
* @var RetryContext
*/
private $context;

/**
* @var \Fansipan\Contracts\RetryStrategyInterface
* @var RetryStrategyInterface
*/
private $strategy;

public function __construct(RetryStrategyInterface $strategy, int $maxRetries = 3, bool $throw = true)
{
/**
* @var ClientInterface|null
*/
private $client;

public function __construct(
RetryStrategyInterface $strategy,
int $maxRetries = 3,
bool $throw = true
) {
$this->context = new RetryContext($maxRetries, $throw);
$this->strategy = $strategy;
}

/**
* @throws \Fansipan\Exception\RequestRetryFailedException
* Set the client.
*/
public function withClient(ClientInterface $client): self
{
$clone = clone $this;
$clone->client = $client;

return $clone;
}

/**
* @throws RequestRetryFailedException
*/
public function __invoke(RequestInterface $request, callable $next): ResponseInterface
{
Expand All @@ -40,24 +60,21 @@
}

$this->context->attempting();
$stop = $this->context->maxRetries() < $this->context->attempts();

if ($stop) {
if ($this->context->throwable()) {
throw new RequestRetryFailedException(
\sprintf('Maximum %d retries reached.', $this->context->maxRetries()),
$request,
$response
);
}

if ($this->context->shouldStop()) {
$this->context->throwExceptionIfNeeded($request, $response);

return $response;
}

$delay = $this->getDelayFromHeaders($response) ?? $this->strategy->delay($this->context);

if ($delay > 0) {
\usleep($delay * 1000);
if ($this->client !== null && \method_exists($this->client, 'delay')) {
$this->client->delay($delay);

Check warning on line 74 in src/Middleware/RetryRequests.php

View check run for this annotation

Codecov / codecov/patch

src/Middleware/RetryRequests.php#L74

Added line #L74 was not covered by tests
} else {
\usleep($delay * 1000);
}
}

return $this($request, $next);
Expand Down
7 changes: 5 additions & 2 deletions src/Retry/Delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class Delay implements DelayStrategyInterface
*/
private $maxDelayMs;

public function __construct(int $delayMs = 1000, float $multiplier = 1.0, int $maxDelayMs = 0)
{
public function __construct(
int $delayMs = 1000,
float $multiplier = 1.0,
int $maxDelayMs = 0
) {
if ($delayMs < 0) {
// @codeCoverageIgnoreStart
throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMs));
Expand Down
30 changes: 24 additions & 6 deletions src/Retry/RetryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

namespace Fansipan\Retry;

use Fansipan\Exception\RequestRetryFailedException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @internal
*/
final class RetryContext
{
/**
Expand Down Expand Up @@ -34,18 +41,29 @@ public function attempting(): void
$this->attempts++;
}

public function maxRetries(): int
public function attempts(): int
{
return $this->maxRetries;
return $this->attempts;
}

public function attempts(): int
public function shouldStop(): bool
{
return $this->attempts;
return $this->maxRetries < $this->attempts;
}

public function throwable(): bool
/**
* @throws RequestRetryFailedException
*/
public function throwExceptionIfNeeded(RequestInterface $request, ResponseInterface $response): void
{
return $this->throw;
if (! $this->throw) {
return;
}

throw new RequestRetryFailedException(
\sprintf('Maximum %d retries reached.', $this->maxRetries),
$request,
$response
);
}
}