Skip to content

add ThrottlePlugin #460

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 12 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
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()
Copy link
Collaborator

@dbu dbu Jun 17, 2024

Choose a reason for hiding this comment

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

looking at the test failure in #461: what is the name here? and what is key?

the name is not made required, but the configuration class does not handle a missing name field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dbu You need to set name, name this is required option

Copy link
Collaborator

@dbu dbu Jun 17, 2024

Choose a reason for hiding this comment

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

then we are missing isRequired()

can you please also add an info() as well with an explanation what this name is? the name of what thing do we specify here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, check #462

->scalarNode('key')->defaultNull()->end()
->integerNode('tokens')->defaultValue(1)->end()
->floatNode('max_time')->defaultNull()->end()
->end()
->end();
// End throttle plugin
}

/**
Expand Down
24 changes: 24 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,28 @@ private function configurePluginByName($name, Definition $definition, array $con

break;

case 'throttle':
if (!\interface_exists(LimiterInterface::class)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this check is redundant. the throttle-plugin repository depends on symfony/rate-limiter. checking for the ThrottlePlugin is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

throw new InvalidConfigurationException('You need to require the Rate Limiter to be able to use it: "composer require symfony/rate-limiter".');
}

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'] : '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

if name is required, this check is for the special case where name is set to ''? below it would still do strange things when name is ''

Copy link
Collaborator

Choose a reason for hiding this comment

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

and i think symfony configuration will not allow '' on something that is set to be required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, check #462

$container
->register($serviceId.$key, LimiterInterface::class)
->setFactory([new Reference('limiter.'.$config['name']), 'create'])
Copy link
Collaborator

Choose a reason for hiding this comment

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

this does not check if the name is set or not. and if the name is empty, it will use limiter., is that the intention?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dbu No, it was my mistake, I assume that if the default value is not specified, then it is required by default

->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