Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
How to set the rate limiter for per second in Laravel version 8

How to set the rate limiter per second in Laravel 8. I need to set the rate limiter per second instead of per minute.

Rate Limiter (Laravel 8) - https://laravel.com/docs/8.x/routing#rate-limiting

Right now I'm able to use Laravel's rate limiter for minutes, hours, etc. But I'm trying to achieve a rate limiter for a second. I want to limit 25 requests per sec. (Exported Limit class from "Illuminate\Cache\RateLimiting\Limit")

Please check the following code which I have used

RateLimiter::for('api', function (Request $request) {
        return [
            // Rate limiter based on Client IP Address
            Limit::perMinute(env('IP_ADDR_RATE_LIMITER_PER_MINUTE', 60))->by($request->ip())->response(function () {
                ....
            }),
            // Rate limiter based on API key/User
            Limit::perMinute(env('API_KEY_RATE_LIMITER_PER_MINUTE', 60))->by($request->input('key'))->response(function () {
                ...
            })
        ];
    });

Is there any way to rate-limit 25 requests per second?

Note: also tried adding/changing functions in Illuminate\Cache\RateLimiting\Limit, where I tried altering the per minute function. Thanks in Advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

<?php

namespace App\Http\Cache;

class Limit extends \Illuminate\Cache\RateLimiting\Limit
{
    
    /**
     * Create a new limit instance.
     *
     * @param  mixed|string  $key
     * @param  int  $maxAttempts
     * @param  int|float  $decayMinutes
     * @return void
     */
    public function __construct($key = '', int $maxAttempts = 60, $decayMinutes = 1)
    {
        $this->key = $key;
        $this->maxAttempts = $maxAttempts;
        $this->decayMinutes = $decayMinutes;
    }

    /**
     * Create a new rate limit using seconds as decay time.
     *
     * @param  int  $decaySeconds
     * @param  int  $maxAttempts
     * @return static
     */
    public static function perSeconds($decaySeconds, $maxAttempts)
    {
        return new static('', $maxAttempts, $decaySeconds/60.0);
    }
   
}

You can redefine the Limit class

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!