Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/instasent/ratelimitbundle

Rate limit bundle
https://github.com/instasent/ratelimitbundle

Last synced: 9 days ago
JSON representation

Rate limit bundle

Awesome Lists containing this project

README

        

InstasentRateLimitBundle
========================

[![Build Status](https://travis-ci.org/jaytaph/RateLimitBundle.svg?branch=master)](https://travis-ci.org/jaytaph/RateLimitBundle)
[![Code Coverage](https://scrutinizer-ci.com/g/jaytaph/RateLimitBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/jaytaph/RateLimitBundle/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jaytaph/RateLimitBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jaytaph/RateLimitBundle/?branch=master)

[![Latest Stable Version](https://poser.pugx.org/Instasent/ratelimit-bundle/v/stable.svg)](https://packagist.org/packages/Instasent/ratelimit-bundle) [![Total Downloads](https://poser.pugx.org/Instasent/ratelimit-bundle/downloads.svg)](https://packagist.org/packages/Instasent/ratelimit-bundle) [![Latest Unstable Version](https://poser.pugx.org/Instasent/ratelimit-bundle/v/unstable.svg)](https://packagist.org/packages/Instasent/ratelimit-bundle) [![License](https://poser.pugx.org/Instasent/ratelimit-bundle/license.svg)](https://packagist.org/packages/Instasent/ratelimit-bundle)

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:

```js
{
"require": {
"Instasent/ratelimit-bundle": "1.x"
}
}
```

Now tell composer to download the bundle by running the command:

``` bash
php composer.phar update Instasent/ratelimit-bundle
```

### Step 2: Enable the bundle

Enable the bundle in the kernel:

``` php
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.

## 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:

```yaml
services:
mybundle.listener.rate_limit_pre_create:
class: MyBundle\Listener\RateLimitFromDatabaseListener
tags:
- { name: kernel.event_listener, event: 'ratelimit.pre.create', method: 'findUserLimit' }
```

```php
setLimit($value);
}
}
```

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:

```
./vendor/bin/phpunit ./Tests
```