Skip to content

WIP : Create Page Rate Limiter Component #14266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions rate_limiter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Rate Limiter Component
======================

The Rate Limiter component provides a Token Bucket implementation to rate limit input and output in your application.

Installation
------------

In applications using :ref:`Symfony Flex <symfony-flex>`, run this command to
install the ``profiler`` :ref:`Symfony pack <symfony-packs>` before using it:

.. code-block:: terminal

$ composer require symfony/rate-limiter


Using Rate Limiter Component
----------------------------

.. code-block:: php

use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Limiter;

$limiter = new Limiter([
'id' => 'login',
'strategy' => 'token_bucket', // or 'fixed_window'
'limit' => 10,
'rate' => ['interval' => '15 minutes'],
], new InMemoryStorage());

// blocks until 1 token is free to use for this process
$limiter->reserve(1)->wait();
// ... execute the code

// only claims 1 token if it's free at this moment (useful if you plan to skip this process)
if ($limiter->consume(1)) {
// ... execute the code
}