Throw question
Some time ago, a big guy said that PHP redis's pconnect can't realize long links. After the request ends, the connection is released. I have doubts in my mind. If so, why does PHP redis have connect and pconnect?
Search document
The connection will not be closed on end of request until the php process ends. Therefore, in combination with the operation mechanism of fpm, the connection of redis will be released only after the current subprocess finishes processing the specified number of Max requests and the subprocess restarts
There is no proof for empty talk, and the experiment is the evidence
The experiment relies on the tideways extension. How can I use this extension to refer to another blog of mine: https://blog.csdn.net/why444216978/article/details/103365063
Test code:
<?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('10.10.6.5', 6379); $redis->set('why', 1); $redis->get('why'); tideways(false, substr(basename(__FILE__), 0, -4) ); ?>
First, request twice. It is found that only the first time there is a pconnect function call, and the second time there is no pconnect function call at the beginning of the request:
[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
Then we restart fpm again. After the simulation process finishes executing the maximum number of requests, it is found that pconnect method is called for the first request:
[why@localhost /tmp]$ps -ef | grep php-fpm 0 9715 1 0 3:39 Afternoon ?? 0:00.01 php-fpm -2 9716 9715 0 3:39 Afternoon ?? 0:00.01 php-fpm -2 9717 9715 0 3:39 Afternoon ?? 0:00.01 php-fpm 501 9732 648 0 3:42 Afternoon 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 Afternoon 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 Afternoon ?? 0:00.00 php-fpm -2 9740 9739 0 3:42 Afternoon ?? 0:00.00 php-fpm -2 9741 9739 0 3:42 Afternoon ?? 0:00.00 php-fpm 501 9743 648 0 3:42 Afternoon 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]$
conclusion
The long link established by pconnect of PHP redis will only be released when the subprocess is restarted.
Finally, I feel shallow on the paper. I never know that I have to do it myself. After reading it, I think it will help me to give a praise. If I can give another attention, it will be better!!!