chore: RateLimiter fixes
This commit is contained in:
@@ -14,7 +14,7 @@ use App\Framework\Core\ValueObjects\Timestamp;
|
|||||||
* Enhanced with sophisticated threat analysis, attack pattern detection,
|
* Enhanced with sophisticated threat analysis, attack pattern detection,
|
||||||
* and adaptive response capabilities from the WAF system.
|
* and adaptive response capabilities from the WAF system.
|
||||||
*/
|
*/
|
||||||
readonly class RateLimitResult
|
final readonly class RateLimitResult
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private bool $allowed,
|
private bool $allowed,
|
||||||
|
|||||||
@@ -20,13 +20,15 @@ final readonly class SlidingWindowTokenBucket
|
|||||||
|
|
||||||
private SlidingWindow $analyticsWindow;
|
private SlidingWindow $analyticsWindow;
|
||||||
|
|
||||||
|
private Duration $refillInterval;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $identifier,
|
private string $identifier,
|
||||||
private int $capacity,
|
private int $capacity,
|
||||||
private int $refillRate,
|
private int $refillRate,
|
||||||
SlidingWindowFactory $windowFactory,
|
SlidingWindowFactory $windowFactory,
|
||||||
private TimeProviderInterface $timeProvider = new SystemTimeProvider(),
|
private TimeProviderInterface $timeProvider = new SystemTimeProvider(),
|
||||||
private Duration $refillInterval = Duration::SECOND,
|
?Duration $refillInterval = null,
|
||||||
private bool $persistent = true
|
private bool $persistent = true
|
||||||
) {
|
) {
|
||||||
// Window for tracking token consumption (short window for actual limiting)
|
// 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
|
windowSize: Duration::fromMinutes(5), // Longer window for pattern analysis
|
||||||
persistent: $this->persistent
|
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
|
// Available tokens = capacity - consumed + refilled, capped at capacity
|
||||||
$availableTokens = min(
|
return min(
|
||||||
$this->capacity,
|
$this->capacity,
|
||||||
max(0, $this->capacity - $tokensConsumed + $tokensRefilled)
|
max(0, $this->capacity - $tokensConsumed + $tokensRefilled)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $availableTokens;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user