海运的博客

PHP函数pfsockopen建立Keep-Alive连接

发布时间:January 1, 2014 // 分类:PHP // No Comments

使用Keep-Alive可减少建立TCP连接的开销,pfsockopen在脚本执行期间可重用socket实现Keep-Alive。

<?php
   function httpget($host)   
   {   
      $conn = pfsockopen($host,80,$errno, $errstr, 30);   
      if (!$conn)    
      {   
         echo "$errstr ($errno)<br />\n";   
         return;   
      }   

      $header = "HEAD / HTTP/1.1\r\n";   
      $header.= "Host: {$host}\r\n";   
      $header.= "Connection: Keep-Alive\r\n\r\n";    

      fwrite($conn,$header);   

      /*
      while (!feof($conn)) {   
         $resp .= fgets($conn);   
      }
      */

      //fclose($conn); //关闭连接后再连接keepalive失效
      return $resp;   
   }   
   $host = 'www.haiyun.me';   
   $count = 2;   

   for ($i = 0; $i < $count; $i++) {   
      echo httpget($host);
      sleep(10);
   }   
?>

Tcpdump分析,1-3行第一次建立TCP连接,第8行建立第二次连接跳过TCP三次握手。

20:27:17.016435 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [S], seq 2231661196, win 5840, options [mss 1400,sackOK,TS val 9961058 ecr 0,nop,wscale 6], length 0
20:27:17.016531 IP 162.211.225.71.80 > 1.2.3.4.49022: Flags [S.], seq 2734092091, ack 2231661197, win 14480, options [mss 1460,sackOK,TS val 1880475065 ecr 9961058,nop,wscale 7], length 0
20:27:17.342653 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [.], ack 1, win 92, options [nop,nop,TS val 9961139 ecr 1880475065], length 0
20:27:17.345695 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [P.], seq 1:65, ack 1, win 92, options [nop,nop,TS val 9961139 ecr 1880475065], length 64
20:27:17.345716 IP 162.211.225.71.80 > 1.2.3.4.49022: Flags [.], ack 65, win 114, options [nop,nop,TS val 1880475394 ecr 9961139], length 0
20:27:17.876911 IP 162.211.225.71.80 > 1.2.3.4.49022: Flags [P.], seq 1:427, ack 65, win 114, options [nop,nop,TS val 1880475925 ecr 9961139], length 426
20:27:18.202503 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [.], ack 427, win 108, options [nop,nop,TS val 9961355 ecr 1880475925], length 0
20:27:27.346477 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [P.], seq 65:129, ack 427, win 108, options [nop,nop,TS val 9963640 ecr 1880475925], length 64
20:27:27.346508 IP 162.211.225.71.80 > 1.2.3.4.49022: Flags [.], ack 129, win 114, options [nop,nop,TS val 1880485395 ecr 9963640], length 0
20:27:27.838571 IP 162.211.225.71.80 > 1.2.3.4.49022: Flags [P.], seq 427:853, ack 129, win 114, options [nop,nop,TS val 1880485887 ecr 9963640], length 426
20:27:28.165367 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [.], ack 853, win 125, options [nop,nop,TS val 9963845 ecr 1880485887], length 0
20:27:37.346030 IP 1.2.3.4.49022 > 162.211.225.71.80: Flags [R.], seq 129, ack 853, win 125, options [nop,nop,TS val 9966140 ecr 1880485887], length 0

PHP异步执行

发布时间:December 31, 2013 // 分类:PHP // No Comments

<?php
   $start = microtime(true); 
   $fp=fsockopen('localhost',80,$errno,$errstr,5);
   if(!$fp){
      echo "$errstr ($errno)<br />\n";
   }
   $head = "GET /get.php HTTP/1.1 \r\n";
   $head .= "Host: localhost \r\n";
   $head .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 \r\n";
   $head .= "Connection: Close \r\n\r\n";
   //发送请求
   fputs($fp,$head);
   /* 忽略结果
   while(!feof($fp)) {
      echo fgets($fp,128);
   }
   */
   fclose($fp);
   header("Content-Type: text/html;charset=utf-8");
   $end = microtime(true); 
   echo "脚本执行时间".($end - $start).'<br>';  
?>

异步执行的代码:

<?php
   ignore_user_abort(1); //由于异步请求后会立即断开连接,默认会终止脚本,忽略掉
   set_time_limit(0); //取消脚本执行延时上限
   for ($i = 1; $i < 10; $i++)
   {
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, "https://www.haiyun.me/?$i");
      curl_setopt($ch, CURLOPT_REFERER, "http://www.test.com");
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0");
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
      curl_setopt($ch,CURLOPT_TIMEOUT,3);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_exec($ch);
   }
?>

PHP查看脚本占用内存

发布时间:December 31, 2013 // 分类:PHP // No Comments

<?php
   function convert($size)
   {
      $unit=array('b','kb','mb','gb','tb','pb');
      return @round($size/pow(1024,($i=floor(log($size,1024)))),2).$unit[$i];
   }

   echo convert(memory_get_usage(true)); 
?>

Debian安装RPM软件

发布时间:December 31, 2013 // 分类:Debian // No Comments

1.使用alien将rpm转换为deb格式安装:

apt-get install alien 
alien pkg.rpm
dpkg -i pkg.deb

2.使用rpm管理器安装:

aptitude install rpm
rpm -i pkg.rpm

Debian7安装队列服务HTTPSQS

发布时间:December 31, 2013 // 分类:消息队列 // No Comments

aptitude install gcc make zlib1g-dev libbz2-dev
yum install gcc zlib-devel bzip2-devel
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar zxvf libevent-2.0.21-stable.tar.gz 
cd libevent-2.0.21-stable/
./configure --prefix=/usr/local/libevent-2.0.21-stable/
make && make install
cd ../

wget http://fallabs.com/tokyocabinet/tokyocabinet-1.4.48.tar.gz
tar zxvf tokyocabinet-1.4.48.tar.gz
cd tokyocabinet-1.4.48/
./configure --prefix=/usr/local/tokyocabinet-1.4.48/
make && make install
cd ../

wget http://httpsqs.googlecode.com/files/httpsqs-1.7.tar.gz
tar zxvf httpsqs-1.7.tar.gz
cd httpsqs-1.7/
sed -i 's/12/21/g' Makefile
sed -i 's/47/48/g' Makefile
make && make install

修改源码添加出多个队列支持:

/* 出多个队列 */
else if (strcmp(httpsqs_input_opt, "mget") == 0 && httpsqs_input_num >= 0 && httpsqs_input_num <= 10000) 
{
    int i;
    for( i=0; i<httpsqs_input_num; i++ )
    {
        int queue_get_value = 0;
        queue_get_value = httpsqs_now_getpos((char *)httpsqs_input_name);
        if (queue_get_value == 0) {
            evbuffer_add_printf(buf, "%s", "HTTPSQS_GET_END");
            break;
        } else {
            char queue_name[300] = {0}; /* 队列名称的总长度,用户输入的队列长度少于256字节 */
            sprintf(queue_name, "%s:%d", httpsqs_input_name, queue_get_value);
            char *httpsqs_output_value;
            httpsqs_output_value = tcbdbget2(httpsqs_db_tcbdb, queue_name);
            if (httpsqs_output_value) {
                memset(queue_name, '\0', 300);
                sprintf(queue_name, "%d", queue_get_value);    
                evhttp_add_header(req->output_headers, "Pos", queue_name);
                evbuffer_add_printf(buf, "%s\n", httpsqs_output_value);
                free(httpsqs_output_value);
            } else {
                evbuffer_add_printf(buf, "%s", "HTTPSQS_GET_END");
                break;
            }
        }
    }
}

源码分析:

将队列读取点和写入点保存为key:queue:getpos/putpos的值,队列内容key为queue:1/queue:2形式
通过httpsqs_read_putpos获取写入/读取点值
读取队列内容时httpsqs_now_read_pos对值判断及更新值,对key:queue:+1
通过tcbdbput2/tcbdget2写入/读取队列内容

更多:http://blog.s135.com/httpsqs/

分类
最新文章
最近回复
  • opnfense: 谢谢博主!!!解决问题了!!!我之前一直以为内置的odhcp6就是唯一管理ipv6的方式
  • liyk: 这个方法获取的IPv6大概20分钟之后就会失效,默认路由先消失,然后Global IPV6再消失
  • 海运: 不好意思,没有。
  • zongboa: 您好,請問一下有immortalwrt設定guest Wi-Fi的GUI教學嗎?感謝您。
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
归档