Skip to content

Commit e6cf138

Browse files
Maxim Solovevdbu
Maxim Solovev
authored andcommitted
Throttle plugin configuration documentation
1 parent 3fdfecc commit e6cf138

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"php-http/cache-plugin": "^1.7",
5858
"php-http/mock-client": "^1.2",
5959
"php-http/promise": "^1.0",
60+
"php-http/throttle-plugin": "^1.1",
6061
"phpunit/phpunit": "^9.6",
6162
"symfony/browser-kit": "^4.4 || ^5.0 || ^6.0 || ^7.0",
6263
"symfony/cache": "^4.4 || ^5.0 || ^6.0 || ^7.0",

src/DependencyInjection/Configuration.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,21 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
630630
->addDefaultsIfNotSet()
631631
->children()
632632
->scalarNode('name')
633-
->info('The name of the configured symfony/rate-limiter to use')
634633
->isRequired()
634+
->info('Rate limiter service name from symfony/rate-limiter configuration. E.g. for a rate limiter http_client you specify limiter.http_client here')
635+
->end()
636+
->scalarNode('key')
637+
->defaultNull()
638+
->info('Key to avoid sharing this rate limiter with other clients or other services. You can use the name of the client for example.')
639+
->end()
640+
->integerNode('tokens')
641+
->defaultValue(1)
642+
->info('How many tokens spending per request')
643+
->end()
644+
->floatNode('max_time')
645+
->defaultNull()
646+
->info('Maximum accepted waiting time in seconds')
635647
->end()
636-
->scalarNode('key')->defaultNull()->end()
637-
->integerNode('tokens')->defaultValue(1)->end()
638-
->floatNode('max_time')->defaultNull()->end()
639648
->end()
640649
->end();
641650
// End throttle plugin

src/DependencyInjection/HttplugExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,14 @@ private function configurePluginByName($name, Definition $definition, array $con
299299
throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".');
300300
}
301301

302+
$limiterServiceId = $serviceId.'.'.$config['name'];
302303
$container
303-
->register($serviceId.$config['name'], LimiterInterface::class)
304-
->setFactory([new Reference('limiter.'.$config['name']), 'create'])
304+
->register($limiterServiceId, LimiterInterface::class)
305+
->setFactory([new Reference($config['name']), 'create'])
305306
->addArgument($config['key'])
306307
->setPublic(false);
307308

308-
$definition->replaceArgument(0, new Reference($serviceId.$config['name']));
309+
$definition->replaceArgument(0, new Reference($limiterServiceId));
309310
$definition->setArgument('$tokens', $config['tokens']);
310311
$definition->setArgument('$maxTime', $config['max_time']);
311312

tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public function testClientPlugins(): void
130130
'headers' => ['X-FOO'],
131131
],
132132
],
133+
[
134+
'query_defaults' => [
135+
'parameters' => ['locale' => 'en'],
136+
],
137+
],
133138
[
134139
'request_seekable_body' => [
135140
'use_file_buffer' => true,
@@ -139,8 +144,8 @@ public function testClientPlugins(): void
139144
'response_seekable_body' => true,
140145
],
141146
[
142-
'query_defaults' => [
143-
'parameters' => ['locale' => 'en'],
147+
'throttle' => [
148+
'name' => 'limiter.test',
144149
],
145150
],
146151
[
@@ -178,9 +183,10 @@ public function testClientPlugins(): void
178183
'httplug.client.acme.plugin.header_defaults',
179184
'httplug.client.acme.plugin.header_set',
180185
'httplug.client.acme.plugin.header_remove',
186+
'httplug.client.acme.plugin.query_defaults',
181187
'httplug.client.acme.plugin.request_seekable_body',
182188
'httplug.client.acme.plugin.response_seekable_body',
183-
'httplug.client.acme.plugin.query_defaults',
189+
'httplug.client.acme.plugin.throttle',
184190
'httplug.client.acme.authentication.my_basic',
185191
'httplug.client.acme.plugin.cache',
186192
'httplug.client.acme.plugin.error',

0 commit comments

Comments
 (0)