Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/ThrottlingHttpClient.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient;
13
14 use Symfony\Component\RateLimiter\LimiterInterface;
15 use Symfony\Contracts\HttpClient\HttpClientInterface;
16 use Symfony\Contracts\HttpClient\ResponseInterface;
17 use Symfony\Contracts\Service\ResetInterface;
18
19 /**
20 * Limits the number of requests within a certain period.
21 */
22 class ThrottlingHttpClient implements HttpClientInterface, ResetInterface
23 {
24 use DecoratorTrait {
25 reset as private traitReset;
26 }
27
28 public function __construct(
29 HttpClientInterface $client,
30 private readonly LimiterInterface $rateLimiter,
31 ) {
32 $this->client = $client;
33 }
34
35 public function request(string $method, string $url, array $options = []): ResponseInterface
36 {
37 $response = $this->client->request($method, $url, $options);
38
39 if (0 < $waitDuration = $this->rateLimiter->reserve()->getWaitDuration()) {
40 $response->getInfo('pause_handler')($waitDuration);
41 }
42
43 return $response;
44 }
45
46 public function reset(): void
47 {
48 $this->traitReset();
49 $this->rateLimiter->reset();
50 }
51 }