2017 © Pedro Peláez
 

symfony-bundle ratelimit-bundle

This bundle provides functionality to limit calls to actions based on rate limits

image

noxlogic/ratelimit-bundle

This bundle provides functionality to limit calls to actions based on rate limits

  • Monday, July 2, 2018
  • by JayTaph
  • Repository
  • 14 Watchers
  • 227 Stars
  • 326,072 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 48 Forks
  • 9 Open issues
  • 21 Versions
  • 8 % Grown

The README.md

NoxlogicRateLimitBundle

Build Status Code Coverage Scrutinizer Code Quality, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

This bundle provides enables the #[RateLimit()] attribute which allows you to limit the number of connections to actions. This is mostly useful in APIs., (*3)

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

This bundle is partially inspired by a GitHub gist from Ruud Kamphuis: https://gist.github.com/ruudk/3350405, (*5)

Features

  • Simple usage through attributes
  • 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:, (*6)

Step 1: Install the bundle using composer

If you're not yet familiar with Composer see http://getcomposer.org. Tell composer to download the bundle by running the command:, (*7)

``` bash composer require noxlogic/ratelimit-bundle, (*8)


### Step 2: Enable the bundle If you are using `symfony/flex` you can skip this step, the bundle will be enabled automatically, otherwise you need to enable the bundle by adding it to the `bundles.php` file of your project. ``` php <?php // bundles.php return [ // .. Noxlogic\RateLimitBundle\NoxlogicRateLimitBundle::class => ['all' => true], // .. ];

Step 3: Install a storage engine

Redis

If you want to use Redis as your storage engine, you might want to install SncRedisBundle:, (*9)

  • https://github.com/snc/SncRedisBundle

Memcache

If you want to use Memcache, you might want to install LswMemcacheBundle, (*10)

  • https://github.com/LeaseWeb/LswMemcacheBundle

Doctrine cache

If you want to use Doctrine cache as your storage engine, you might want to install DoctrineCacheBundle:, (*11)

  • https://github.com/doctrine/DoctrineCacheBundle

Referer to their documentations for more details. You can change your storage engine with the storage_engine configuration parameter. See Configuration reference., (*12)

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

# config_dev.yml
noxlogic_rate_limit:
    enabled: false

Configuration reference

This is the default bundle configuration:, (*14)

noxlogic_rate_limit:
    enabled:              true

    # The storage engine where all the rates will be stored
    storage_engine:       ~ # One of "redis"; "memcache"; "doctrine"; "php_redis"; "php_redis_cluster"

    # The redis client to use for the redis storage engine
    redis_client:         default_client

    # The Redis service, use this if you dont use SncRedisBundle and want to specify a service to use
    # Should be instance of \Predis\Client
    redis_service:    null # Example: project.predis

    # The Redis client to use for the php_redis storage engine
    # Depending on storage_engine an instance of \Redis or \RedisCluster
    php_redis_service:    null # Example: project.redis

    # The memcache client to use for the memcache storage engine
    memcache_client:      default

    # The Memcached service, use this if you dont use LswMemcacheBundle and want to specify a service to use
    # Should be instance of \Memcached
    memcache_service:    null # Example: project.memcached

    # The Doctrine Cache provider to use for the doctrine storage engine
    doctrine_provider:    null # Example: my_apc_cache

    # The Doctrine Cache service, use this if you dont use DoctrineCacheBundle and want to specify a service to use
    # Should be an instance of \Doctrine\Common\Cache\Cache
    doctrine_service:    null # Example: project.my_apc_cache

    # The HTTP status code to return when a client hits the rate limit
    rate_response_code:   429

    # Optional exception class that will be returned when a client hits the rate limit
    rate_response_exception:  null

    # The HTTP message to return when a client hits the rate limit
    rate_response_message:  'You exceeded the rate limit'

    # Should the ratelimit headers be automatically added to the response?
    display_headers:      true

    # What are the different header names to add
    headers:
        limit:                X-RateLimit-Limit
        remaining:            X-RateLimit-Remaining
        reset:                X-RateLimit-Reset

    # Rate limits for paths
    path_limits:
        path:                 ~ # Required
        methods:

            # Default:
            - *
        limit:                ~ # Required
        period:               ~ # Required

    # - { path: /api, limit: 1000, period: 3600 }
    # - { path: /dashboard, limit: 100, period: 3600, methods: ['GET', 'POST']}

    # Should the FOS OAuthServerBundle listener be enabled 
    fos_oauth_key_listener: true

Usage

Simple rate limiting

To enable rate limiting, you only need to add the attribute to the specified action, (*15)

<?php

use Noxlogic\RateLimitBundle\Attribute\RateLimit;
use Symfony\Component\Routing\Annotation\Route;

#[Route(...)]
#[RateLimit(limit: 1000, period: 3600)]
public function someApiAction()
{
}

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

<?php

use Noxlogic\RateLimitBundle\Attribute\RateLimit;
use Symfony\Component\Routing\Annotation\Route;

#[Route(...)]
#[RateLimit(methods: ["PUT", "POST"], limit: 1000, period: 3600)]
#[RateLimit(methods: ["GET"], limit: 1000, period: 3600)]
#[RateLimit(limit: 5000, period: 3600)]
public function someApiAction()
{
}

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

<?php

use Noxlogic\RateLimitBundle\Attribute\RateLimit;
use Symfony\Component\Routing\Annotation\Route;

#[RateLimit(methods: ["POST"], limit: 100, period: 10)] // 100 POST requests per 10 seconds
class DefaultController extends Controller
{
    #[RateLimit(methods: ["POST"], limit: 200, period: 10)] // 200 POST requests to indexAction allowed.
    public function indexAction()
    {
    }
}

Create a custom key generator

NOTE

Note that this bundle by default does not perform rate-limiting based on user's IP. If you wish to enable IP-based rate limiting or any other strategy, custom key generators are the way to go., (*18)

If you need to create a custom key generator, you need to register a listener to listen to the ratelimit.generate.key event:, (*19)

services:
    mybundle.listener.rate_limit_generate_key:
        class: MyBundle\Listener\RateLimitGenerateKeyListener
        tags:
            - { name: kernel.event_listener, event: 'ratelimit.generate.key', method: 'onGenerateKey' }
<?php

namespace MyBundle\Listener;

use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;

class RateLimitGenerateKeyListener
{
    public function onGenerateKey(GenerateKeyEvent $event)
    {
        $key = $this->generateKey();

        $event->addToKey($key);
        // $event->setKey($key); // to overwrite key completely
    }
}

Make sure to generate a key based on what is rate limited in your controllers., (*20)

And example of a IP-based key generator can be:, (*21)

<?php

namespace MyBundle\Listener;

use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;

class IpBasedRateLimitGenerateKeyListener
{
    public function onGenerateKey(GenerateKeyEvent $event)
    {
        $request = $event->getRequest();
        $event->addToKey($request->getClientIp());
    }
}

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

Running tests

If you want to run the tests use:, (*23)

./vendor/bin/simple-phpunit

The Versions

02/07 2018

1.11.1

1.11.1.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

02/07 2018

dev-master

9999999-dev

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

29/06 2018

1.10.1

1.10.1.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

29/06 2018

1.11.0

1.11.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

27/06 2018

1.10.0

1.10.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

27/06 2018

1.9.1

1.9.1.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

27/06 2018

1.9.0

1.9.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

04/06 2018

1.8.2

1.8.2.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

25/05 2018

1.8.1

1.8.1.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

24/05 2018

1.8.0

1.8.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

24/05 2018

1.x-dev

1.9999999.9999999.9999999-dev

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

24/05 2018

dev-symfony-4

dev-symfony-4

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

29/03 2016

1.7.0

1.7.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

03/01 2016

1.6.0

1.6.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

10/04 2015

1.5.0

1.5.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

10/12 2014

1.4

1.4.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

17/11 2014

1.3

1.3.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

21/07 2014

1.2

1.2.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

08/07 2014

1.1

1.1.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joshua Thijssen

api rest x-rate-limit

23/06 2014

1.0.1

1.0.1.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

by Joshua Thijssen

api rest x-rate-limit

23/06 2014

1.0.0

1.0.0.0

This bundle provides functionality to limit calls to actions based on rate limits

  Sources   Download

MIT

The Requires

 

by Joshua Thijssen

api rest x-rate-limit