dev-master
9999999-dev https://github.com/romeritoCL/TrafficLimitBundleA Redis based traffic handler for max request limit in Symfony 2
MIT
The Requires
The Development Requires
by César Romero
redis symfony request limit
Wallogit.com
2017 © Pedro Peláez
A Redis based traffic handler for max request limit in Symfony 2
Limit the amount of request to your application based on a defined key, (*1)
It uses SNC\RedisBundle to connect to redis. You can create as many traffic limit services as you require, (*2)
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*3)
$ composer require devoralive/traffic-limit-bundle "dev-master"
This will also install snc\RedisBundle if you did not have it installed, (*4)
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*5)
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:, (*6)
<?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
);
// ...
}
// ...
}
To use this bundle you need to add configuration into the config.yml file inside you app/config directory, (*7)
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, (*8)
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:, (*9)
<?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., (*10)
If the limit of requests is reached the method "processRequest" will return a exception., (*11)
A Redis based traffic handler for max request limit in Symfony 2
MIT
redis symfony request limit