PHPackages                             damirius/rate-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. damirius/rate-limiter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

damirius/rate-limiter
=====================

Rate limiter Symfony bundle based on the token bucket algorithm.

1.0.0(6y ago)15MITPHPPHP &gt;=7.1.3

Since Nov 25Pushed 6y ago2 watchersCompare

[ Source](https://github.com/damirius/rate-limiter)[ Packagist](https://packagist.org/packages/damirius/rate-limiter)[ Docs](https://github.com/damirius/rate-limiter/)[ RSS](/packages/damirius-rate-limiter/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Rate Limiter Symfony Bundle
===========================

[](#rate-limiter-symfony-bundle)

This bundle lets you Rate Limit specific requests. While main usage is for limiting end user access to API endpoints, this bundle gives you access to the service which can limit access to any part of the code. It can be used in controllers but also any other services if needed. While this service gives you tools to see if given limit was reached it does not provide further logic. Consumer will have to decide what to do and implement necessary functionality.

Installation
============

[](#installation)

1. Download damirius/rate-limiter using composer
2. Enable the Bundle

### Step 1: Download damirius/rate-limiter using composer

[](#step-1-download-damiriusrate-limiter-using-composer)

Require the bundle with composer inside your symfony project: `$ composer require damirius/rate-limiter "~1.0"`

Composer will install the bundle to your project's `vendor/damirius/rate-limiter` directory.

### Step 2: Enable the bundle

[](#step-2-enable-the-bundle)

Enable the bundle in the `config/bundles.php`

```
// config/bundles.php
return [
    // ...
    Damirius\RateLimiter\DamiriusRateLimiterBundle::class => ['all' => true],
];
```

Configuration
=============

[](#configuration)

To enable Rate Limiting for specific request, new service should be registered. To create a service simply add new configuration options for rate limit bundle in new `config/rate_limiter.yml` file:

```
# config/rate_limiter.yml
damirius_rate_limiter:
    domains:
        default: # name of the domain
            limit: 10 # Request limit
            period: 60 # Request time window in seconds
            storage_service: App\Service\RateLimiterStorage\YourStorageService # Storage service
```

`limit` (`int`, default: `10`).

`period` (`int`, default: `60`).

`storage_service` (service of `RateLimiterStorageInterface` type).

If you want to have different limits for different requests/parts of your code you can register multiple services with a different domains. Different domains don't share their limits between them.

- Note: Domains are unique per storage! Using the same domain with a different storage will behave like a different domain.

If default configuration example was used new limiter service will be available in the service container: `damirius_rate_limiter.limiter.default`.

Since you can have multiple services of the same class we can't rely on type-hinting injection. Instead in your `config/services.yaml` you can either make a default alias.

```
# config/services.yaml
# ...
Damirius\RateLimiter\Service\RateLimiter: '@damirius_rate_limiter.limiter.default'
```

Then use different one when needed like this.

```
# config/services.yaml
# ...
#our other custom app service
App\Service\Custom
    arguments:
        $rateLimiter: '@damirius_rate_limiter.limited.specialdomain'
```

Or you can use quite similar argument binding by name or type.

```
# config/services.yaml
services:
    _defaults:
        bind:
            Damirius\RateLimiter\Service\RateLimiter: '@damirius_rate_limiter.limiter.default'
# ...
```

Then override specific bind for our custom service or even group of services (i.e controllers).

```
# config/services.yaml
services:
    # _defaults config as before
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
        bind:
            Damirius\RateLimiter\Service\RateLimiter: '@damirius_rate_limiter.limiter.controller'
# ...
```

For more information about customizing service container and configuration check Service Container link under References.

**Name will always be `damirius_rate_limiter.limiter.DOMAINNAME` where `DOMAINNAME` is the name of the node in the configuration.**

Usage
=====

[](#usage)

Usage is simple, we can call `RateLimiter::checkAndReturn($identifier)`, where only parameter we need to send is unique identifier. This can be IP address if we want to limit access by IP, but it can also be anything else like username or some different string.

Method will make necessary steps to store our current call and return number of remaining calls in our window. If that number is 0 it means that this client hit the rate limit, consumer can then limit further access if needed.

```
