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

[8.0] Remove the exponentialDelay function from RetryMiddleware #2500

Open
wants to merge 2 commits into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -820,11 +820,6 @@ parameters:
count: 1
path: src/RetryMiddleware.php

-
message: "#^Method GuzzleHttp\\\\RetryMiddleware\\:\\:doRetry\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/RetryMiddleware.php

-
message: "#^Method GuzzleHttp\\\\RetryMiddleware\\:\\:doRetry\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
34 changes: 13 additions & 21 deletions src/RetryMiddleware.php
Expand Up @@ -21,14 +21,14 @@ class RetryMiddleware
private $delay;

/**
* @param callable $decider Function that accepts the number of retries,
* a request, [response], and [exception] and
* returns true if the request is to be
* retried.
* @param callable $nextHandler Next handler to invoke.
* @param callable $delay Function that accepts the number of retries
* and [response] and returns the number of
* milliseconds to delay.
* @param callable $decider Function that accepts the number of retries,
* a request, [response], and [exception] and
* returns true if the request is to be
* retried.
* @param callable $nextHandler Next handler to invoke.
* @param callable|null $delay Function that accepts the number of retries
* and [response] and returns the number of
* milliseconds to delay.
*/
public function __construct(
callable $decider,
Expand All @@ -37,17 +37,9 @@ public function __construct(
) {
$this->decider = $decider;
$this->nextHandler = $nextHandler;
$this->delay = $delay ?: __CLASS__ . '::exponentialDelay';
}

/**
* Default exponential backoff delay function.
*
* @return int milliseconds.
*/
public static function exponentialDelay(int $retries): int
{
return (int) \pow(2, $retries - 1) * 1000;
$this->delay = $delay ?: function (int $retries): int {
return (int) \pow(2, $retries - 1) * 1000;
};
}

public function __invoke(RequestInterface $request, array $options): PromiseInterface
Expand Down Expand Up @@ -88,7 +80,7 @@ private function onFulfilled(RequestInterface $req, array $options): callable
*/
private function onRejected(RequestInterface $req, array $options): callable
{
return function ($reason) use ($req, $options) {
return function ($reason) use ($req, $options): PromiseInterface {
if (!\call_user_func(
$this->decider,
$options['retries'],
Expand All @@ -102,7 +94,7 @@ private function onRejected(RequestInterface $req, array $options): callable
};
}

private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null)
private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface
{
$options['delay'] = \call_user_func($this->delay, ++$options['retries'], $response);

Expand Down
37 changes: 32 additions & 5 deletions tests/RetryMiddlewareTest.php
@@ -1,4 +1,5 @@
<?php

namespace GuzzleHttp\Tests;

use GuzzleHttp\Client;
Expand Down Expand Up @@ -71,10 +72,36 @@ public function testCanRetryExceptions()

public function testBackoffCalculateDelay()
{
self::assertSame(0, RetryMiddleware::exponentialDelay(0));
self::assertSame(1000, RetryMiddleware::exponentialDelay(1));
self::assertSame(2000, RetryMiddleware::exponentialDelay(2));
self::assertSame(4000, RetryMiddleware::exponentialDelay(3));
self::assertSame(8000, RetryMiddleware::exponentialDelay(4));
$calls = [];
$decider = function ($retries, $request, $response, $error) use (&$calls) {
$calls[] = \func_get_args();
return $error instanceof \Exception;
};
$m = Middleware::retry($decider);
$h = new MockHandler();
$c = new Client(['handler' => $m($h)]);

$h->append(new \Exception(), new Response());
$p = $c->send(new Request('GET', 'http://test.com'), []);

self::assertSame(200, $p->getStatusCode());
self::assertCount(2, $calls);
self::assertSame(1000, $h->getLastOptions()['delay']);

$calls = [];
$h->append(new \Exception(), new \Exception(), new Response());
$p = $c->send(new Request('GET', 'http://test.com'), []);

self::assertSame(200, $p->getStatusCode());
self::assertCount(3, $calls);
self::assertSame(2000, $h->getLastOptions()['delay']);

$calls = [];
$h->append(new \Exception(), new \Exception(), new \Exception(), new Response());
$p = $c->send(new Request('GET', 'http://test.com'), []);

self::assertSame(200, $p->getStatusCode());
self::assertCount(4, $calls);
self::assertSame(4000, $h->getLastOptions()['delay']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main problem here is that these tests are causing actual delays.. any workaround would be helpful here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could override the sleep function, or create an injectable/overrideable clock that falls back to the system clock. But I think this is fine for now as it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or.. we could just keep it public.

The tests were easier to read and write and did not take forever to run =)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or...we could not test it. 😄 It's a trivial sleep after all...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has recently been a bug in this trivial code. I think the tests should be there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not have this delay v7. Will add it to v8 milestone...

}
}