Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[RateLimiter] compound rate limiter#20870

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes fromall 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
117 changes: 117 additions & 0 deletionsrate_limiter.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -541,6 +541,123 @@
lock factory (``lock.factory``) failed if the Symfony Lock component was not
installed in the application.

Compound Rate Limiter
---------------------

.. versionadded:: 7.3

Configuring compound rate limiters was added in 7.3.

You can configure multiple rate limiters to work together:

.. configuration-block::

.. code-block:: yaml

# config/packages/rate_limiter.yaml
framework:
rate_limiter:
two_per_minute:
policy: 'fixed_window'
limit: 2
interval: '1 minute'
five_per_hour:
policy: 'fixed_window'
limit: 5
interval: '1 hour'
contact_form:
policy: 'compound'
limiters: [two_per_minute, five_per_hour]

.. code-block:: xml

<!-- config/packages/rate_limiter.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:rate-limiter>
<framework:limiter name="two_per_minute"
policy="fixed_window"
limit="2"
interval="1 minute"
/>

<framework:limiter name="five_per_hour"
policy="fixed_window"
limit="5"
interval="1 hour"
/>

<framework:limiter name="contact_form"
policy="compound"
>
<limiter>two_per_minute</limiter>
<limiter>five_per_hour</limiter>
</framework:limiter>
</framework:rate-limiter>
</framework:config>
</container>

.. code-block:: php

// config/packages/rate_limiter.php
use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework): void {
$framework->rateLimiter()
->limiter('two_per_minute')
->policy('fixed_window')
->limit(2)
->interval('1 minute')
;

$framework->rateLimiter()
->limiter('two_per_minute')
->policy('fixed_window')
->limit(5)
->interval('1 hour')
;

$framework->rateLimiter()
->limiter('contact_form')
->policy('compound')
->limiters(['two_per_minute', 'five_per_hour'])
;
};

Check failure on line 633 in rate_limiter.rst

View workflow job for this annotation

GitHub Actions/ Code Blocks

[Cache Warmup] In FrameworkExtension.php line 3300: Compound rate limiter "contact_form" requires at least one sub-limiter to b e configured.

Then, inject and use as normal::

// src/Controller/ContactController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\RateLimiter\RateLimiterFactory;

class ContactController extends AbstractController
{
public function registerUser(Request $request, RateLimiterFactoryInterface $contactFormLimiter): Response
{
$limiter = $contactFormLimiter->create($request->getClientIp());

if (false === $limiter->consume(1)->isAccepted()) {
// either of the two limiters has been reached
}

// ...
}

// ...
}

.. _`DoS attacks`: https://cheatsheetseries.owasp.org/cheatsheets/Denial_of_Service_Cheat_Sheet.html
.. _`Apache mod_ratelimit`: https://httpd.apache.org/docs/current/mod/mod_ratelimit.html
.. _`NGINX rate limiting`: https://www.nginx.com/blog/rate-limiting-nginx/
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp