Skip to content

Commit bc88843

Browse files
committed
[FrameworkBundle][HttpClient] Adding an explanation of ThrottlingHttpClient integration with the Framework
1 parent dc59969 commit bc88843

File tree

1 file changed

+89
-15
lines changed

1 file changed

+89
-15
lines changed

http_client.rst

+89-15
Original file line numberDiff line numberDiff line change
@@ -1488,30 +1488,104 @@ Limit the Number of Requests
14881488
----------------------------
14891489

14901490
This component provides a :class:`Symfony\\Component\\HttpClient\\ThrottlingHttpClient`
1491-
decorator that allows to limit the number of requests within a certain period.
1491+
decorator that allows to limit the number of requests within a certain period,
1492+
potentially delaying calls based on the rate limiting policy.
14921493

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

1498-
use Symfony\Component\HttpClient\HttpClient;
1499-
use Symfony\Component\HttpClient\ThrottlingHttpClient;
1500-
use Symfony\Component\RateLimiter\LimiterInterface;
1499+
.. configuration-block::
15011500

1502-
$rateLimiter = ...; // $rateLimiter is an instance of Symfony\Component\RateLimiter\LimiterInterface
1503-
$client = HttpClient::create();
1504-
$client = new ThrottlingHttpClient($client, $rateLimiter);
1501+
.. code-block:: yaml
15051502
1506-
$requests = [];
1507-
for ($i = 0; $i < 100; $i++) {
1508-
$requests[] = $client->request('GET', 'https://example.com');
1509-
}
1503+
# config/packages/framework.yaml
1504+
framework:
1505+
http_client:
1506+
scoped_clients:
1507+
example.client:
1508+
base_uri: 'https://example.com'
1509+
rate_limiter: 'http_example_limiter'
15101510
1511-
foreach ($requests as $request) {
1512-
// Depending on rate limiting policy, calls will be delayed
1513-
$output->writeln($request->getContent());
1514-
}
1511+
rate_limiter:
1512+
# Don't send more than 10 requests in 5 seconds
1513+
http_example_limiter:
1514+
policy: 'token_bucket'
1515+
limit: 10
1516+
rate: { interval: '5 seconds', amount: 10 }
1517+
1518+
.. code-block:: xml
1519+
1520+
<!-- config/packages/framework.xml -->
1521+
<?xml version="1.0" encoding="UTF-8" ?>
1522+
<container xmlns="http://symfony.com/schema/dic/services"
1523+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1524+
xmlns:framework="http://symfony.com/schema/dic/symfony"
1525+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1526+
https://symfony.com/schema/dic/services/services-1.0.xsd
1527+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
1528+
1529+
<framework:config>
1530+
<framework:http-client>
1531+
<framework:scoped-client name="example.client"
1532+
base-uri="https://example.com"
1533+
rate-limiter="http_example_limiter"
1534+
/>
1535+
</framework:http-client>
1536+
1537+
<framework:rate-limiter>
1538+
<!-- Don't send more than 10 requests in 5 seconds -->
1539+
<framework:limiter name="http_example_limiter"
1540+
policy="token_bucket"
1541+
limit="10"
1542+
>
1543+
<framework:rate interval="5 seconds" amount="10"/>
1544+
</framework:limiter>
1545+
</framework:rate-limiter>
1546+
</framework:config>
1547+
</container>
1548+
1549+
.. code-block:: php
1550+
1551+
// config/packages/framework.php
1552+
use Symfony\Config\FrameworkConfig;
1553+
1554+
return static function (FrameworkConfig $framework): void {
1555+
$framework->httpClient()->scopedClient('example.client')
1556+
->baseUri('https://example.com')
1557+
->rateLimiter('http_example_limiter');
1558+
// ...
1559+
;
1560+
1561+
$framework->rateLimiter()
1562+
// Don't send more than 10 requests in 5 seconds
1563+
->limiter('http_example_limiter')
1564+
->policy('token_bucket')
1565+
->limit(10)
1566+
->rate()
1567+
->interval('5 seconds')
1568+
->amount(10)
1569+
;
1570+
};
1571+
1572+
.. code-block:: php-standalone
1573+
1574+
use Symfony\Component\HttpClient\HttpClient;
1575+
use Symfony\Component\HttpClient\ThrottlingHttpClient;
1576+
use Symfony\Component\RateLimiter\RateLimiterFactory;
1577+
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
1578+
1579+
$factory = new RateLimiterFactory([
1580+
'id' => 'http_example_limiter',
1581+
'policy' => 'token_bucket',
1582+
'limit' => 10,
1583+
'rate' => ['interval' => '5 seconds', 'amount' => 10],
1584+
], new InMemoryStorage());
1585+
$limiter = $factory->create();
1586+
1587+
$client = HttpClient::createForBaseUri('https://example.com');
1588+
$throttlingClient = new ThrottlingHttpClient($client, $limiter);
15151589
15161590
.. versionadded:: 7.1
15171591

0 commit comments

Comments
 (0)