-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1bee09
commit b779408
Showing
7 changed files
with
168 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Subscription/RetryStrategy/ClockBasedRetryStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wwwision\DCBLibrary\Subscription\RetryStrategy; | ||
|
||
use DateTimeImmutable; | ||
use Psr\Clock\ClockInterface; | ||
use Wwwision\DCBLibrary\Subscription\Subscription; | ||
|
||
final class ClockBasedRetryStrategy implements RetryStrategy | ||
{ | ||
public const DEFAULT_BASE_DELAY = 5; | ||
public const DEFAULT_DELAY_FACTOR = 2; | ||
public const DEFAULT_MAX_ATTEMPTS = 5; | ||
|
||
/** | ||
* @param int $baseDelay in seconds | ||
* @param positive-int $maxAttempts | ||
*/ | ||
public function __construct( | ||
private readonly ClockInterface $clock, | ||
private readonly int $baseDelay = self::DEFAULT_BASE_DELAY, | ||
private readonly float $delayFactor = self::DEFAULT_DELAY_FACTOR, | ||
private readonly int $maxAttempts = self::DEFAULT_MAX_ATTEMPTS, | ||
) { | ||
} | ||
|
||
public function shouldRetry(Subscription $subscription): bool | ||
{ | ||
if ($subscription->retryAttempt >= $this->maxAttempts) { | ||
return false; | ||
} | ||
|
||
$lastSavedAt = $subscription->lastSavedAt; | ||
|
||
if ($lastSavedAt === null) { | ||
return false; | ||
} | ||
|
||
$nextRetryDate = $this->calculateNextRetryDate($lastSavedAt, $subscription->retryAttempt); | ||
|
||
return $nextRetryDate <= $this->clock->now(); | ||
} | ||
|
||
private function calculateNextRetryDate(DateTimeImmutable $lastDate, int $attempt): DateTimeImmutable | ||
{ | ||
$nextDate = $lastDate->modify(sprintf('+%d seconds', $this->calculateDelay($attempt))); | ||
|
||
if ($nextDate === false) { | ||
throw new \RuntimeException('Could not calculate next retry date.', 1721897113); | ||
} | ||
|
||
return $nextDate; | ||
} | ||
|
||
private function calculateDelay(int $attempt): int | ||
{ | ||
return (int)round($this->baseDelay * ($this->delayFactor ** $attempt)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wwwision\DCBLibrary\Subscription\RetryStrategy; | ||
|
||
use Wwwision\DCBLibrary\Subscription\Subscription; | ||
|
||
final class NoRetryStrategy implements RetryStrategy | ||
{ | ||
public function shouldRetry(Subscription $subscription): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wwwision\DCBLibrary\Subscription\RetryStrategy; | ||
|
||
use Wwwision\DCBLibrary\Subscription\Subscription; | ||
|
||
interface RetryStrategy | ||
{ | ||
public function shouldRetry(Subscription $subscription): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters