diff --git a/src/Contracts/DelayStrategyInterface.php b/src/Contracts/DelayStrategyInterface.php index 2b06d5f..54d20ad 100644 --- a/src/Contracts/DelayStrategyInterface.php +++ b/src/Contracts/DelayStrategyInterface.php @@ -6,5 +6,8 @@ interface DelayStrategyInterface { - public function delayFor(int $attempts): int; + /** + * Returns the time to wait in milliseconds for given attempt. + */ + public function delayFor(int $attempt): int; } diff --git a/src/Retry/Backoff.php b/src/Retry/Backoff.php index edecb7b..8e732ae 100644 --- a/src/Retry/Backoff.php +++ b/src/Retry/Backoff.php @@ -24,8 +24,8 @@ public function __construct(array $backoff, int $fallbackDelayMs = 1000) $this->fallbackDelayMs = $fallbackDelayMs; } - public function delayFor(int $attempts): int + public function delayFor(int $attempt): int { - return $this->backoff[$attempts - 1] ?? $this->fallbackDelayMs; + return $this->backoff[$attempt - 1] ?? $this->fallbackDelayMs; } } diff --git a/src/Retry/Delay.php b/src/Retry/Delay.php index de5f943..4488330 100644 --- a/src/Retry/Delay.php +++ b/src/Retry/Delay.php @@ -45,9 +45,9 @@ public function __construct(int $delayMs = 1000, float $multiplier = 1.0, int $m $this->maxDelayMs = $maxDelayMs; } - public function delayFor(int $attempts): int + public function delayFor(int $attempt): int { - $delay = $this->delayMs * $this->multiplier ** $attempts; + $delay = $this->delayMs * $this->multiplier ** $attempt; if ($delay > $this->maxDelayMs && $this->maxDelayMs > 0) { return $this->maxDelayMs; diff --git a/tests/RetryRequestsTest.php b/tests/RetryRequestsTest.php index dc5726d..97b2768 100644 --- a/tests/RetryRequestsTest.php +++ b/tests/RetryRequestsTest.php @@ -41,13 +41,13 @@ public function test_retry_requests_with_custom_strategy(): void $this->expectException(RequestRetryFailedException::class); $this->expectExceptionMessage('Maximum 2 retries reached.'); - $connector->retry(2, RetryCallback::when(function ($request, $response) { + $connector->retry(2, RetryCallback::when(static function () { return true; }, 1000, 1.0))->send(new GetStatusRequest(502)); $client->assertSentCount(2); - $connector->retry(3, RetryCallback::when(function ($request, $response) { + $connector->retry(3, RetryCallback::when(static function () { return true; })->withDelay(new Backoff([1000, 2000, 3000])))->send(new GetStatusRequest(502));