使用 PHP Curl 库持久/保持 HTTP 连接? [英] Persistent/keepalive HTTP with the PHP Curl library?

查看:33
本文介绍了使用 PHP Curl 库持久/保持 HTTP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的 PHP 库通过 HTTP 将文档添加到 SOLR 索引.

I'm using a simple PHP library to add documents to a SOLR index, via HTTP.

目前涉及3台服务器:

  1. 运行索引作业的 PHP 框
  2. 一个保存被索引数据的数据库框
  3. solr 盒.

以 80 个文档/秒(在 100 万个文档中),我注意到 PHP 和 solr 盒上的网络接口上的中断率异常高(2000 个/秒;此外,图表几乎相同——当 PHP 框的中断率达到峰值时,它也会在 Solr 框上达到峰值),但数据库框的中断率要低得多(300/秒).我想这仅仅是因为我打开并重用了一个到数据库服务器的连接,但是由于 Solr 客户端库的编写方式,每个 Solr 请求当前都通过 cURL 打开一个新的 HTTP 连接.

At 80 documents/sec (out of 1 million docs), I'm noticing an unusually high interrupt rate on the network interfaces on the PHP and solr boxes (2000/sec; what's more, the graphs are nearly identical -- when the interrupt rate on the PHP box spikes, it also spikes on the Solr box), but much less so on the database box (300/sec). I imagine this is simply because I open and reuse a single connection to the database server, but every single Solr request is currently opening a new HTTP connection via cURL, thanks to the way the Solr client library is written.

所以,我的问题是:

  1. 能否通过 cURL 来打开 keepalive 会话?
  2. 重用连接需要什么?-- 是否像重用 cURL 句柄资源一样简单?
  3. 我需要设置任何特殊的 cURL 选项吗?(例如强制 HTTP 1.1?)
  4. cURL 保持活动连接有什么问题吗?这个脚本一次运行几个小时;我可以使用单个连接,还是需要定期重新连接?

推荐答案

cURL PHP 文档 (curl_setopt) 说:

cURL PHP documentation (curl_setopt) says:

CURLOPT_FORBID_REUSE - TRUE 强制要显式关闭的连接当它完成处理时,和不得汇集以供重复使用.

CURLOPT_FORBID_REUSE - TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.

所以:

  1. 是的,实际上它应该默认重用连接,只要你重用 cURL 句柄.
  2. 默认情况下,cURL 自己处理持久连接;如果您需要一些特殊的标头,请检查 CURLOPT_HTTPHEADER
  3. 服务器可能会发送一个保持活动超时(对于默认的 Apache 安装,它是 15 秒或 100 个请求,以先到者为准) - 但 cURL 会在发生这种情况时打开另一个连接.

这篇关于使用 PHP Curl 库持久/保持 HTTP 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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