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

add ThrottlePlugin #460

merged 12 commits into from
Jun 17, 2024

Conversation

Big-Shark
Copy link
Contributor

@Big-Shark Big-Shark commented Jun 14, 2024

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
License MIT

What's in this PR?

Add config settings for ThrottlePlugin

Why?

Adding the plugin without using configurations causes inconvenience.

Example

    default:
        factory: 'httplug.factory.curl'
        config:
            CURLOPT_TIMEOUT: 10
        plugins:
            - throttle:
                  name: default
                  key: default
                  tokens: 1
                  max_time: 1

@Big-Shark Big-Shark changed the title add ThrottlePlugin draft: add ThrottlePlugin Jun 14, 2024
@Big-Shark Big-Shark changed the title draft: add ThrottlePlugin add ThrottlePlugin Jun 14, 2024
@Big-Shark
Copy link
Contributor Author

@dbu Please, can you check my PR. If you have some question, just ask me.

Copy link
Collaborator

@dbu dbu left a comment

Choose a reason for hiding this comment

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

thanks, nice work! what happens if somebody would install the old version of throttle plugin with these changes? with the named parameters, we would get an error from symfony about the new parameters, i think? if that is correct, we should add a conflict in composer.json to avoid installing this with a too old version.

can you please add to the HttplugExtensionTest::testClientPlugins to enable the throttle plugin and verify the service is created (the test is generic, just add the config and the expected service name)

to keep the documentation up to date, can you please also add to https://github.com/php-http/documentation/blob/main/integrations/symfony-bundle.rst#list-of-services and https://github.com/php-http/documentation/blob/main/integrations/symfony-full-configuration.rst ?

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

@Big-Shark
Copy link
Contributor Author

Big-Shark commented Jun 16, 2024

@dbu

thanks, nice work! what happens if somebody would install the old version of throttle plugin with these changes? with the named parameters, we would get an error from symfony about the new parameters, i think? if that is correct, we should add a conflict in composer.json to avoid installing this with a too old version.

yes, you are right, thanks
image

can you please add to the HttplugExtensionTest::testClientPlugins to enable the throttle plugin and verify the service is created (the test is generic, just add the config and the expected service name)

I tried, but minimal supported version for bundle, it's 7.3, but throttle plugin works only with 7.4 and upper, and I can not run this test in 7.3. I think we can add this only in v2, if you want.
https://github.com/php-http/HttplugBundle/actions/runs/9521365832/job/26248616912

@ostrolucky
Copy link
Collaborator

I think you can make this work if you use positional arguments instead of named

@Big-Shark
Copy link
Contributor Author

@ostrolucky Possible, but is it necessary, because there are no backwards compatible changes?

@ostrolucky
Copy link
Collaborator

Well I believe it will be backwards compatible with positional arguments instead of named ones.

@Big-Shark
Copy link
Contributor Author

@ostrolucky It will work, but the options won't work as expected, which can also be confusing.

Copy link
Collaborator

@dbu dbu left a comment

Choose a reason for hiding this comment

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

regarding the test with the plugin: i see. then lets add that in the 2.x branch. i will merge this now and then merge 1.x to 2.x

@dbu dbu merged commit 58223eb into php-http:1.x Jun 17, 2024
14 checks passed
@Big-Shark
Copy link
Contributor Author

@dbu thanks, please, create new release

@dbu
Copy link
Collaborator

dbu commented Jun 17, 2024

on it, on it ;-)

https://github.com/php-http/HttplugBundle/releases/tag/1.34.0

and merging to 2.x in #461 (i added the test right away)

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

$key = $config['name'] ? '.'.$config['name'] : '';
$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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants