Download the PHP package instasent/ratelimit-bundle without Composer
On this page you can find all versions of the php package instasent/ratelimit-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download instasent/ratelimit-bundle
More information about instasent/ratelimit-bundle
Files in instasent/ratelimit-bundle
Package ratelimit-bundle
Short Description This bundle provides functionality to limit calls to actions based on rate limits
License MIT
Informations about the package ratelimit-bundle
InstasentRateLimitBundle
This bundle provides enables the @RateLimit
annotation which allows you to limit the number of connections to actions.
This is mostly useful in APIs.
The bundle is prepared to work by default in cooperation with the FOSOAuthServerBundle
. It contains a listener that adds the OAuth token to the cache-key. However, you can create your own key generator to allow custom rate limiting based on the request. See Create a custom key generator below.
This bundle is partially inspired by a GitHub gist from Ruud Kamphuis: https://gist.github.com/ruudk/3350405
Features
- Simple usage through annotations
- Customize rates per controller, action and even per HTTP method
- Multiple storage backends: Redis, Memcached and Doctrine cache
Installation
Installation takes just few easy steps:
Step 1: Add the bundle to your composer.json
If you're not yet familiar with Composer see http://getcomposer.org. Add the InstasentRateLimitBundle in your composer.json:
Now tell composer to download the bundle by running the command:
Step 2: Enable the bundle
Enable the bundle in the kernel:
Step 3: Install a storage engine
Redis
If you want to use Redis as your storage engine, you will need to install SncRedisBundle
:
Memcache
If you want to use Memcache, you need to install LswMemcacheBundle
Doctrine cache
If you want to use Doctrine cache as your storage engine, you will need to install DoctrineCacheBundle
:
Referer to their documentations for more details. You can change your storage engine with the storage_engine
configuration parameter. See Configuration reference.
Configuration
Enable bundle only in production
If you wish to enable the bundle only in production environment (so you can test without worrying about limit in your development environments), you can use the enabled
configuration setting to enable/disable the bundle completely. It's enabled by default:
Configuration reference
This is the default bundle configuration:
Usage
Simple rate limiting
To enable rate limiting, you only need to add the annotation to the docblock of the specified action
Limit per method
It's possible to rate-limit specific HTTP methods as well. This can be either a string or an array of methods. When no method argument is given, all other methods not defined are rated. This allows to add a default rate limit if needed.
Limit per controller
It's also possible to add rate-limits to a controller class instead of a single action. This will act as a default rate limit for all actions, except the ones that actually defines a custom rate-limit.
Create a custom key generator
If you need to create a custom key generator, you need to register a listener to listen to the ratelimit.generate.key
event:
Make sure to generate a key based on what is rate limited in your controllers.
Set custom period or limit
If you need to create a custom period/limit based for example on client settings saved in database, you need to register a listener to listen to the ratelimit.pre.create
event:
Set your own limit value
Throwing exceptions
Instead of returning a Response object when a rate limit has exceeded, it's also possible to throw an exception. This
allows you to easily handle the rate limit on another level, for instance by capturing the kernel.exception
event.
Running tests
If you want to run the tests use: