Skip to content

[FrameworkBundle][HttpClient] Add ThrottlingHttpClient #19729

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
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
34 changes: 34 additions & 0 deletions http_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,40 @@ installed in your application::
:class:`Symfony\\Component\\HttpClient\\CachingHttpClient` accepts a third argument
to set the options of the :class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache`.

Limit the Number of Requests
----------------------------

This component provides a :class:`Symfony\\Component\\HttpClient\\ThrottlingHttpClient`
decorator that allows to limit the number of requests within a certain period.

The implementation leverages the
:class:`Symfony\\Component\\RateLimiter\\LimiterInterface` class under the hood
so that the :doc:`Rate Limiter component </rate_limiter>` needs to be
installed in your application::

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\ThrottlingHttpClient;
use Symfony\Component\RateLimiter\LimiterInterface;

$rateLimiter = ...; // $rateLimiter is an instance of Symfony\Component\RateLimiter\LimiterInterface
$client = HttpClient::create();
$client = new ThrottlingHttpClient($client, $rateLimiter);

$requests = [];
for ($i = 0; $i < 100; $i++) {
$requests[] = $client->request('GET', 'https://example.com');
}

foreach ($requests as $request) {
// Depending on rate limiting policy, calls will be delayed
$output->writeln($request->getContent());
}

.. versionadded:: 7.1

The :class:`Symfony\\Component\\HttpClient\\ThrottlingHttpClient` was
introduced in Symfony 7.1.

Consuming Server-Sent Events
----------------------------

Expand Down
14 changes: 14 additions & 0 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,20 @@ query
An associative array of the query string values added to the URL before making
the request. This value must use the format ``['parameter-name' => parameter-value, ...]``.

rate_limiter
............

**type**: ``string``

This option limit the number of requests within a certain period thanks
to the :doc:`Rate Limiter component </rate_limiter>`.

The rate limiter service ID you want to use.

.. versionadded:: 7.1

The ``rate_limiter`` option was introduced in Symfony 7.1.

resolve
.......

Expand Down