devoralive / traffic-limit-bundle
A Redis based traffic handler for max request limit in Symfony 2
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.4
- snc/redis-bundle: 2.0.0
- symfony/framework-bundle: ^2.7 || ^3.0
- symfony/yaml: ^2.7 || ^3.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
- predis/predis: ^1.0
- symfony/phpunit-bridge: ^2.7 || ^3.0
This package is auto-updated.
Last update: 2024-10-29 04:36:36 UTC
README
Limit the amount of request to your application based on a defined key
It uses SNC\RedisBundle to connect to redis. You can create as many traffic limit services as you require
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require devoralive/traffic-limit-bundle "dev-master"
This will also install snc\RedisBundle if you did not have it installed
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Snc\RedisBundle\SncRedisBundle(), //Mandatory of using this bundle new Devoralive\TrafficLimit\TrafficLimitBundle(), //Include the bundle ); // ... } // ... }
Step 3: Configure the bundle
To use this bundle you need to add configuration into the config.yml file inside you app/config directory
We supose you al ready included at least one connection to redis from the sncRedisBundle If you need more information have a look at SncRedisBundle configuration
snc_redis: clients: default: type: phpredis alias: default dsn: redis://localhost:6379 traffic_limit: type: phpredis alias: default dsn: redis://localhost:6379 traffic_limit: low_limit: enabled: true snc_client: traffic_limit amount: 600 ttl: 60 high_limit: enabled: true snc_client: default amount: 6000 ttl: 60
As you can see you can define as many traffic limit services as you desire. You can have infinite configurations. All are available in the container:
<?php use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; namespace AppBundle\Controller; /** * Class ApiController * * @package AppBundle\Controller */ class ApiController { /** * Example on how to limit requests by IP * * @param string $ip * * @return string * * @throws TooManyRequestsHttpException $e */ public function getLocationAction(Request $request, $ip) { try { $this->get('traffic_limit.low_limit')->processRequest( $request->getClientIp() ); //do some stuff... return new JsonResponse(''); } catch (TooManyRequestsHttpException $e) { //handle exception or throw it } } }
You can define the key, so you can limit the request by IP, userId, or anything you can identify as a variable.
If the limit of requests is reached the method "processRequest" will return a exception.