可用于實現純異步PHP程序:php_http_parser

jopen 10年前發布 | 13K 次閱讀 PHP開發

php_http_parser 是基于node.js http-parser的PHP擴展,可用于實現純異步PHP程序

libcurl提供了異步調用方式,有兩種風格:

  • ONE MULTI HANDLE MANY EASY HANDLES:加入多個easy handle后執行curl_multi_perform方法。該方法在php curl擴展中有對應實現。但最后一步curl_multi_perform是阻塞的。

  • MULTI_SOCKET,這個是真正的非阻塞方法,但需要自行實現event loop,且封裝較為困難,目前在php中沒有對應實現。經過調研,curl_multi_socket_action跟php內核結合困難度很高。

除此之外,基本上沒有真正的實現異步http請求的php擴展。目前僅有部分純php實現的版本,比如tsf中的http client實現。 使用純php實現的問題主要受限于http解析的性能。因此考慮將這一模塊用擴展的方式來實現。node.js http-parser就是一個很好的c語言的http解析庫。 php_http_parser就是對其做的一個封裝,在php中暴露出相應的接口。

為了實現真正的非阻塞請求,仍然需要自己實現event loop。目前推薦結合swoole使用,以獲得更好的性能。

使用方式

$buffs = array("HTTP/1.1 301 Moved Permanently\r\n"
,"Location: http://www.google.com/\r\n"
,"Content-Type: text/html; charset=UTF-8\r\n"
,"Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"
,"Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"
,"Cache-Control: public, max-age=2592000\r\n"
,"Server: gws\r\n"
,"Content-Length: 193\r\n"
,"\r\n"
,"<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
,"<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
,"<H1>301 Moved</H1>\n"
,"The document has moved\n"
,"<A HREF=\"http://www.google.com/\">here</A>.\r\n"
    ,"<A HREF=\"http://www.google.com/\">here</A>.\r\n"
    ,"<A HREF=\"http://www.google.com/\">here</A>.\r\n"
    ,"<A HREF=\"http://www.google.com/\">here</A>.\r\n"
    ,"<A HREF=\"http://www.google.com/\">here</A>.\r\n"
,"</BODY></HTML>\r\n");
$hp = new HttpParser();
foreach($buffs as $buff){
    $ret = $hp->execute($buff);
    if($ret !== false){
        echo $ret;
        break;
    }
}

雖然http請求可能分包發送,HttpParser會將所有包合并在一起后,出發body事件,然后調用相應的回調方法。 諸如header回調,目前暫未實現。另外,此處需要自行實現timeout邏輯。

示例代碼是結合swoole_client與swPromise框架實現的一個異步http client。 籍此可以實現真正的非阻塞的PHP程序。

class HttpClientFuture implements FutureIntf {
    protected $url = null;
    protected $post = null;
    protected $proxy = false;
    public function __construct($url, $post = array(), $proxy = array()) {
        $this->url = $url;
        $this->post = $post;
        if($proxy){
            $this->proxy = $proxy;
        }
    }
    public function run(Promise &$promise) {
        $cli = new \swoole_client ( SWOOLE_TCP, SWOOLE_SOCK_ASYNC );
        $urlInfo = parse_url ( $this->url );
        if(!isset($urlInfo ['port']))$urlInfo ['port'] = 80;
        $httpParser = new \HttpParser();
        $cli->on ( "connect", function ($cli)use($urlInfo){
            $host = $urlInfo['host'];
            if($urlInfo['port'])$host .= ':'.$urlInfo['port'];
            $req = array();
            $req[] = "GET {$this->url} HTTP/1.1\r\n";
            $req[] = "User-Agent: PHP swAsync\r\n";
            $req[] = "Host:{$host}\r\n";
            $req[] = "Connection:close\r\n";
            $req[] = "\r\n";
            $req = implode('', $req);
            $cli->send ( $req );
        } );
        $cli->on ( "receive", function ($cli, $data = "") use(&$httpParser, &$promise) {
            $ret = $httpParser->execute($data);
            if($ret !== false){
                $cli->close();
                $promise->accept(['http_data'=>$ret]);
            }
        } );
        $cli->on ( "error", function ($cli) use(&$promise) {
            $promise->reject ();
        } );
        $cli->on ( "close", function ($cli) {
        } );
        if($this->proxy){
            $cli->connect ( $this->proxy['host'], $this->proxy ['port'], 1 );
        }else{
            $cli->connect ( $urlInfo ['host'], $urlInfo ['port'], 1 );
        }
    }
}

性能

[web@gz-web01 php_http_parser]$ time /data/server/php/bin/php http_parser.php  2000000

real    0m11.489s
user    0m11.435s
sys 0m0.017s

1個worker進程

./http_load -fetches 20000 -parallel 100 9502.listasync 
20000 fetches, 100 max parallel, 2.02e+06 bytes, in 5.94536 seconds
101 mean bytes/connection
3363.97 fetches/sec, 339761 bytes/sec
msecs/connect: 0.0473873 mean, 1.155 max, 0.019 min
msecs/first-response: 29.6366 mean, 51.736 max, 15.22 min
HTTP response codes:
code 200 -- 20000
-bash: history: write error: Success

2個worker進程

./http_load -fetches 20000 -parallel 100 9502.listasync 
20000 fetches, 100 max parallel, 2.02e+06 bytes, in 3.17119 seconds
101 mean bytes/connection
6306.77 fetches/sec, 636984 bytes/sec
msecs/connect: 0.0643583 mean, 1.211 max, 0.023 min
msecs/first-response: 15.7489 mean, 32.425 max, 3.242 min
HTTP response codes:
code 200 -- 20000
-bash: history: write error: Success

4個woker進程

./http_load -fetches 20000 -parallel 100 9502.listasync 
20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.57194 seconds
101 mean bytes/connection
12723.2 fetches/sec, 1.28504e+06 bytes/sec
msecs/connect: 0.0815263 mean, 1.349 max, 0.02 min
msecs/first-response: 7.65904 mean, 22.568 max, 1.221 min
HTTP response codes:
code 200 -- 20000
-bash: history: write error: Success

8個woker進程

./http_load -fetches 20000 -parallel 100 9502.listasync 
20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.02967 seconds
101 mean bytes/connection
19423.8 fetches/sec, 1.9618e+06 bytes/sec
msecs/connect: 0.147502 mean, 1.575 max, 0.014 min
msecs/first-response: 3.17218 mean, 22.566 max, 0.339 min
HTTP response codes:
code 200 -- 20000
-bash: history: write error: Success

官方網站:http://www.baiduhome.net/lib/view/home/1451808838620

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!