CoreFiles by mySites.guru
ThrottlingHttpClient.php55 lines, 1,489 bytes, from Joomla_6.2.1-Stable-Full_Package.zip
Raw History Compare with 6.2.0
md5
032271a7c9962cac046b686cc7242116
sha1
3632373a744c57e2a22c19c72e960da2e2b0f927
sha256
2378780b9e01ea3f061a5552c2d0e9adca6926916db6a9d47bb61b834fc3ab4e
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpClient;

use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
 * Limits the number of requests within a certain period.
 */
class ThrottlingHttpClient implements HttpClientInterface, ResetInterface
{
    use DecoratorTrait {
        reset as private traitReset;
    }

    public function __construct(
        HttpClientInterface $client,
        private readonly LimiterInterface $rateLimiter,
    ) {
        $this->client = $client;
    }

    public function request(string $method, string $url, array $options = []): ResponseInterface
    {
        $response = $this->client->request($method, $url, $options);

        if (0 < $waitDuration = $this->rateLimiter->reserve()->getWaitDuration()) {
            if (\is_callable($pause = $response->getInfo('pause_handler'))) {
                $pause($waitDuration);
            } else {
                usleep((int) (1E6 * $waitDuration));
            }
        }

        return $response;
    }

    public function reset(): void
    {
        $this->traitReset();
        $this->rateLimiter->reset();
    }
}

The same content is in 3 releases, Joomla 6.2.1-rc1 to 6.3.0-alpha1. See them all, or every version of this file.