Guzzle send() 方法导致 cURL 错误 35 打开的文件太多

Guzzle send() method causes cURL error 35 Too many open files

正在尝试使用 Guzzle 5 执行以下代码。

$client = new GuzzleClient(['defaults/headers/User-Agent' => static::$userAgentString]);

$request = $client->createRequest(static::$serviceRequestMethod, $url, $options); // Create signing request.

$signature = new Signature\Signature($this->accessKey, $this->secretKey);

$options = array_merge_recursive($options, ['query' => ['Signature' => $signature->signString($hash)]]);

$request = $client->createRequest(static::$serviceRequestMethod, $url, $options); // Create real request.

$response = $client->send($request);

当我在较长的 运行 CLI 进程中多次调用此行时,我得到以下错误,可追溯到行 $response = $client->send($request);

cURL error 35: error:02001018:system library:fopen:Too many open files

点击之后,服务器上的所有其他网页和命令都会出现同样的 "too many open files" 错误。

这是堆栈跟踪:

#0 /home/vagrant/code/example.com/vendor/guzzlehttp/guzzle/src/RequestFsm.php(104): GuzzleHttp\Exception\RequestException::wrapException(Object(GuzzleHttp\Message\Request), Object(GuzzleHttp\Ring\Exception\ConnectException))
#1 /home/vagrant/code/example.com/vendor/guzzlehttp/guzzle/src/RequestFsm.php(132): GuzzleHttp\RequestFsm->__invoke(Object(GuzzleHttp\Transaction))
#2 /home/vagrant/code/example.com/vendor/react/promise/src/FulfilledPromise.php(25): GuzzleHttp\RequestFsm->GuzzleHttp\{closure}(Array)
#3 /home/vagrant/code/example.com/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(Object(Closure), NULL, NULL)
#4 /home/vagrant/code/example.com/vendor/guzzlehttp/guzzle/src/Message/FutureResponse.php(43): GuzzleHttp\Ring\Future\CompletedFutureValue->then(Object(Closure), NULL, NULL)
#5 /home/vagrant/code/example.com/vendor/guzzlehttp/guzzle/src/RequestFsm.php(135): GuzzleHttp\Message\FutureResponse::proxy(Object(GuzzleHttp\Ring\Future\CompletedFutureArray), Object(Closure))
#6 /home/vagrant/code/example.com/vendor/guzzlehttp/guzzle/src/Client.php(165): GuzzleHttp\RequestFsm->__invoke(Object(GuzzleHttp\Transaction))
#7 /home/vagrant/code/example.com/app/library/amazon/src/AWS.php(540): GuzzleHttp\Client->send(Object(GuzzleHttp\Message\Request))

我不知道在通过 Guzzle 发送请求后是否需要显式关闭资源。我在这里遗漏了什么或者这可能是 Guzzle 中的错误?

这不是 Guzzle 或 MailGun 的问题,而是您对库的特定实现。您实际上已经达到了底层操作系统(libcurl、openssl 和 fopen)的极限,因为有如此多的长 运行(打开)请求。

根据 libcurl errors 错误 35 表示 SSL/TLS 握手出错。

根据各种 google 参考错误:02001018 表示 openssl 无法访问(或读取)证书文件。

您可以使用ulimit查看和修改各种系统范围资源的限制。

您还可以使用 lsof 查看打开的文件。

解决您的问题:

  1. (如果可以)增加系统资源限额 - 请务必研究此更改可能产生的影响。
  2. 重构您的代码,以免达到操作环境的限制。也许我可以对某些请求使用异步通信。一个不同的库,或者 "dropping down" 并实现你自己的库。
  3. 找到一些方法来实施某种类型的速率限制(我已将其与 #2 分开列出)但它们可以齐头并进。