Let's talk about the Reload operation in PHP and phpreload.
Preface
Many predecessors warned us that reload can ensure the smoothness of the entire process. The so-called smoothness means that the old process will not be terminated before processing the current request. For many years, I have never questioned such a statement until one day, when I reload, there was a 502 error, so I had to rethink it.
How can we reproduce the problem? Let's write a simple script for simulation:
<?phpsleep(11);echo "foo";?>
In this case, use your browser to browse the URL and immediately execute the reload operation. The error 502 is displayed.
Is PHP so weak? Even the basic smoothness of reload cannot be guaranteed? The answer is no.process_control_timeout
Parameters can achieve our goal. Unfortunately, this parameter defaults to 0, that is, it does not take effect. This article sets it to 10 s. Re-execute the previous experiment step and output the result normally this time. However, if you do more experiments, you may find that when we reload, sleep is over immediately, because sleep returns directly after receiving the reload signal, next let's rewrite the script:
<?phpsleep(11);echo "foo";sleep(11);echo "bar";?>
When you re-execute the previous experiment step, you will find that the error 502 has appeared again. This is because although reload immediately stops the first sleep, the second sleep is still valid and exceedsprocess_control_timeout
. If we setprocess_control_timeout
Set it to 12 s.
In this case, we only needprocess_control_timeout
Setting a reasonable value can ensure the smoothness of the reload operation. But what is a reasonable value? If it is too small, it may not work. If it is too large, will it have any side effects? Let's repeat the previous experiment with questions, but this time we add another Metric:
shell> watch -n1 'ps aux | grep php[-]fpm'
The purpose of this monitoring is to observe the changes in the number of PHP-FPM processes in the reload process, in order to make the effect more obvious, it is recommended to change the PHP-FPM startup mode to static mode, while the number of processes should not be too large.
When we repeat the previous experiment, we found that in addition to the process executing the request, other processes were killed directly, and the new process was not started immediately, in this way, the new process is started only after the last old process is executed. During this period, if other requests come in, it will undoubtedly not be able to get a response immediately.
According to our experiment, we can draw a conclusion: by default, the PHP-FPM cannot ensure smooth reload operation, must set a reasonableprocess_control_timeout
At the same time, you must note that the value cannot be set too large, otherwise the system may encounter more serious request congestion problems.
Summary
The above is all about the Reload operations in PHP. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message.