Skip to content

Commit d4d2302

Browse files
committed
[FrameworkBundle][RateLimiter] default lock_factory to auto
1 parent 03f7ed2 commit d4d2302

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CHANGELOG
2222
* Enable service argument resolution on classes that use the `#[Route]` attribute,
2323
the `#[AsController]` attribute is no longer required
2424
* Deprecate setting the `framework.profiler.collect_serializer_data` config option to `false`
25+
* Set `framework.rate_limiter.limiters.*.lock_factory` to `auto` by default
2526

2627
7.2
2728
---

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $
25042504
->children()
25052505
->scalarNode('lock_factory')
25062506
->info('The service ID of the lock factory used by this limiter (or null to disable locking).')
2507-
->defaultValue('lock.factory')
2507+
->defaultValue('auto')
25082508
->end()
25092509
->scalarNode('cache_pool')
25102510
->info('The cache pool to use for storing the current limiter state.')

DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,10 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
32393239
$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'))
32403240
->addTag('rate_limiter', ['name' => $name]);
32413241

3242+
if ('auto' === $limiterConfig['lock_factory']) {
3243+
$limiterConfig['lock_factory'] = $this->isInitializedConfigEnabled('lock') ? 'lock.factory' : null;
3244+
}
3245+
32423246
if (null !== $limiterConfig['lock_factory']) {
32433247
if (!interface_exists(LockInterface::class)) {
32443248
throw new LogicException(\sprintf('Rate limiter "%s" requires the Lock component to be installed. Try running "composer require symfony/lock".', $name));

Tests/DependencyInjection/PhpFrameworkExtensionTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function testWorkflowDefaultMarkingStoreDefinition()
188188
$this->assertNull($argumentsB['index_1'], 'workflow_b marking_store argument is null');
189189
}
190190

191-
public function testRateLimiterWithLockFactory()
191+
public function testRateLimiterLockFactoryWithLockDisabled()
192192
{
193193
try {
194194
$this->createContainerFromClosure(function (ContainerBuilder $container) {
@@ -199,7 +199,7 @@ public function testRateLimiterWithLockFactory()
199199
'php_errors' => ['log' => true],
200200
'lock' => false,
201201
'rate_limiter' => [
202-
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
202+
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour', 'lock_factory' => 'lock.factory'],
203203
],
204204
]);
205205
});
@@ -208,7 +208,10 @@ public function testRateLimiterWithLockFactory()
208208
} catch (LogicException $e) {
209209
$this->assertEquals('Rate limiter "with_lock" requires the Lock component to be configured.', $e->getMessage());
210210
}
211+
}
211212

213+
public function testRateLimiterAutoLockFactoryWithLockEnabled()
214+
{
212215
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
213216
$container->loadFromExtension('framework', [
214217
'annotations' => false,
@@ -226,13 +229,35 @@ public function testRateLimiterWithLockFactory()
226229
$this->assertEquals('lock.factory', (string) $withLock->getArgument(2));
227230
}
228231

229-
public function testRateLimiterLockFactory()
232+
public function testRateLimiterAutoLockFactoryWithLockDisabled()
230233
{
231234
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
232235
$container->loadFromExtension('framework', [
233236
'annotations' => false,
234237
'http_method_override' => false,
235238
'handle_all_throwables' => true,
239+
'lock' => false,
240+
'php_errors' => ['log' => true],
241+
'rate_limiter' => [
242+
'without_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
243+
],
244+
]);
245+
});
246+
247+
$this->expectException(OutOfBoundsException::class);
248+
$this->expectExceptionMessageMatches('/^The argument "2" doesn\'t exist.*\.$/');
249+
250+
$container->getDefinition('limiter.without_lock')->getArgument(2);
251+
}
252+
253+
public function testRateLimiterDisableLockFactory()
254+
{
255+
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
256+
$container->loadFromExtension('framework', [
257+
'annotations' => false,
258+
'http_method_override' => false,
259+
'handle_all_throwables' => true,
260+
'lock' => true,
236261
'php_errors' => ['log' => true],
237262
'rate_limiter' => [
238263
'without_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour', 'lock_factory' => null],

0 commit comments

Comments
 (0)