Skip to content

Commit 8ece739

Browse files
committed
minor #19729 [FrameworkBundle][HttpClient] Add ThrottlingHttpClient (alamirault)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [FrameworkBundle][HttpClient] Add ThrottlingHttpClient Fix #19482 Commits ------- 265a69c [FrameworkBundle][HttpClient] Add ThrottlingHttpClient
2 parents 6b3d171 + 265a69c commit 8ece739

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

http_client.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,40 @@ installed in your application::
14741474
:class:`Symfony\\Component\\HttpClient\\CachingHttpClient` accepts a third argument
14751475
to set the options of the :class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache`.
14761476

1477+
Limit the Number of Requests
1478+
----------------------------
1479+
1480+
This component provides a :class:`Symfony\\Component\\HttpClient\\ThrottlingHttpClient`
1481+
decorator that allows to limit the number of requests within a certain period.
1482+
1483+
The implementation leverages the
1484+
:class:`Symfony\\Component\\RateLimiter\\LimiterInterface` class under the hood
1485+
so that the :doc:`Rate Limiter component </rate_limiter>` needs to be
1486+
installed in your application::
1487+
1488+
use Symfony\Component\HttpClient\HttpClient;
1489+
use Symfony\Component\HttpClient\ThrottlingHttpClient;
1490+
use Symfony\Component\RateLimiter\LimiterInterface;
1491+
1492+
$rateLimiter = ...; // $rateLimiter is an instance of Symfony\Component\RateLimiter\LimiterInterface
1493+
$client = HttpClient::create();
1494+
$client = new ThrottlingHttpClient($client, $rateLimiter);
1495+
1496+
$requests = [];
1497+
for ($i = 0; $i < 100; $i++) {
1498+
$requests[] = $client->request('GET', 'https://example.com');
1499+
}
1500+
1501+
foreach ($requests as $request) {
1502+
// Depending on rate limiting policy, calls will be delayed
1503+
$output->writeln($request->getContent());
1504+
}
1505+
1506+
.. versionadded:: 7.1
1507+
1508+
The :class:`Symfony\\Component\\HttpClient\\ThrottlingHttpClient` was
1509+
introduced in Symfony 7.1.
1510+
14771511
Consuming Server-Sent Events
14781512
----------------------------
14791513

reference/configuration/framework.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,20 @@ query
11741174
An associative array of the query string values added to the URL before making
11751175
the request. This value must use the format ``['parameter-name' => parameter-value, ...]``.
11761176

1177+
rate_limiter
1178+
............
1179+
1180+
**type**: ``string``
1181+
1182+
This option limit the number of requests within a certain period thanks
1183+
to the :doc:`Rate Limiter component </rate_limiter>`.
1184+
1185+
The rate limiter service ID you want to use.
1186+
1187+
.. versionadded:: 7.1
1188+
1189+
The ``rate_limiter`` option was introduced in Symfony 7.1.
1190+
11771191
resolve
11781192
.......
11791193

0 commit comments

Comments
 (0)