Disable rate limiter in Laravel?

PhpLaravel

Php Problem Overview


Is there a way to disable rate limiting on every/individual routes in Laravel?

I'm trying to test an endpoint that receives a lot of requests, but randomly Laravel will start responding with { status: 429, responseText: 'Too Many Attempts.' } for a few hundred requests which makes testing a huge pain.

Php Solutions


Solution 1 - Php

In app/Http/Kernel.php Laravel has a default throttle limit for all api routes.

protected $middlewareGroups = [
    ...
    'api' => [
        'throttle:60,1',
    ],
];

Comment or increase it.

Solution 2 - Php

You can actually disable only a certain middleware in tests.

use Illuminate\Routing\Middleware\ThrottleRequests;

class YourTest extends TestCase 
{
 
    protected function setUp()
    {
        parent::setUp();
        $this->withoutMiddleware(
            ThrottleRequests::class
        );
    }
    ...
}

Solution 3 - Php

Assuming you are using the API routes then you can change the throttle in app/Http/Kernel.php or take it off entirely. If you need to throttle for the other routes you can register the middleware for them separately.

(example below: throttle - 60 attempts then locked out for 1 minute)

'api' => [
        'throttle:60,1',
        'bindings',
    ],

Solution 4 - Php

You can use cache:clear command to clear your cache including your rate limits, like so:

php artisan cache:clear

Solution 5 - Php

In Laravel 5.7

Dynamic Rate Limiting You may specify a dynamic request maximum based on an attribute of the authenticated User model. For example, if your User model contains a rate_limit attribute, you may pass the name of the attribute to the throttle middleware so that it is used to calculate the maximum request count:

Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});

https://laravel.com/docs/5.7/routing#rate-limiting

Solution 6 - Php

If you want to disable just for automated tests, you can use the WithoutMiddleware trait on your tests.

use Illuminate\Foundation\Testing\WithoutMiddleware;

class YourTest extends TestCase {
    use WithoutMiddleware;

    ...

Otherwise, just remove the 'throttle:60,1', line from your Kernel file (app/Http/Kernel.php), and your problem will be solved.

Solution 7 - Php

You can add the following lines in your app/Http/Kernel.php

    'api' => [
        'throttle:120,1',
        'bindings',
         \App\Library\Cobalt\Http\Middleware\LogMiddleware::class,
    ],

If the problem persists try the artisan command php artisan cache:clear

Solution 8 - Php

If you are using Laravel 8.x or later you can use RateLimiter with the following steps:

In your app/Providers/RouteServiceProvider.php find below configureRateLimiting: protected function configureRateLimiting() {

    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
    });

    // no limit throttle
    RateLimiter::for('none', function (Request $request) {
        return Limit::none();
    });

}

In your app/web.php add 'throttle:none:

Route::group(['middleware' => ['auth', 'throttle:none']], function ($router) {
    Route::post('test', 'TestController@test');
});

Solution 9 - Php

A non-hacky way to increase the throttle in unit tests to avoid the dreaded 429:

  1. Remove the throttle:60,1 from the kernel file middleware.
  2. Add the throttle middleware back in to the route group, using an environment variable instead:
$requestsPerMinute = ENV("REQUESTS_PER_MINUTE", 60);
Route::middleware(["auth:api", "throttle:$requestsPerMinute,1"])->group(function(){
  // your routes
});
  1. Define the REQUESTS_PER_MINUTE environment variable much higher in phpunit.xml, to allow more requests in test environment before throttling.
<server name="REQUESTS_PER_MINUTE" value="500"/>
  1. (Also define the new REQUESTS_PER_MINUTE var in .env, even though it'll fall back to 60).

Solution 10 - Php

In my case, I just changed the default value of perMinute() in `App\Providers\RouteServiceProvider.

protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            $perMinute = env('APP_ENV') === 'testing' ? 1000 : 60;

            return Limit::perMinute($perMinute)
              ->by(optional($request->user())->id ?: $request->ip());
        });
    }

Solution 11 - Php

You can remove or comment out the line (throttle:60,1) from App\Http\Kernel.php.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimpleJView Question on Stackoverflow
Solution 1 - PhpEddyTheDoveView Answer on Stackoverflow
Solution 2 - PhpMostafa BahriView Answer on Stackoverflow
Solution 3 - Phpcyclops1101View Answer on Stackoverflow
Solution 4 - PhpPetar VasilevView Answer on Stackoverflow
Solution 5 - Php120DEVView Answer on Stackoverflow
Solution 6 - PhpElias SoaresView Answer on Stackoverflow
Solution 7 - PhpNIKHIL NEDIYODATHView Answer on Stackoverflow
Solution 8 - PhpsvikramjeetView Answer on Stackoverflow
Solution 9 - PhpRhysView Answer on Stackoverflow
Solution 10 - PhparielkollrossView Answer on Stackoverflow
Solution 11 - PhpAl Amin ProdhanView Answer on Stackoverflow