Preface
Many seniors have warned us that reload can guarantee the smoothness of the whole process, so-called smoothness refers to the process of reload, the old process before processing the current request will not terminate prematurely. For many years, I have never questioned this, until one day, when I reload, there were 502 errors, so I had to rethink.
How do you reproduce the problem? Let's write a simple script that simulates:
<?phpsleep (one); echo "foo";? >
At this point in the browser to browse the URL, and then immediately perform the reload operation, you can see the 502 error.
Isn't PHP so weak? Even the basic smoothness of reload is not guaranteed? The answer is, of course, negative, actually through
Process_control_timeout
Parameters can achieve our goals. Unfortunately this parameter defaults to 0, which is not effective, this article set it to 10s. Re-execute the previous experimental step, which normally outputs the result. But if you do a few more experiments, you may find that when we reload, sleep is over immediately, because sleep receives a signal from reload and returns directly, so let's rewrite the script:
<?phpsleep (one), echo "foo", Sleep (one), echo "Bar"; >
Re-execute the previous lab steps and you'll see that the 502 error has appeared again. This is because reload the first sleep immediately, but the second sleep is still valid, and more than
Process_control_timeout
The time limit. If we put
Process_control_timeout
Set to 12s, then it's good.
So, let's just give
Process_control_timeout
Setting a reasonable value can guarantee the smoothness of the reload operation, but how large is the reasonable value? Too small words may not play a role, too big words will have side effects? Let's repeat the last experiment with a question, but this time we add another monitor:
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 not too much.
When we repeated the last experiment, we found that in addition to the process in which the request was being executed, the other process was killed directly, and the new process did not start immediately, so that the new process was stuck until the last old process was executed before the startup process was completed. In the meantime, if there is another request coming in, it is no doubt that it cannot be answered immediately.
According to our experiment, we can conclude that PHP-FPM cannot guarantee smooth execution of reload operation by default, and must set up a reasonable
Process_control_timeout
, it is important to note that the value cannot be set too large, or the system may have a more serious request blocking problem.
Summarize
The above is about PHP reload operation of all content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!