Table of Contents
1. Overview
php.ini
php- fpm.conf
三、max_input_time、max_execution_time
php.ini 解释
max_input_time
max_execution_time
配置解析规则
设置超时定时器
四、process_control_timeout
php-fpm.conf 解释
分析
五、request_terminate_timeout、request_slowlog_timeout
php-fpm.conf Explanation
request_terminate_timeout
request_slowlog_timeout
Analysis
Home Backend Development PHP Tutorial A brief analysis of several timeout configurations in PHP configuration files

A brief analysis of several timeout configurations in PHP configuration files

Mar 09, 2022 am 11:21 AM
php php-fpm

This article will talk about the PHP configuration file and analyze several timeout-related configurations in the configuration file (php.ini and php-fpm.conf). I hope it can help you!

A brief analysis of several timeout configurations in PHP configuration files

1. Overview

There are many timeout-related configurations in php.ini and php-fpm.conf, so what exactly do these configurations do? How is it implemented 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

##Running environment: Mac 10.14.2 PHP 7.3.7

2. Configure parsing rules

Parsing rules

The parsing of php.ini is completed in the

php_module_startup() stage, ini_entry is in The parsing rules defined for each php.ini configuration in main.c are in the following format:

ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, arg3, displayer)
Copy after login
PHP defines many macros for different types of configurations,

ZEND_INI_ENTRY3_EX is The final macro after they are expanded, such as PHP_INI_ENTRYMacro

PHP_INI_ENTRY(name, default_value, modifiable, on_modify)
Copy after login
Parameter explanation

name: Configuration name

default_value: Configure the default value

modifiable: The configurable range of the configuration

These modes determine when and where a PHP command can be Whether it can be set. Each instruction in the manual has a mode to which it belongs. For example, some commands can be set in PHP scripts using ini_set(), while others can only be set in php.ini or httpd.conf.

For example, the output_buffering directive belongs to

PHP_INI_PERDIR, so it cannot be set with ini_set(). But the display_errors directive belongs to PHP_INI_ALL and can be set anywhere, including ini_set().

ModeMeaning##PHP_INI_USERPHP_INI_PERDIRPHP_INI_SYSTEMPHP_INI_ALL

on_modify: 配置修改函数

三、max_input_time、max_execution_time

因为max_input_timemax_execution_time 联系比较密切,所以放在一起来讲。

php.ini 解释

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是每个脚本可以花在解析请求数据上的最大时间。在生产服务器上通过限制max_input_time可以清除掉长时间运行的脚本。在CLI模式下会硬编码为-1,即无限制。

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是每个脚本的最大可执行时间。在CLI模式下硬编码为0

配置解析规则

// max_input_time,默认值为无限制
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,默认值为30s,修改函数为OnUpdateTimeout
PHP_INI_ENTRY("max_execution_time", "30",    PHP_INI_ALL, OnUpdateTimeout)
Copy after login

OnUpdateTimeout()函数如下,由第二节可知配置解析发生在php_module_startup()阶段,此时EG(timeout_seconds)被赋值为了max_execution_time,但还没有设置定时器。

// main.c
static 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;
}
Copy after login

设置超时定时器

// main.c
int 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);
  }  
  ......
}
Copy after login

从上面代码可以看到,如果设置了max_input_time(即值不等于-1,-1可以认为是在CLI模式下),在php_request_startup()阶段会设置一个定时器,超时时间为max_input_time;在php_execute_script()阶段会重新设置一个定时器,超时时间为max_execution_time。那么整个PHP脚本执行的最大执行时间就等于max_input_time + max_execution_time

如果没有设置max_input_time的话(即值等于-1),在php_request_startup()阶段也会设置一个定时器,但超时时间被设为了EG(timeout_seconds),而EG(timeout_seconds)已经在php_module_startup()阶段被赋值为max_execution_time,所以此时的超时时间就是max_execution_time;在php_execute_script()阶段不会重新设置定时器,前一阶段设置的max_execution_time定时器仍然生效着。那么整个PHP脚本的最大执行时间就是max_execution_time
A brief analysis of several timeout configurations in PHP configuration files

zend_set_time() 使用setitimer(ITIMER_PROF, &t_r, NULL); 来实现定时器,ITIMER_PROF会统计包括用户态和内核态下所花费的时间,而像sleep()这样的系统调用会让进程挂起,不占用cpu时间片,所以这俩超时时间是不包括sleep()时间的。

当定时器到时间后,ZendVM会抛出E_ERROR,即Fatal error错误。

四、process_control_timeout

php-fpm.conf 解释

; 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是留给子进程处理来自master进程信号的时间限制。

分析

当master进程接收到SIGINTSIGTERMSIGQUITSIGUSR2这些信号时,会调用fpm_pctl()来进行处理。

首先master进程会根据 接收到的信号 和 当前fpm的运行状态 来决定发送给worker进程的是SIGQUIT还是SIGTERM信号,同时注册时间为process_control_timeout的定时事件。

如果在process_control_timeout时间内子进程没有退出,那么master进程会升级SIGQUITSIGTERMSIGTERMSIGKILL,并注册1s的定时事件。SIGKILL就直接终止worker进程了,SIGTERM还能再给worker进程1s的时间。

综上,process_control_timeout可以理解为master进程留给worker进程结束自己的时间,要是到时间worker还没搞定那就开始master自己的策略了。

五、request_terminate_timeout、request_slowlog_timeout

因为request_terminate_timeoutrequest_slowlog_timeout 联系比较密切,所以放在一起来讲。

php-fpm.conf Explanation

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

Translation: The timeout period for executing a request, after which the worker process will be terminated. This option should be used when max_execution_time this ini option 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

Translation: Execute a request timeout, after which a PHP backtrace will be 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 The simplified algorithm is

  • When request_terminate_timeout is turned on: request_terminate_timeout/1000*3

  • When request_terminate_timeout is not enabled: request_slowlog_timeout/1000*3 or 0

  • request_terminate_timeout >= request_slowlog_timeout

##The third rule is to ensure that slowlog does not affect normal requests,

heartbeat takes 1 of the timeout time /3 should be to avoid too frequent heartbeat detection, because each heartbeat detection needs to traverse all worker processes.

If a timeout event occurs, the worker process will be killed directly,

kill(child_pid, SIGTERM);, and then the kernel will recycle resources and close the client_socket, and nginx will return a 502 error to the browser.

Recommended learning: "

PHP Video Tutorial"

Can be set in a user script (e.g. ini_set()) or in the Windows registry (since PHP 5.3) and in .user.ini
Can be set in php.ini, .htaccess or httpd.conf
Can be set in php .ini or httpd.conf set
can be set anywhere

The above is the detailed content of A brief analysis of several timeout configurations in PHP configuration files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles