36 lines
1015 B
PHP
36 lines
1015 B
PHP
<?php
|
|
namespace EveAI\Chat;
|
|
|
|
class RateLimiter {
|
|
private const RATE_LIMIT_KEY = 'eveai_rate_limit_';
|
|
private const MAX_REQUESTS = 60; // Maximum requests per window
|
|
private const WINDOW_SECONDS = 60; // Time window in seconds
|
|
|
|
public static function check_rate_limit($identifier): bool {
|
|
$key = self::RATE_LIMIT_KEY . $identifier;
|
|
$requests = get_transient($key);
|
|
|
|
if ($requests === false) {
|
|
set_transient($key, 1, self::WINDOW_SECONDS);
|
|
return true;
|
|
}
|
|
|
|
if ($requests >= self::MAX_REQUESTS) {
|
|
return false;
|
|
}
|
|
|
|
set_transient($key, $requests + 1, self::WINDOW_SECONDS);
|
|
return true;
|
|
}
|
|
|
|
public static function get_remaining_requests($identifier): int {
|
|
$key = self::RATE_LIMIT_KEY . $identifier;
|
|
$requests = get_transient($key);
|
|
|
|
if ($requests === false) {
|
|
return self::MAX_REQUESTS;
|
|
}
|
|
|
|
return max(0, self::MAX_REQUESTS - $requests);
|
|
}
|
|
} |