In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to configure timeout time in the PHP file. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
I. Summary
There are a lot of timeout-related configurations in php.ini and php-fpm.conf, so what exactly are these configurations for? How is it realized in the source code? This article will talk about the following timeout configurations:
Php.ini
Max_execution_time
Max_input_time
Php-fpm.conf
Process_control_timeout
Request_terminate_timeout
Request_slowlog_timeout
Operating environment: Mac 10.14.2 + PHP 7.3.7
2. Configure resolution rules
The parsing of php.ini is completed in the php_module_startup () phase, and ini_entry is the parsing rule defined in main.c for each php.ini configuration in the following format:
ZEND_INI_ENTRY3_EX (name, default_value, modifiable, on_modify, arg1, arg2, arg3, displayer)
PHP defines many macros for different types of configurations, and ZEND_INI_ENTRY3_EX is the final macro after they are expanded, such as PHP_INI_ENTRY macros
Interpretation of PHP_INI_ENTRY (name, default_value, modifiable, on_modify) parameters
Name: configuration name
Default_value: configuration default
Modifiable: configurable range
These modes determine when and where an PHP instruction can be set. Each instruction in the manual has its own mode. For example, some instructions can be set with ini_set () in PHP scripts, while others can only be set in php.ini or httpd.conf.
For example, the output_buffering instruction belongs to PHP_INI_PERDIR, so it cannot be set with ini_set (). But the display_errors instruction belongs to PHP_INI_ALL and can be set anywhere, including ini_set ().
Mode meaning PHP_INI_USER can be set in user scripts (such as ini_set ()) or Windows registry (since PHP 5.3) and .user.ini PHP_INI_PERDIR can be set in php.ini,.htaccess or httpd.conf PHP_INI_SYSTEM can be set in php.ini or httpd.conf PHP_INI_ALL can be set anywhere
On_modify: configuration modification function
3. Max_input_time, max_execution_time
Because max_input_time and max_execution_time are closely related, they are put together.
Php.ini interprets max_input_time
; Maximum amount of time each script may spend parsing request data. It's a good
; idea to limit this time on productions servers in order to eliminate unexpectedly
; long running scripts.
; Note: This directive is hardcoded to-1 for the CLI SAPI
; http://php.net/max-input-time
Max_input_time is the maximum time each script can spend parsing request data. Long-running scripts can be cleared on the production server by restricting max_input_time. In CLI mode, it is hard-coded to-1, that is, unlimited.
Max_execution_time
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-...
; Note: This directive is hardcoded to 0 for the CLI SAPI
Max_execution_time is the maximum executable time for each script. Hard-coded to 0 in CLI mode
Configure resolution rule / / max_input_time. The default value is unlimited STD_PHP_INI_ENTRY ("max_input_time", "- 1", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateLong, max_input_time, php_core_globals, core_globals) / / max_execution_time, the default value is 30s, and the modification function is OnUpdateTimeoutPHP_INI_ENTRY ("max_execution_time", "30", PHP_INI_ALL, OnUpdateTimeout).
The OnUpdateTimeout () function is as follows. As you can see in section 2, configuration parsing occurs in the php_module_startup () phase, when EG (timeout_seconds) is assigned to max_execution_time, but no timer has been set.
/ / main.cstatic PHP_INI_MH (OnUpdateTimeout) {if (stage==PHP_INI_STAGE_STARTUP) {/ * Don't set a timeout on startup, only per-request * / * EG (timeout_seconds) = max_execution_time * / ZEND_ATOL (EG (timeout_seconds), ZSTR_VAL (new_value)); return SUCCESS;} zend_unset_timeout () ZEND_ATOL (EG (timeout_seconds), ZSTR_VAL (new_value)); zend_set_timeout (EG (timeout_seconds), 0); return SUCCESS;} set timeout timer / / main.cint php_request_startup (void) {. If (PG (max_input_time) =-1) {zend_set_timeout (EG (timeout_seconds), 1);} else {zend_set_timeout (PG (max_input_time), 1);}.} int php_execute_script (zend_file_handle * primary_file) {. If (PG (max_input_time)! =-1) {zend_set_timeout (INI_INT ("max_execution_time"), 0);}.
As you can see from the above code, if max_input_time is set (that is, if the value is not equal to-1 maxim 1 can be considered to be in CLI mode), a timer will be set in the php_request_startup () phase, and a timer will be reset when the timeout is max_input_time; in the php_execute_script () phase, and the timeout will be max_execution_time. Then the maximum execution time of the entire PHP script is equal to max_input_time + max_execution_time.
If max_input_time is not set (that is, the value is equal to-1), a timer is also set in the php_request_startup () phase, but the timeout is set to EG (timeout_seconds), and EG (timeout_seconds) has been assigned to max_execution_time in the php_module_startup () phase, so the timeout is max_execution_time. The timer is not reset during the php_execute_script () phase, and the max_execution_time timer set in the previous phase is still in effect. Then the maximum execution time of the entire PHP script is max_execution_time.
Zend_set_time () uses setitimer (ITIMER_PROF, & tweer, NULL); to implement timers. ITIMER_PROF counts the time spent in both user mode and kernel mode, while system calls such as sleep () suspend the process and do not take up cpu time slices, so these timeouts do not include sleep () time.
When the timer expires, ZendVM throws an E_ERROR, that is, a Fatal error error.
4. Process_control_timeoutphp-fpm.conf explanation, Time limit for child processes to wait for a reaction on signals from master.
; Available units: s (econds), m (inutes), h (ours), or d (ays)
; Default Unit: seconds
Process_control_timeout is the time limit left for child processes to process signals from master processes.
Analysis.
When the master process receives signals such as SIGINT, SIGTERM, SIGQUIT, and SIGUSR2, it calls fpm_pctl () for processing.
First of all, the master process will decide whether to send the SIGQUIT or SIGTERM signal to the worker process according to the received signal and the running state of the current fpm, and register the timing event with the time of process_control_timeout.
If the child process does not exit within the process_control_timeout time, the master process upgrades the SIGQUIT to SIGTERM,SIGTERM to SIGKILL and registers a timed event of 1s. SIGKILL terminates the worker process directly, and SIGTERM can give the worker process another 1 second.
To sum up, process_control_timeout can be understood as the time that the master process gives the worker process to end itself, and if it's time for worker to finish, then start master's own strategy.
5. Request_terminate_timeout, request_slowlog_timeout
Because request_terminate_timeout and request_slowlog_timeout are closely related, they are put together.
Php-fpm.conf interprets request_terminate_timeout
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of'0' means' off'.
; Available units: s (econds) (default), m (inutes), h (ours), or d (ays)
; Default Value: 0
The timeout for executing a request, after which the worker process will be terminated. This option should be used when the ini option max_execution_time cannot stop script execution for some reason.
Request_slowlog_timeout
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of'0s' means' off'.
; Available units: s (econds) (default), m (inutes), h (ours), or d (ays)
; Default Value: 0
The timeout for executing a request, after which the backtrace of a PHP is output to the slowlog file.
Analysis.
Request_slowlog_timeout and request_terminate_timeout are used in the heartbeat detection of the master process (fpm_pctl_heartbeat ()). The heartbeat time heartbeat simplified algorithm is
When request_terminate_timeout is enabled: request_terminate_timeout/1000*3
If request_terminate_timeout is not enabled: request_slowlog_timeout/1000*3 or 0
Request_terminate_timeout > = request_slowlog_timeout
The third rule is that in order to ensure that slowlog does not affect normal requests, heartbeat should take 1 / 3 of the timeout to avoid heartbeat detection too frequently, because all worker processes need to be traversed for each heartbeat detection.
If the timeout event occurs, it will directly kill the worker process, kill (child_pid, SIGTERM);, and then the kernel reclaims resources and closes the client_socket,nginx to return a 502 error to the browser.
These are all the contents of the article "how to configure timeouts in PHP files". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.