Skip to content

Commit

Permalink
Throttle plugin configuration documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Solovev authored and dbu committed Sep 1, 2024
1 parent 3fdfecc commit 2ffa230
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- dependencies: "php-http/guzzle7-adapter"
php-version: "8.2"
symfony-deprecations-helper: "weak"
- dependencies: "php-http/guzzle7-adapter"
- dependencies: "php-http/guzzle7-adapter php-http/throttle-plugin"
php-version: "8.3"
symfony-deprecations-helper: "weak"

Expand Down
17 changes: 13 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,21 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
->addDefaultsIfNotSet()
->children()
->scalarNode('name')
->info('The name of the configured symfony/rate-limiter to use')
->isRequired()
->info('Rate limiter service name from symfony/rate-limiter configuration. E.g. for a rate limiter http_client you specify limiter.http_client here')
->end()
->scalarNode('key')
->defaultNull()
->info('Key to avoid sharing this rate limiter with other clients or other services. You can use the name of the client for example.')
->end()
->integerNode('tokens')
->defaultValue(1)
->info('How many tokens spending per request')
->end()
->floatNode('max_time')
->defaultNull()
->info('Maximum accepted waiting time in seconds')
->end()
->scalarNode('key')->defaultNull()->end()
->integerNode('tokens')->defaultValue(1)->end()
->floatNode('max_time')->defaultNull()->end()
->end()
->end();
// End throttle plugin
Expand Down
7 changes: 4 additions & 3 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,14 @@ private function configurePluginByName($name, Definition $definition, array $con
throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".');
}

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

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

Expand Down
29 changes: 21 additions & 8 deletions tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;

use Http\Adapter\Guzzle7\Client;
use Http\Client\Common\Plugin\ThrottlePlugin;
use Http\Client\HttpClient;
use Http\Client\Plugin\Vcr\Recorder\InMemoryRecorder;
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function testConfigLoadService(): void

public function testClientPlugins(): void
{
$this->load([
$config = [
'clients' => [
'acme' => [
'factory' => 'httplug.factory.curl',
Expand Down Expand Up @@ -130,6 +131,11 @@ public function testClientPlugins(): void
'headers' => ['X-FOO'],
],
],
[
'query_defaults' => [
'parameters' => ['locale' => 'en'],
],
],
[
'request_seekable_body' => [
'use_file_buffer' => true,
Expand All @@ -138,11 +144,6 @@ public function testClientPlugins(): void
[
'response_seekable_body' => true,
],
[
'query_defaults' => [
'parameters' => ['locale' => 'en'],
],
],
[
'authentication' => [
'my_basic' => [
Expand All @@ -165,7 +166,16 @@ public function testClientPlugins(): void
],
],
],
]);
];
if (class_exists(ThrottlePlugin::class)) {
$config['clients']['acme']['plugins'][] = [
'throttle' => [
'name' => 'limiter.test',
],
];
}

$this->load($config);

$plugins = [
'httplug.client.acme.plugin.decoder',
Expand All @@ -178,13 +188,16 @@ public function testClientPlugins(): void
'httplug.client.acme.plugin.header_defaults',
'httplug.client.acme.plugin.header_set',
'httplug.client.acme.plugin.header_remove',
'httplug.client.acme.plugin.query_defaults',
'httplug.client.acme.plugin.request_seekable_body',
'httplug.client.acme.plugin.response_seekable_body',
'httplug.client.acme.plugin.query_defaults',
'httplug.client.acme.authentication.my_basic',
'httplug.client.acme.plugin.cache',
'httplug.client.acme.plugin.error',
];
if (\class_exists(ThrottlePlugin::class)) {
$plugins[] = 'httplug.client.acme.plugin.throttle';
}
$pluginReferences = array_map(function ($id) {
return new Reference($id);
}, $plugins);
Expand Down

0 comments on commit 2ffa230

Please sign in to comment.