Skip to content

Commit

Permalink
feature #53091 [FrameworkBundle][RateLimiter] add rate_limiter tag …
Browse files Browse the repository at this point in the history
…to rate limiter services (kbond)

This PR was merged into the 7.1 branch.

Discussion
----------

[FrameworkBundle][RateLimiter] add `rate_limiter` tag to rate limiter services

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | Fix #52516
| License       | MIT

This will allow creating tagged iterators/locators containing all registered rate limiters (indexed by their name).

Commits
-------

8706f84 [RateLimiter][FrameworkBundle] add `rate_limiter` tag to rate limiter services
  • Loading branch information
fabpot committed Dec 19, 2023
2 parents 1d68ce9 + 8706f84 commit 685e034
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Move the Router `cache_dir` to `kernel.build_dir`
* Deprecate the `router.cache_dir` config option
* Add `rate_limiter` tags to rate limiter services

7.0
---
Expand Down
Expand Up @@ -2861,7 +2861,8 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
// default configuration (when used by other DI extensions)
$limiterConfig += ['lock_factory' => 'lock.factory', 'cache_pool' => 'cache.rate_limiter'];

$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'));
$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'))
->addTag('rate_limiter', ['name' => $name]);

if (null !== $limiterConfig['lock_factory']) {
if (!interface_exists(LockInterface::class)) {
Expand Down
Expand Up @@ -245,4 +245,24 @@ public function testRateLimiterLockFactory()

$container->getDefinition('limiter.without_lock')->getArgument(2);
}

public function testRateLimiterIsTagged()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'lock' => true,
'rate_limiter' => [
'first' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
'second' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
],
]);
});

$this->assertSame('first', $container->getDefinition('limiter.first')->getTag('rate_limiter')[0]['name']);
$this->assertSame('second', $container->getDefinition('limiter.second')->getTag('rate_limiter')[0]['name']);
}
}

0 comments on commit 685e034

Please sign in to comment.