http client parallel requests

This commit is contained in:
Yurii
2026-03-17 13:22:13 +02:00
parent 98a8ca9449
commit 2490408dfd
4 changed files with 203 additions and 30 deletions

View File

@@ -61,6 +61,50 @@ class Client
* @throws ConnectException
*/
public function send(RequestInterface $request): ResponseInterface
{
$client = $this->prepareGuzzleClient();
try {
return $client->send($request);
} catch (GuzzleHttp\Exception\ConnectException $e) {
Util::handleConnectException($e);
} catch (GuzzleHttp\Exception\TooManyRedirectsException $e) {
throw new TooManyRedirectsException(previous: $e);
} catch (GuzzleHttp\Exception\GuzzleException $e) {
throw new RuntimeException(previous: $e);
}
}
/**
* Send a request in async.
*/
public function sendAsync(RequestInterface $request): Promise
{
$client = $this->prepareGuzzleClient();
$promise = $client->sendAsync($request);
return new Promise($promise);
}
/**
* @param string[] $allowed
* @throws NotAllowedInternalHost
*/
private function checkUrl(string $url, array $allowed): void
{
if (
!Util::matchUrlToAddressList($url, $allowed) &&
!$this->urlCheck->isNotInternalUrl($url)
) {
throw new NotAllowedInternalHost("Not allowed internal host in '$url'.");
}
}
/**
* @return array<string, mixed>
*/
private function prepareOptions(): array
{
$options = [
'protocols' => array_map(
@@ -106,38 +150,13 @@ class Client
$options['handler'] = $stack;
}
$client = new GuzzleHttp\Client($options);
try {
return $client->send($request);
} catch (GuzzleHttp\Exception\ConnectException $e) {
$context = $e->getHandlerContext();
$reason = null;
if (($context['errno'] ?? 0) === CURLE_OPERATION_TIMEDOUT) {
$reason = ConnectErrorReason::Timeout;
}
throw ConnectException::create(previous: $e, reason: $reason);
} catch (GuzzleHttp\Exception\TooManyRedirectsException $e) {
throw new TooManyRedirectsException(previous: $e);
} catch (GuzzleHttp\Exception\GuzzleException $e) {
throw new RuntimeException(previous: $e);
}
return $options;
}
/**
* @param string[] $allowed
* @throws NotAllowedInternalHost
*/
private function checkUrl(string $url, array $allowed): void
private function prepareGuzzleClient(): GuzzleHttp\Client
{
if (
!Util::matchUrlToAddressList($url, $allowed) &&
!$this->urlCheck->isNotInternalUrl($url)
) {
throw new NotAllowedInternalHost("Not allowed internal host in '$url'.");
}
$options = $this->prepareOptions();
return new GuzzleHttp\Client($options);
}
}