2017 © Pedro Peláez
 

symfony-bundle traffic-limit-bundle

A Redis based traffic handler for max request limit in Symfony 2

image

devoralive/traffic-limit-bundle

A Redis based traffic handler for max request limit in Symfony 2

  • Friday, October 28, 2016
  • by romeritoCL
  • Repository
  • 0 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

TrafficLimitBundle

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)

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:, (*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)

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:, (*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
        );

        // ...
    }

    // ...
}

Step 3: Configure 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)

The Versions

28/10 2016

dev-master

9999999-dev https://github.com/romeritoCL/TrafficLimitBundle

A Redis based traffic handler for max request limit in Symfony 2

  Sources   Download

MIT

The Requires

 

The Development Requires

by César Romero

redis symfony request limit