Skip to content

Commit 58223eb

Browse files
authored
add configuration for ThrottlePlugin (#460)
1 parent b7ad2c1 commit 58223eb

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"php-http/guzzle6-adapter": "<1.1",
4646
"php-http/cache-plugin": "<1.7.0",
4747
"php-http/curl-client": "<2.0",
48-
"php-http/socket-client": "<2.0"
48+
"php-http/socket-client": "<2.0",
49+
"php-http/throttle-plugin": "<1.1"
4950
},
5051
"require-dev": {
5152
"guzzlehttp/psr7": "^1.7 || ^2.0",

src/DependencyInjection/Configuration.php

+12
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
624624
->end()
625625
->end();
626626
// End error plugin
627+
628+
$throttle = $children->arrayNode('throttle')
629+
->canBeEnabled()
630+
->addDefaultsIfNotSet()
631+
->children()
632+
->scalarNode('name')->end()
633+
->scalarNode('key')->defaultNull()->end()
634+
->integerNode('tokens')->defaultValue(1)->end()
635+
->floatNode('max_time')->defaultNull()->end()
636+
->end()
637+
->end();
638+
// End throttle plugin
627639
}
628640

629641
/**

src/DependencyInjection/HttplugExtension.php

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Http\Client\Common\HttpMethodsClient;
1111
use Http\Client\Common\HttpMethodsClientInterface;
1212
use Http\Client\Common\Plugin\AuthenticationPlugin;
13+
use Http\Client\Common\Plugin\ThrottlePlugin;
1314
use Http\Client\Common\PluginClient;
1415
use Http\Client\Common\PluginClientFactory;
1516
use Http\Client\HttpAsyncClient;
@@ -35,6 +36,7 @@
3536
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3637
use Symfony\Component\DependencyInjection\Reference;
3738
use Symfony\Component\HttpKernel\Kernel;
39+
use Symfony\Component\RateLimiter\LimiterInterface;
3840
use Twig\Environment as TwigEnvironment;
3941

4042
/**
@@ -292,6 +294,24 @@ private function configurePluginByName($name, Definition $definition, array $con
292294

293295
break;
294296

297+
case 'throttle':
298+
if (!\class_exists(ThrottlePlugin::class)) {
299+
throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".');
300+
}
301+
302+
$key = $config['name'] ? '.'.$config['name'] : '';
303+
$container
304+
->register($serviceId.$key, LimiterInterface::class)
305+
->setFactory([new Reference('limiter.'.$config['name']), 'create'])
306+
->addArgument($config['key'])
307+
->setPublic(false);
308+
309+
$definition->replaceArgument(0, new Reference($serviceId.$key));
310+
$definition->setArgument('$tokens', $config['tokens']);
311+
$definition->setArgument('$maxTime', $config['max_time']);
312+
313+
break;
314+
295315
/* client specific plugins */
296316

297317
case 'add_host':

src/Resources/config/plugins.xml

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<service id="httplug.plugin.stopwatch" class="Http\Client\Common\Plugin\StopwatchPlugin" public="false" abstract="true">
2929
<argument />
3030
</service>
31+
<service id="httplug.plugin.throttle" class="Http\Client\Common\Plugin\ThrottlePlugin" public="false" abstract="true">
32+
<argument />
33+
</service>
3134

3235
<!-- client specific plugin definition prototypes -->
3336

tests/Unit/DependencyInjection/ConfigurationTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
9898
'enabled' => false,
9999
'only_server_exception' => false,
100100
],
101+
'throttle' => [
102+
'enabled' => false,
103+
'key' => null,
104+
'tokens' => 1,
105+
'max_time' => null,
106+
],
101107
],
102108
'discovery' => [
103109
'client' => 'auto',
@@ -308,6 +314,12 @@ public function testSupportsAllConfigFormats(): void
308314
'enabled' => false,
309315
'only_server_exception' => false,
310316
],
317+
'throttle' => [
318+
'enabled' => false,
319+
'key' => null,
320+
'tokens' => 1,
321+
'max_time' => null,
322+
],
311323
],
312324
'discovery' => [
313325
'client' => 'auto',

0 commit comments

Comments
 (0)