feat: notification rate limiter
fix: limit server up / down notification limits
This commit is contained in:
@@ -39,6 +39,7 @@ use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Process;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -4038,3 +4039,30 @@ function sslipDomainWarning(string $domains)
|
||||
|
||||
return $showSslipHttpsWarning;
|
||||
}
|
||||
|
||||
function isEmailRateLimited(string $limiterKey, int $decaySeconds = 3600, ?callable $callbackOnSuccess = null): bool
|
||||
{
|
||||
if (isDev()) {
|
||||
$decaySeconds = 120;
|
||||
}
|
||||
$rateLimited = false;
|
||||
$executed = RateLimiter::attempt(
|
||||
$limiterKey,
|
||||
$maxAttempts = 0,
|
||||
function () use (&$rateLimited, &$limiterKey, $callbackOnSuccess) {
|
||||
isDev() && loggy('Rate limit not reached for '.$limiterKey);
|
||||
$rateLimited = false;
|
||||
|
||||
if ($callbackOnSuccess) {
|
||||
$callbackOnSuccess();
|
||||
}
|
||||
},
|
||||
$decaySeconds,
|
||||
);
|
||||
if (! $executed) {
|
||||
isDev() && loggy('Rate limit reached for '.$limiterKey.'. Rate limiter will be disabled for '.$decaySeconds.' seconds.');
|
||||
$rateLimited = true;
|
||||
}
|
||||
|
||||
return $rateLimited;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user