Skip to content

Throttle plugin fixes #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throttle plugin needs symfony 7.4 as minimum requirement. test optionally for 1.x and cleanup in 2.x

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