Skip to content

Commit 2ffa230

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

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

Diff for: .github/workflows/continuous-integration.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- dependencies: "php-http/guzzle7-adapter"
4444
php-version: "8.2"
4545
symfony-deprecations-helper: "weak"
46-
- dependencies: "php-http/guzzle7-adapter"
46+
- dependencies: "php-http/guzzle7-adapter php-http/throttle-plugin"
4747
php-version: "8.3"
4848
symfony-deprecations-helper: "weak"
4949

Diff for: src/DependencyInjection/Configuration.php

+13-4
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

Diff for: src/DependencyInjection/HttplugExtension.php

+4-3
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

Diff for: tests/Unit/DependencyInjection/HttplugExtensionTest.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;
66

77
use Http\Adapter\Guzzle7\Client;
8+
use Http\Client\Common\Plugin\ThrottlePlugin;
89
use Http\Client\HttpClient;
910
use Http\Client\Plugin\Vcr\Recorder\InMemoryRecorder;
1011
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
@@ -79,7 +80,7 @@ public function testConfigLoadService(): void
7980

8081
public function testClientPlugins(): void
8182
{
82-
$this->load([
83+
$config = [
8384
'clients' => [
8485
'acme' => [
8586
'factory' => 'httplug.factory.curl',
@@ -130,6 +131,11 @@ public function testClientPlugins(): void
130131
'headers' => ['X-FOO'],
131132
],
132133
],
134+
[
135+
'query_defaults' => [
136+
'parameters' => ['locale' => 'en'],
137+
],
138+
],
133139
[
134140
'request_seekable_body' => [
135141
'use_file_buffer' => true,
@@ -138,11 +144,6 @@ public function testClientPlugins(): void
138144
[
139145
'response_seekable_body' => true,
140146
],
141-
[
142-
'query_defaults' => [
143-
'parameters' => ['locale' => 'en'],
144-
],
145-
],
146147
[
147148
'authentication' => [
148149
'my_basic' => [
@@ -165,7 +166,16 @@ public function testClientPlugins(): void
165166
],
166167
],
167168
],
168-
]);
169+
];
170+
if (class_exists(ThrottlePlugin::class)) {
171+
$config['clients']['acme']['plugins'][] = [
172+
'throttle' => [
173+
'name' => 'limiter.test',
174+
],
175+
];
176+
}
177+
178+
$this->load($config);
169179

170180
$plugins = [
171181
'httplug.client.acme.plugin.decoder',
@@ -178,13 +188,16 @@ public function testClientPlugins(): void
178188
'httplug.client.acme.plugin.header_defaults',
179189
'httplug.client.acme.plugin.header_set',
180190
'httplug.client.acme.plugin.header_remove',
191+
'httplug.client.acme.plugin.query_defaults',
181192
'httplug.client.acme.plugin.request_seekable_body',
182193
'httplug.client.acme.plugin.response_seekable_body',
183-
'httplug.client.acme.plugin.query_defaults',
184194
'httplug.client.acme.authentication.my_basic',
185195
'httplug.client.acme.plugin.cache',
186196
'httplug.client.acme.plugin.error',
187197
];
198+
if (\class_exists(ThrottlePlugin::class)) {
199+
$plugins[] = 'httplug.client.acme.plugin.throttle';
200+
}
188201
$pluginReferences = array_map(function ($id) {
189202
return new Reference($id);
190203
}, $plugins);

0 commit comments

Comments
 (0)