Download the PHP package bvtterfly/sliding-window-rate-limiter without Composer
On this page you can find all versions of the php package bvtterfly/sliding-window-rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bvtterfly/sliding-window-rate-limiter
More information about bvtterfly/sliding-window-rate-limiter
Files in bvtterfly/sliding-window-rate-limiter
Package sliding-window-rate-limiter
Short Description a sliding window rate limiter for laravel
License MIT
Homepage https://github.com/bvtterfly/sliding-window-rate-limiter
Informations about the package sliding-window-rate-limiter
🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
I no longer use Laravel and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.
Laravel Sliding Window Rate Limiter
This package provides an easy way to limit any action during a specified time window. You may be familiar with Laravel's Rate Limiter, It has a similar API, but it uses the Sliding Window algorithm and requires Redis.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
The package relies on Redis and requires a Redis connection, and you choose which Redis connection to use.
Usage
The Bvtterfly\SlidingWindowRateLimiter\Facades\SlidingWindowRateLimiter
facade may be used to interact with the rate limiter.
The simplest method offered by the rate limiter is the attempt
method, which rate limits an action for a given number of seconds.
The attempt
method returns a result object that specifies if an attempt was successful and how many attempts remain. If the attempt is unsuccessful, you can get the number of seconds until the action is available again.
You can call the following methods on the SlidingWindowRateLimiter
:
tooManyAttempts
attempts
resetAttempts
remaining
clear
availableIn
retriesLeft
Route Rate Limiting
This package comes with a throttle
middleware for Route Rate Limiting. It can replace the default Laravel's throttle
middleware to use this package rate limiter. The only difference is it tries to get a named rate limiter from the SlidingWindowRateLimiter
or, as a fallback, it will take them from Laravel RateLimiter.
You may wish to change the mapping of throttle
middleware in your application's HTTP kernel(App\Http\Kernel
) to use \Bvtterfly\SlidingWindowRateLimiter\Http\Middleware\ThrottleRequests
class.
Rate Limiters must be configured for route rate-limiting to work. Laravel Rate Limiter comes with a RateLimiting class(Illuminate\Cache\RateLimiting\Limit
) that works in a minutes-based system. But This package is designed to allow rate limit actions in a seconds-based system, so it comes with its rate limiters classes and lets you configure rate limiters for less than a minute. Still, for ease of usage of this package, It supports default Laravel's Rate Limiters.
Defining Rate Limiters
SlidingWindowRateLimiter
rate limiters are heavily based on Laravel's rate limiters. It only differs in the fact that it is seconds-based. So, before getting started, be sure to read about them on Laravel docs.
Limit configurations are instances of the Bvtterfly\SlidingWindowRateLimiter\RateLimiting\Limit
class, and It contains helpful "builder" methods to define your rate limits quickly. The rate limiter name may be any string you wish.
For limiting to 500 requests in 45 seconds:
If the incoming request exceeds the specified rate limit, a response with a 429 HTTP status code will automatically be returned by Laravel. If you would like to define your response that a rate limit should return, you may use the response
method:
You can have multiple rate limits. This configuration will limit only 100 requests per 30 seconds and 1000 requests per day:
Incoming HTTP request instances are passed to rate limiter callbacks, and the rate limit may be calculated dynamically depending on the user or request:
There may be times when you wish to segment rate limits by some arbitrary value. For example, you may want to allow users to access a given route with 100 requests per minute per authenticated user ID and 10 requests per minute per IP address for guests. Using the by
a method, you can create your rate limit as follows:
Attaching Rate Limiters To Routes
Rate limiters can be attached to routes or route groups using the throttle
middleware. The throttle
middleware accepts the name of the rate limiter you wish to assign to the route:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Ari
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of sliding-window-rate-limiter with dependencies
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^9.0