@@ -1488,30 +1488,108 @@ Limit the Number of Requests
1488
1488
----------------------------
1489
1489
1490
1490
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, potentially delaying calls based on the rate limiting policy .
1492
1492
1493
1493
The implementation leverages the
1494
1494
:class: `Symfony\\ Component\\ RateLimiter\\ LimiterInterface ` class under the hood
1495
1495
so the :doc: `Rate Limiter component </rate_limiter >` needs to be
1496
1496
installed in your application::
1497
1497
1498
- use Symfony\Component\HttpClient\HttpClient;
1499
- use Symfony\Component\HttpClient\ThrottlingHttpClient;
1500
- use Symfony\Component\RateLimiter\LimiterInterface;
1498
+ .. configuration-block ::
1501
1499
1502
- $rateLimiter = ...; // $rateLimiter is an instance of Symfony\Component\RateLimiter\LimiterInterface
1503
- $client = HttpClient::create();
1504
- $client = new ThrottlingHttpClient($client, $rateLimiter);
1500
+ .. code-block :: yaml
1505
1501
1506
- $requests = [];
1507
- for ($i = 0; $i < 100; $i++) {
1508
- $requests[] = $client->request('GET', 'https://example.com');
1509
- }
1502
+ # config/packages/framework.yaml
1503
+ framework :
1504
+ http_client :
1505
+ scoped_clients :
1506
+ symfony.client :
1507
+ base_uri : ' https://symfony.com'
1508
+ rate_limiter : ' http_symfony_termsofuse'
1510
1509
1511
- foreach ($requests as $request) {
1512
- // Depending on rate limiting policy, calls will be delayed
1513
- $output->writeln($request->getContent());
1514
- }
1510
+ rate_limiter :
1511
+ # Don't send more than 10 requests in 5 seconds
1512
+ http_symfony_termsofuse :
1513
+ policy : ' token_bucket'
1514
+ limit : 10
1515
+ rate : { interval: '5 seconds', amount: 10 }
1516
+
1517
+ .. code-block :: xml
1518
+
1519
+ <!-- config/packages/framework.xml -->
1520
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1521
+ <container xmlns =" http://symfony.com/schema/dic/services"
1522
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1523
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
1524
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1525
+ https://symfony.com/schema/dic/services/services-1.0.xsd
1526
+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
1527
+
1528
+ <framework : config >
1529
+ <framework : http-client >
1530
+ <framework : scoped-client name =" symfony.client"
1531
+ base-uri =" https://symfony.com"
1532
+ rate-limiter =" http_symfony_termsofuse"
1533
+ />
1534
+ </framework : http-client >
1535
+
1536
+ <framework : rate-limiter >
1537
+ <!-- Don't send more than 10 requests in 5 seconds -->
1538
+ <framework : limiter name =" http_symfony_termsofuse"
1539
+ policy =" token_bucket"
1540
+ limit =" 10"
1541
+ >
1542
+ <framework : rate interval =" 5 seconds" amout =" 10" />
1543
+ </framework : limiter >
1544
+ </framework : rate-limiter >
1545
+ </framework : config >
1546
+ </container >
1547
+
1548
+ .. code-block :: php
1549
+
1550
+ // config/packages/framework.php
1551
+ use Symfony\Config\FrameworkConfig;
1552
+
1553
+ return static function (FrameworkConfig $framework): void {
1554
+ $framework->httpClient()->scopedClient('symfony.client')
1555
+ ->baseUri('https://symfony.com')
1556
+ ->rateLimiter('http_symfony_termsofuse');
1557
+ // ...
1558
+ ;
1559
+
1560
+ $framework->rateLimiter()
1561
+ // Don't send more than 10 requests in 5 seconds
1562
+ ->limiter('http_symfony_termsofuse')
1563
+ ->policy('token_bucket')
1564
+ ->limit(10)
1565
+ ->rate()
1566
+ ->interval('5 seconds')
1567
+ ->amount(10)
1568
+ ;
1569
+ };
1570
+
1571
+ .. code-block :: php-standalone
1572
+
1573
+ use Symfony\Component\HttpClient\HttpClient;
1574
+ use Symfony\Component\HttpClient\ThrottlingHttpClient;
1575
+ use Symfony\Component\RateLimiter\LimiterInterface;
1576
+
1577
+ $rateLimiter = ...; // $rateLimiter is an instance of Symfony\Component\RateLimiter\LimiterInterface
1578
+ $client = HttpClient::create();
1579
+ $client = new ScopingHttpClient($client, [
1580
+ 'base_uri' => 'https://symfony.com',
1581
+ ]);
1582
+ $client = new ThrottlingHttpClient($client, $rateLimiter);
1583
+
1584
+ $requests = [];
1585
+ for ($i = 0; $i < 100; $i++) {
1586
+ $requests[] = $client->request('GET', 'https://symfony.com');
1587
+ }
1588
+
1589
+ foreach ($requests as $request) {
1590
+ // Depending on rate limiting policy, calls will be delayed
1591
+ $output->writeln($request->getContent());
1592
+ }
1515
1593
1516
1594
.. versionadded :: 7.1
1517
1595
0 commit comments