關於php-redis的pconnect長鏈接的思考

拋出問題

前段時間有個大佬說,php-redis的pconnect並不能實現長鏈接,在請求結束後連接就被釋放掉了,我心中存疑,如果是這樣的話,那php-reds爲什麼還要有connect和pconnect?

查找文檔

php-redis官方文檔中有這樣一句話:The connection will not be closed on end of request until the php process ends,意思就是隻有在進程結束後纔會被釋放。所以結合fpm的運行機制,只有在當前子進程處理完配置規定的max_requests請求次數之後,隨着子進程的重啓,纔會釋放redis的連接

空說無憑,實驗爲證

實驗依賴tideways擴展,這個擴展怎麼用參考我的另一篇博客:https://blog.csdn.net/why444216978/article/details/103365063

測試代碼:

<?php

function &tideways($begin = true, $file = '', $dir = '/tmp/')
{
    static $data = [];
    if ($begin){
        extension_loaded('tideways_xhprof');
        tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_CPU | TIDEWAYS_XHPROF_FLAGS_MEMORY);
        $data['begin'] = microtime(true);
    }else{
        $data['end'] = microtime(true);
        $dir  = sprintf('%s%s-%s.log',  $dir, $file, date('Y-m-d', time()));
        $content = json_encode(tideways_xhprof_disable());
        file_put_contents($dir, $content);
    }

    return $data;
}

tideways(true);
    $redis = new Redis();
    $con = $redis->pconnect('127.0.0.1', 6379);
    $redis->set('why', 1);
    $redis->get('why');

tideways(false, substr(basename(__FILE__), 0, -4) );
?>

首先請求2次,發現只有第一次有pconnect函數調用,第二次請求開始就沒有pconnect的函數調用了:

[why@localhost /tmp]$toolkit analyze-xhprof why-2020-03-12.log 
Showing XHProf data by Exclusive Wall-Time
+-----------------+-------+-----------+------------------------------+
|    FUNCTION     | COUNT | WALL-TIME | EXCL  WALL-TIME (>= 0.75 MS) |
+-----------------+-------+-----------+------------------------------+
| Redis::pconnect |     1 | 7.54 ms   | 7.54 ms                      |
| Redis::get      |     1 | 6.32 ms   | 6.32 ms                      |
| Redis::set      |     1 | 6.21 ms   | 6.21 ms                      |
+-----------------+-------+-----------+------------------------------+
Looking for a Web UI and SQL Profiling Support? Try our SaaS: https://tideways.io
[why@localhost /tmp]$
[why@localhost /tmp]$toolkit analyze-xhprof why-2020-03-12.log 
Showing XHProf data by Exclusive Wall-Time
+------------+-------+-----------+------------------------------+
|  FUNCTION  | COUNT | WALL-TIME | EXCL  WALL-TIME (>= 1.02 MS) |
+------------+-------+-----------+------------------------------+
| Redis::set |     1 | 10.20 ms  | 10.20 ms                     |
| Redis::get |     1 | 6.67 ms   | 6.67 ms                      |
+------------+-------+-----------+------------------------------+
Looking for a Web UI and SQL Profiling Support? Try our SaaS: https://tideways.io

然後我們再重啓fpm,模擬進程執行完最大請求數後重啓,發現第一次請求又調用了pconnect方法:

[why@localhost /tmp]$ps -ef | grep php-fpm
    0  9715     1   0  3:39下午 ??         0:00.01 php-fpm
   -2  9716  9715   0  3:39下午 ??         0:00.01 php-fpm
   -2  9717  9715   0  3:39下午 ??         0:00.01 php-fpm
  501  9732   648   0  3:42下午 ttys001    0:00.01 grep php-fpm
[why@localhost /tmp]$sudo kill 9715
[why@localhost /tmp]$ps -ef |grep php-fpm
  501  9736   648   0  3:42下午 ttys001    0:00.00 grep php-fpm
[why@localhost /tmp]$sudo php-fpm
[why@localhost /tmp]$ps -ef | grep php-fpm
    0  9739     1   0  3:42下午 ??         0:00.00 php-fpm
   -2  9740  9739   0  3:42下午 ??         0:00.00 php-fpm
   -2  9741  9739   0  3:42下午 ??         0:00.00 php-fpm
  501  9743   648   0  3:42下午 ttys001    0:00.00 grep php-fpm
[why@localhost /tmp]$
[why@localhost /tmp]$toolkit analyze-xhprof why-2020-03-12.log 
Showing XHProf data by Exclusive Wall-Time
+-----------------+-------+-----------+------------------------------+
|    FUNCTION     | COUNT | WALL-TIME | EXCL  WALL-TIME (>= 1.03 MS) |
+-----------------+-------+-----------+------------------------------+
| Redis::pconnect |     1 | 10.33 ms  | 10.33 ms                     |
| Redis::get      |     1 | 7.89 ms   | 7.89 ms                      |
| Redis::set      |     1 | 6.87 ms   | 6.87 ms                      |
+-----------------+-------+-----------+------------------------------+
Looking for a Web UI and SQL Profiling Support? Try our SaaS: https://tideways.io
[why@localhost /tmp]$

結論

php-redis的pconnect建立的長鏈接,只有在子進程重啓時纔會釋放鏈接。

 

紙上得來終覺淺,絕知此事要躬行,看完了覺得有幫助辛苦給個贊,如果能再給個關注那再好不過了!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章