EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
http client parallel requests
Yurii2490408dfdd0
diff --git a/application/Espo/Core/HttpClient/Client.php b/application/Espo/Core/HttpClient/Client.phpindex af3e640bc8..364b33cb0d 100644--- a/application/Espo/Core/HttpClient/Client.php+++ b/application/Espo/Core/HttpClient/Client.php@@ -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);}}diff --git a/application/Espo/Core/HttpClient/Promise.php b/application/Espo/Core/HttpClient/Promise.phpnew file mode 100644index 0000000000..99de0e0218--- /dev/null+++ b/application/Espo/Core/HttpClient/Promise.php@@ -0,0 +1,83 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\HttpClient;++use Closure;+use Espo\Core\HttpClient\Exceptions\ConnectException;+use Espo\Core\HttpClient\Exceptions\TooManyRedirectsException;+use GuzzleHttp\Promise\PromiseInterface;+use GuzzleHttp;+use Psr\Http\Message\ResponseInterface;+use RuntimeException;+use UnexpectedValueException;++class Promise+{+ /**+ * @internal+ */+ public function __construct(+ private PromiseInterface $promise,+ ) {}++ /**+ * @throws TooManyRedirectsException+ * @throws ConnectException+ * @internal+ */+ public function wait(): ResponseInterface+ {+ $value = $this->requestCallback(fn () => $this->promise->wait());++ if (!$value instanceof ResponseInterface) {+ throw new UnexpectedValueException();+ }++ return $value;+ }++ /**+ * @throws TooManyRedirectsException+ * @throws ConnectException+ * @noinspection PhpRedundantCatchClauseInspection+ */+ private function requestCallback(Closure $closure): mixed+ {+ try {+ return $closure();+ } 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);+ }+ }+}diff --git a/application/Espo/Core/HttpClient/PromiseUtil.php b/application/Espo/Core/HttpClient/PromiseUtil.phpnew file mode 100644index 0000000000..1162a290b2--- /dev/null+++ b/application/Espo/Core/HttpClient/PromiseUtil.php@@ -0,0 +1,52 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\HttpClient;++use Espo\Core\HttpClient\Exceptions\ConnectException;+use Espo\Core\HttpClient\Exceptions\TooManyRedirectsException;+use Psr\Http\Message\ResponseInterface;++/**+ * @since 9.4.0+ * @noinspection PhpUnused+ */+class PromiseUtil+{+ /**+ * @param array<int|string, Promise> $promises+ * @return array<int|string, ResponseInterface>+ * @throws ConnectException+ * @throws TooManyRedirectsException+ */+ public static function unwrap(array $promises): array+ {+ return array_map(fn ($promise) => $promise->wait(), $promises);+ }+}diff --git a/application/Espo/Core/HttpClient/Util.php b/application/Espo/Core/HttpClient/Util.phpindex 5571f0a598..3fbbdec37d 100644--- a/application/Espo/Core/HttpClient/Util.php+++ b/application/Espo/Core/HttpClient/Util.php@@ -29,7 +29,9 @@namespace Espo\Core\HttpClient;+use Espo\Core\HttpClient\Exceptions\ConnectException;use GuzzleHttp\Psr7\Utils;+use GuzzleHttp;use Psr\Http\Message\StreamInterface;class Util@@ -77,4 +79,21 @@ class Utilreturn in_array($address, $addressList);}++ /**+ * @internal+ * @throws ConnectException+ */+ public static function handleConnectException(GuzzleHttp\Exception\ConnectException $exception): never+ {+ $context = $exception->getHandlerContext();++ $reason = null;++ if (($context['errno'] ?? 0) === CURLE_OPERATION_TIMEDOUT) {+ $reason = ConnectErrorReason::Timeout;+ }++ throw ConnectException::create(previous: $exception, reason: $reason);+ }}