This commit is contained in:
2026-05-09 01:18:51 +02:00
parent 7116ee4619
commit 959970c150
132 changed files with 21310 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Temporal\Shared;
use Temporal\Exception\Failure\ApplicationFailure;
class FaultSimulator
{
public static function maybeApply(array $config, string $activityName = ''): void
{
if (empty($config)) {
return;
}
// 1. Base latency
$latencyMs = $config['latencyMs'] ?? 0;
if ($latencyMs > 0) {
usleep($latencyMs * 1000);
}
// 2. Rate limiting — throw ApplicationFailure with nextRetryDelay
$rl = $config['rateLimiting'] ?? [];
if (!empty($rl['enabled']) && ($rl['hitChance'] ?? 0) > 0) {
if (rand(1, 100) <= $rl['hitChance']) {
$retryAfterMs = $rl['retryAfterMs'] ?? 2000;
$retrySeconds = (int) ceil($retryAfterMs / 1000);
throw new ApplicationFailure(
"Rate limited (429) on {$activityName}",
'RateLimited',
false, // nonRetryable = false → Temporal WILL retry
null,
null,
\DateInterval::createFromDateString("{$retrySeconds} seconds"),
);
}
}
// 3. Random failure — plain RuntimeException (Temporal retries automatically)
$failureRate = $config['failureRate'] ?? 0;
if ($failureRate > 0 && rand(1, 100) <= $failureRate) {
throw new \RuntimeException("Simulated failure on {$activityName}");
}
}
}