Skip to content

Commit

Permalink
add configuration for ThrottlePlugin (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Big-Shark authored Jun 17, 2024
1 parent b7ad2c1 commit 58223eb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"php-http/guzzle6-adapter": "<1.1",
"php-http/cache-plugin": "<1.7.0",
"php-http/curl-client": "<2.0",
"php-http/socket-client": "<2.0"
"php-http/socket-client": "<2.0",
"php-http/throttle-plugin": "<1.1"
},
"require-dev": {
"guzzlehttp/psr7": "^1.7 || ^2.0",
Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,18 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
->end()
->end();
// End error plugin

$throttle = $children->arrayNode('throttle')
->canBeEnabled()
->addDefaultsIfNotSet()
->children()
->scalarNode('name')->end()
->scalarNode('key')->defaultNull()->end()
->integerNode('tokens')->defaultValue(1)->end()
->floatNode('max_time')->defaultNull()->end()
->end()
->end();
// End throttle plugin
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\Plugin\ThrottlePlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\HttpAsyncClient;
Expand All @@ -35,6 +36,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\RateLimiter\LimiterInterface;
use Twig\Environment as TwigEnvironment;

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

break;

case 'throttle':
if (!\class_exists(ThrottlePlugin::class)) {
throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".');
}

$key = $config['name'] ? '.'.$config['name'] : '';
$container
->register($serviceId.$key, LimiterInterface::class)
->setFactory([new Reference('limiter.'.$config['name']), 'create'])
->addArgument($config['key'])
->setPublic(false);

$definition->replaceArgument(0, new Reference($serviceId.$key));
$definition->setArgument('$tokens', $config['tokens']);
$definition->setArgument('$maxTime', $config['max_time']);

break;

/* client specific plugins */

case 'add_host':
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<service id="httplug.plugin.stopwatch" class="Http\Client\Common\Plugin\StopwatchPlugin" public="false" abstract="true">
<argument />
</service>
<service id="httplug.plugin.throttle" class="Http\Client\Common\Plugin\ThrottlePlugin" public="false" abstract="true">
<argument />
</service>

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

Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
'enabled' => false,
'only_server_exception' => false,
],
'throttle' => [
'enabled' => false,
'key' => null,
'tokens' => 1,
'max_time' => null,
],
],
'discovery' => [
'client' => 'auto',
Expand Down Expand Up @@ -308,6 +314,12 @@ public function testSupportsAllConfigFormats(): void
'enabled' => false,
'only_server_exception' => false,
],
'throttle' => [
'enabled' => false,
'key' => null,
'tokens' => 1,
'max_time' => null,
],
],
'discovery' => [
'client' => 'auto',
Expand Down

0 comments on commit 58223eb

Please sign in to comment.