Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jul 29, 2023
1 parent df1dfdb commit a541dee
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Contracts/DelayStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/Retry/Backoff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Retry/Delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/RetryRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit a541dee

Please sign in to comment.