DaitoGoogleChatRateLimitMiddleware.php
746 Bytes
<?php
namespace Daito\Lib\DaitoGoogleChat\Middleware;
use Illuminate\Support\Facades\RateLimiter;
class DaitoGoogleChatRateLimitMiddleware
{
public function handle($job, $next): void
{
$rateLimitKey = (string) config('daito-google-chat.rate_limit_key', 'daito-google-chat:webhook');
$maxJobs = max(1, (int) config('daito-google-chat.rate_limit_max_jobs', 20));
$decaySeconds = max(1, (int) config('daito-google-chat.rate_limit_decay_seconds', 60));
if (RateLimiter::tooManyAttempts($rateLimitKey, $maxJobs)) {
$job->release(max(1, RateLimiter::availableIn($rateLimitKey)));
return;
}
RateLimiter::hit($rateLimitKey, $decaySeconds);
$next($job);
}
}