带有代理的PHP CURL在套接字上导致CLOSE_WAIT [英] PHP CURL with proxy causes CLOSE_WAIT on socket

查看:108
本文介绍了带有代理的PHP CURL在套接字上导致CLOSE_WAIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP curl库来建立连接并从WEB中检索内容-通常。

I'm using PHP curl library to establish connection and retrieve content from WEB - the usual.

我有多个SOCKS5代理服务器在本地主机上运行,​​端口从10300开始到10350,PHP随机选择一个端口。

I have multiple SOCKS5 proxy servers running on localhost on ports from 10300 to 10350 and PHP selects a port randomly.

我的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "localhost:".mt_rand(10300, 10350));
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_resp = curl_exec($ch);
curl_close($ch);
/// ... whatever logic

但是如果我将此代码放入循环很长一段时间,我收到错误消息,提示无法打开更多文件。我在网上阅读这是因为达到了文件处理程序限制,所以我用 lsof -p 进行了调试,我注意到该过程有成百上千的这些行:

But if I put this code into loop for long time, I get error telling that no more files can be opened. I read online that is because filehandler limit being reached, so I debugged with lsof -p and I noticed that the process has hundreds and even thousands of these lines:

... TCP localhost:43030->localhost:10303 (CLOSE_WAIT)
... TCP localhost:40982->localhost:10341 (CLOSE_WAIT)
... TCP localhost:48718->localhost:10304 (CLOSE_WAIT)
... TCP localhost:41655->localhost:10350 (CLOSE_WAIT)
... TCP localhost:41915->localhost:10310 (CLOSE_WAIT)
... TCP localhost:49746->localhost:10322 (ESTABLISHED)

我正在研究为什么这样做,但我不明白,因为我在呼叫 curl_close 并我以为CURL也将关闭PROXY连接,还是我缺少什么?

I was researching why is this that way, but I don't understand, because I am calling curl_close and I would assume that CURL would close PROXY connection as well, or am I missing something?

版本:

PHP 7.1.4-1+deb.sury.org~trusty+1 (cli) (built: Apr 11 2017 22:45:20) (NTS)
curl 7.52.1 (x86_64-pc-linux-gnu) libcurl/7.52.1 OpenSSL/1.0.1f zlib/1.2.8 libidn2/0.9 libpsl/0.11.0 (+libicu/52.1) libssh2/1.4.3 nghttp2/1.19.0 librtmp/2.3


推荐答案

我也遇到了这个问题。解决方法是添加

I also came across this issue. The work around is add

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Connection: close"));

起初我以为与php7有关,因为我可以在php7盒上重现它,但是在php5上,它不会发生。不幸的是,我在另一个php7盒上尝试过,也没有再发生。因此,我仍然对根本原因感到困惑。

At first I thought is was related with php7, because i can reproduce it on a php7 box, but on php5, it will not happen. Unfortunately, I tried this on another php7 box, it also did not happen again. So I am still confused about the root cause.

这篇关于带有代理的PHP CURL在套接字上导致CLOSE_WAIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆