From ae4095d7acc24ff3748f04d21a355ca0a7ed529c Mon Sep 17 00:00:00 2001 From: gary houbre Date: Tue, 22 Sep 2020 13:56:03 +0200 Subject: [PATCH 1/3] Create Page Rate Limiter Component --- rate_limiter.rst | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 rate_limiter.rst diff --git a/rate_limiter.rst b/rate_limiter.rst new file mode 100644 index 00000000000..cb8fa318012 --- /dev/null +++ b/rate_limiter.rst @@ -0,0 +1,44 @@ +.. index:: + single: Rate limiter + +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 `, run this command to +install the ``profiler`` :ref:`Symfony pack ` 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 + } + + From fe8001c332cbf5511cc3cda2408d3c6b55e687d0 Mon Sep 17 00:00:00 2001 From: gary houbre Date: Tue, 22 Sep 2020 14:01:06 +0200 Subject: [PATCH 2/3] Fix Error --- rate_limiter.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rate_limiter.rst b/rate_limiter.rst index cb8fa318012..c2e8ea9144c 100644 --- a/rate_limiter.rst +++ b/rate_limiter.rst @@ -2,7 +2,7 @@ single: Rate limiter Rate Limiter Component -======== +====================== The Rate Limiter component provides a Token Bucket implementation to rate limit input and output in your application. From fd85c406d572bab0731a849cea45689b9cc63eb0 Mon Sep 17 00:00:00 2001 From: gary houbre Date: Tue, 22 Sep 2020 14:04:06 +0200 Subject: [PATCH 3/3] Fix Error --- rate_limiter.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/rate_limiter.rst b/rate_limiter.rst index c2e8ea9144c..42089ebdf33 100644 --- a/rate_limiter.rst +++ b/rate_limiter.rst @@ -1,6 +1,3 @@ -.. index:: - single: Rate limiter - Rate Limiter Component ======================