chore: RateLimiter fixes

This commit is contained in:
2025-10-27 19:23:22 +01:00
parent 8ef2b8547d
commit 5d9258d8c3
2 changed files with 7 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ use App\Framework\Core\ValueObjects\Timestamp;
* Enhanced with sophisticated threat analysis, attack pattern detection,
* and adaptive response capabilities from the WAF system.
*/
readonly class RateLimitResult
final readonly class RateLimitResult
{
public function __construct(
private bool $allowed,

View File

@@ -20,13 +20,15 @@ final readonly class SlidingWindowTokenBucket
private SlidingWindow $analyticsWindow;
private Duration $refillInterval;
public function __construct(
private string $identifier,
private int $capacity,
private int $refillRate,
SlidingWindowFactory $windowFactory,
private TimeProviderInterface $timeProvider = new SystemTimeProvider(),
private Duration $refillInterval = Duration::SECOND,
?Duration $refillInterval = null,
private bool $persistent = true
) {
// Window for tracking token consumption (short window for actual limiting)
@@ -42,6 +44,8 @@ final readonly class SlidingWindowTokenBucket
windowSize: Duration::fromMinutes(5), // Longer window for pattern analysis
persistent: $this->persistent
);
$this->refillInterval = $refillInterval ?? Duration::fromSeconds(1);
}
/**
@@ -212,11 +216,9 @@ final readonly class SlidingWindowTokenBucket
);
// Available tokens = capacity - consumed + refilled, capped at capacity
$availableTokens = min(
return min(
$this->capacity,
max(0, $this->capacity - $tokensConsumed + $tokensRefilled)
);
return $availableTokens;
}
}