Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #53550 [FrameworkBundle][HttpClient] Add `ThrottlingHttpClien…
…t` to limit requests within a timeframe (HypeMC) This PR was merged into the 7.1 branch. Discussion ---------- [FrameworkBundle][HttpClient] Add `ThrottlingHttpClient` to limit requests within a timeframe | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT This PR adds a simple `ThrottlingHttpClient` to help with limiting the number of requests within a certain period. Simple example, don't send more than 10 requests in 5 second: ```yaml framework: http_client: scoped_clients: ping.client: base_uri: 'http://localhost:8080' rate_limiter: http_throttling rate_limiter: http_throttling: policy: 'token_bucket' limit: 10 rate: { interval: '5 seconds', amount: 10 } ``` ```php #[AsCommand('app:ping')] class PingCommand extends Command { public function __construct( #[Target('ping.client')] private HttpClientInterface $httpClient, ) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { $requests = []; for ($i = 0; $i < 100; $i++) { $requests[] = $this->httpClient->request('GET', '/ping'); } foreach ($requests as $request) { $output->writeln($request->getContent()); } return Command::SUCCESS; } } ``` Receiving controller: ```php #[AsController] class PingController { #[Route('/ping', name: 'app_ping')] public function __invoke(): Response { return new Response((new \DateTime())->format('Y-m-d\TH:i:s.u')); } } ``` Output: ``` $ bin/console app:ping 2024-01-16T11:52:54.922597 2024-01-16T11:52:54.964851 2024-01-16T11:52:55.009504 2024-01-16T11:52:55.053986 2024-01-16T11:52:55.098267 2024-01-16T11:52:55.139621 2024-01-16T11:52:55.182769 2024-01-16T11:52:55.264433 2024-01-16T11:52:55.304775 2024-01-16T11:52:55.223805 2024-01-16T11:52:59.877858 2024-01-16T11:52:59.914724 2024-01-16T11:52:59.950610 2024-01-16T11:52:59.986818 2024-01-16T11:53:00.025638 2024-01-16T11:53:00.063687 2024-01-16T11:53:00.879850 2024-01-16T11:53:00.919651 2024-01-16T11:53:00.959273 2024-01-16T11:53:00.999381 2024-01-16T11:53:04.890126 2024-01-16T11:53:04.965116 2024-01-16T11:53:04.999916 2024-01-16T11:53:05.037780 2024-01-16T11:53:05.073999 2024-01-16T11:53:05.110241 2024-01-16T11:53:05.917045 2024-01-16T11:53:05.971672 2024-01-16T11:53:06.016025 2024-01-16T11:53:06.059220 2024-01-16T11:53:09.920092 2024-01-16T11:53:09.974757 2024-01-16T11:53:10.010406 2024-01-16T11:53:10.045790 2024-01-16T11:53:10.081749 2024-01-16T11:53:10.118100 2024-01-16T11:53:10.924485 2024-01-16T11:53:10.981094 2024-01-16T11:53:11.023935 2024-01-16T11:53:11.067210 ... ``` Commits ------- 5bb5474 [FrameworkBundle][HttpClient] Add `ThrottlingHttpClient` to limit requests within a timeframe
- Loading branch information
Showing
13 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...undle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_rate_limiter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', [ | ||
'annotations' => false, | ||
'http_method_override' => false, | ||
'handle_all_throwables' => true, | ||
'php_errors' => ['log' => true], | ||
'rate_limiter' => [ | ||
'foo_limiter' => [ | ||
'lock_factory' => null, | ||
'policy' => 'token_bucket', | ||
'limit' => 10, | ||
'rate' => ['interval' => '5 seconds', 'amount' => 10], | ||
], | ||
], | ||
'http_client' => [ | ||
'default_options' => [ | ||
'rate_limiter' => 'default_limiter', | ||
], | ||
'scoped_clients' => [ | ||
'foo' => [ | ||
'base_uri' => 'http://example.com', | ||
'rate_limiter' => 'foo_limiter', | ||
], | ||
], | ||
], | ||
]); |
21 changes: 21 additions & 0 deletions
21
...undle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_rate_limiter.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config http-method-override="false" handle-all-throwables="true"> | ||
<framework:annotations enabled="false" /> | ||
<framework:php-errors log="true" /> | ||
<framework:rate-limiter> | ||
<framework:limiter name="foo_limiter" lock-factory="null" policy="token_bucket" limit="10"> | ||
<framework:rate interval="5 seconds" amount="10" /> | ||
</framework:limiter> | ||
</framework:rate-limiter> | ||
<framework:http-client> | ||
<framework:default-options rate-limiter="default_limiter" /> | ||
<framework:scoped-client name="foo" base-uri="http://example.com" rate-limiter="foo_limiter" /> | ||
</framework:http-client> | ||
</framework:config> | ||
</container> |
19 changes: 19 additions & 0 deletions
19
...undle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_rate_limiter.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
framework: | ||
annotations: false | ||
http_method_override: false | ||
handle_all_throwables: true | ||
php_errors: | ||
log: true | ||
rate_limiter: | ||
foo_limiter: | ||
lock_factory: null | ||
policy: token_bucket | ||
limit: 10 | ||
rate: { interval: '5 seconds', amount: 10 } | ||
http_client: | ||
default_options: | ||
rate_limiter: default_limiter | ||
scoped_clients: | ||
foo: | ||
base_uri: http://example.com | ||
rate_limiter: foo_limiter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.