Full summary of PHP timeout processing

Source: Internet
Author: User

Overview

In the PHP development work very much uses the time-out processing to the time-out situation, I say several scenes:

    1. Get data asynchronously if a backend data source gets unsuccessful then skips without affecting the entire page presentation
    2. To ensure that the Web server does not have access to other pages because of poor performance of the page processing, some page operations are set
    3. For certain uploads or uncertain processing times, you need to set all timeouts in the entire process to unlimited, otherwise any one of the links set incorrectly, will lead to inexplicable execution interrupt
    4. Multiple back-end modules (MySQL, Memcached, HTTP interface), in order to prevent poor performance of a single interface, resulting in the entire front to get data too slow, affecting the page opening speed, causing avalanche
    5. 。。。 Lots of occasions that need to be timed out

These places need to consider the setting of timeouts, but the time-out in PHP is categorized, each processing method and strategy are different, in order to describe the system, I summed up the common PHP time-out processing summary.

Web Server timeout processing

Apache

Generally in high performance situations, the default all timeout configuration is 30 seconds, but in the case of uploading files, or network speed is very slow, it is possible to trigger a timeout operation.

There are currently three timeout settings in APACHEFASTCGIPHP-FPM mode:

FastCGI Timeout setting:

Modify the httpd.conf fastcgi connection configuration, similar to the following:

 
  
   
  fastcgiexternalserver/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/ php-fpm.sockscriptalias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/" addhandlerphp-fastcgi.phpactionphp-fastcgi/fcgi-bin/php-cgiaddtypeapplication/x-httpd-php.php
 
  

The default configuration is 30s, if you need to customize your configuration, you need to modify the configuration, such as modified to 100 seconds: (restart Apache after modification):

 
  
   
  fastcgiexternalserver/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/ php-fpm.sock-idle-timeout100scriptalias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/" addhandlerphp-fastcgi.phpactionphp-fastcgi/fcgi-bin/php-cgiaddtypeapplication/x-httpd-php.php
 
  

If the timeout returns a 500 error, disconnect the back-end PHP service and log an Apache error logs:

[thujan2718:30:152011] [ERROR] [client10.81.41.110] Fastcgi:commwithserver "/home/forum/apache/apache_php/cgi-bin/php-cgi" Aborted:idletimeout (30sec) [ThuJan2718 : 30:152011][error][client10.81.41.110]fastcgi:incompleteheaders (0bytes) receivedfromserver "/home/forum/apache/ apache_php/cgi-bin/php-cgi "

Additional fastcgi configuration parameter description:

IdleTimeout Daze time limit processlifetime the longest life cycle of a process, Unconditional Killmaxprocesscount maximum number of processes after expiration Defaultminclassprocesscount the minimum number of processes started per program Defaultmaxclassprocesscount the maximum number of processes started per program Ipcconnec Ttimeout Program Response time-out period ipccommtimeout the maximum time to communicate with the program, the above error may be that the value is set too small resulting in maxrequestsperprocess each process up to the number of processing, the completion of suicide

Lighttpd

Configuration: lighttpd.conf

In the LIGHTTPD configuration, there are several parameters about timeouts (space considerations, write-only timeouts, and write timeout parameters):

The main concerns are options:

server.max-keep-alive-idle=5server.max-read-idle=60server.read-timeout=0server.max-connection-idle=360 ———————— ———————— – The maximum number of requests #每次keep-alive, the default value is the longest wait time for 16server.max-keep-alive-requests=100#keep-alive, in seconds, The default value is the number of work subprocess 5SERVER.MAX-KEEP-ALIVE-IDLE=1200#LIGHTTPD, the default value is 0, and the single-process run server.max-worker=2# limits the user's maximum intermediate pause time during the sending of the request ( Unit is seconds), #如果用户在发送请求的过程中 (no request), the middle of the pause is too long, LIGHTTPD will actively disconnect the # default value is 60 (seconds) server.max-read-idle=1200# limit the user in the process of receiving an answer, The maximum intermediate pause time (in seconds), #如果用户在接收应答的过程中 (not finished), the middle of the pause is too long, LIGHTTPD will actively disconnect the # default value is 360 (seconds) server.max-write-idle=12000# The time-out limit for read client requests, in seconds, with 0 for no Limit # setting is less than max-read-idle when the read-timeout takes effect server.read-timeout=0# the time-out limit of the write reply page to the client, in seconds, With 0 for no limit # set less than Max-write-idle when the Write-timeout effective server.write-timeout=0# request Processing time limit, if used mod_proxy_core, that is and the back end of the interaction time limit , Unit is seconds server.max-connection-idle=1200 ———————————————— –

Description

For successive requests on a keep-alive connection, the maximum interval for sending the content of the first request is determined by the parameter max-read-idle, and the maximum interval for sending the requested content from the second request is determined by the parameter max-keep-alive-idle. The interval timeout between requests is also determined by Max-keep-alive-idle. The total time timeout for sending the requested content is determined by the parameter read-timeout. The timeout for lighttpd interaction data with the backend is determined by Max-connection-idle.

Extended reading:

http://www.snooda.com/read/244

Nginx

Configuration: nginx.conf

http{#Fastcgi:(Effective for the Fastcgi of the backend, Fastcgi does not belong to the proxy mode) fastcgi_connect_timeout5; #连接超时fastcgi_send_timeout10; #写超时fastcgi_read_timeout10 #读取超时 #proxy: (for the entry into force of proxy/upstreams) proxy_connect_timeout15s; #连接超时proxy_read_ timeout24s; #读超时proxy_send_timeout10s; #写超时}

Description

Nginx timeout settings are very clear and easy to understand, the above time-out for different modes of operation, but because the time-out caused by a lot of problems.

Extended reading:

Http://hi.baidu.com/pibuchou/blog/item/a1e330dd71fb8a5995ee3753.html http://hi.baidu.com/pibuchou/blog/item/ 7cbccff0a3b77dc60b46e024.html http://hi.baidu.com/pibuchou/blog/item/10a549818f7e4c9df703a626.html/HTTP www.apoyl.com/?p=466

PHP itself timed out processing

php-fpm

Configuration: php-fpm.conf

 
  
 
  
   
  ... Setsthelimitonthenumberofsimultaneousrequeststhatwillbeserved.EquivalenttoApacheMaxClientsdirective.EquivalenttoPHP _fcgi_childrenenvironmentinoriginalphp.fcgiusedwithanypm_style. Number of processes #php-cgi
  
   
    
   128Thetimeout ( inseconds) Forservingasinglerequestafterwhichtheworkerprocesswillbeterminatedshouldbeusedwhen ' Max_execution_ Time ' inioptiondoesnotstopscriptexecutionforsomereason ' 0s ' means ' off ' #php-fpm request execution timeout, 0s to never time out, otherwise set an Ns to time-out of the number of seconds
   
    
     
    0sThetimeout (inseconds) forservingofsinglerequestafterwhichaphpbacktracewillbedumpedtoslow.logfile ' 0s ' means ' off '
    
     
      
     0s
    
     
   
    
  
   
 
  

Description

In PHP.ini, there is a parameter max_execution_time can set the maximum execution time for PHP scripts, but in php-cgi (PHP-FPM), this parameter will not work. Really able to control the maximum execution of PHP scripts:

 
  
   
  0s
 
  

This means that if you are running Max_execution_time using mod_php5.so mode, it will take effect, but not when running in PHP-FPM mode.

Extended reading:

http://blog.s135.com/file_get_contents/

Php

Configuration: PHP.ini

Options:

Max_execution_time=30

or set it in code:

Ini_set ("Max_execution_time"), Set_time_limit (30);

Description

Valid for the current session, such as setting 01 to not timeout, but if PHP Safe_mode is turned on, these settings will not take effect.

The same effect, but the specific content needs to refer to php-fpm part of the content, if the php-fpm set Request_terminate_timeout, then Max_execution_time will not take effect.

Backend & Interface Access timeout

HTTP access

In general, we visit HTTP many ways, mainly: Curl,socket,file_get_contents () and other methods.

If you encounter the other side of the server has not been responding, we are tragic, it is easy to kill the entire server, so when accessing HTTP also need to consider the problem of timeouts.

CURL access to HTTP

CURL is one of the more reliable LIB libraries that we use to access HTTP protocol interfaces, with high performance, and some features that are supported concurrently.

CURL:

curl_setopt ($ch, opt) can set some timeout settings, mainly including: * (important) Curlopt_timeout sets the maximum number of seconds that curl is allowed to execute. * (important) Curlopt_timeout_ms sets the maximum number of milliseconds that curl is allowed to perform. (added in cURL7.16.2.) Can be used from PHP5.2.3 onwards. Curlopt_connecttimeout the time to wait before initiating the connection and, if set to 0, waits indefinitely. Curlopt_connecttimeout_ms the time, in milliseconds, that the connection is trying to wait. If set to 0, the wait is infinite. Was added in the cURL7.16.2. Available starting from PHP5.2.3. Curlopt_dns_cache_timeout sets the time to save DNS information in memory by default of 120 seconds.

Curl normal second time-out:

$ch =curl_init (); curl_setopt ($ch, Curlopt_url, $url) curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, CURLOPT_TIMEOUT,60)///only need to set a number of seconds to curl_setopt ($ch, Curlopt_httpheader, $headers); curl_setopt ($ch, Curlopt_ useragent, $defined _vars[' http_user_agent ']);

Curl normal second time-out use:

curl_setopt ($ch, curlopt_timeout,60); If you need to perform a millisecond timeout, you need to add: curl_easy_setopt (curl,curlopt_nosignal,1l);

Or is:

curl_setopt ($ch, curlopt_nosignal,true), which can support millisecond-level timeout settings

Curl An example of a millisecond-level timeout:

 
  0) {echo "Curlerror ($curl _errno): $curl _errorn";} Else{echo "datareceived: $datan";}} Else{//serversleep (+); echo "done.";}? >

Some other tricks:

    1. According to experience is: Curl version >=libcurl/7.21.0 version, the millisecond time-out is bound to take effect, remember.
    2. The Curl_multi millisecond timeout also has a problem: A single access is to support MS-level timeouts, curl_multi parallel tuning will not be allowed

Streaming Mode Access http

In addition to curl, we often use the fsockopen, or the file operation function to do the HTTP protocol processing, so we have the time-out processing of this block is also necessary.

The general connection timeout can be set directly, but the stream read timeout needs to be handled separately.

Write your own code handling:

$tmCurrent =gettimeofday (); $intUSGone = ($tmCurrent [' sec ']-$tmStart [' sec ']) *1000000+ ($tmCurrent [' usec ']-$tmStart [ ' USEC ']); if ($intUSGone > $this->_intreadtimeoutus) {returnfalse;}

Or use the built-in stream handler functions Stream_set_timeout () and Stream_get_meta_data () Processing:

     

File_get_contents timeout:

 
   The array (' timeout ' =>5//sets a time-out, in seconds), $ctx =stream_context_create ($timeout), $text =file_get_contents ("http:// example.com/", 0, $ctx);? >

fopen Timeout:

 
   Array (' timeout ' =>5//sets a time-out, in seconds), $ctx =stream_context_create ($timeout); if ($fp =fopen ("http://example.com/" , "R", False, $ctx)) {while ($c =fread ($fp, 8192)) {echo$c;} Fclose ($FP);}? >

Mysql

The MySQL client in PHP does not have the option to set the timeout, neither mysqli nor MySQL, but Libmysql is a timeout option, but we just hide it in PHP.

So how to use this operation in PHP, we need to define some of our own MySQL operation constants, mainly related to the constants are:

mysql_opt_read_timeout=11; mysql_opt_write_timeout=12;

With these two, you can use the options to set the corresponding values after the definition.

But there is a point of note, MySQL internal implementation:

    1. Time-out setting is seconds, minimum configuration 1 seconds
    2. But the MySQL bottom of read will retry two times, so it will actually be 3 seconds

Retry two times + self once = 3 times times the timeout, so that the minimum timeout is 3 seconds, no less than this value, for most applications is acceptable, but for a small number of applications need to be optimized.

To view a PHP instance that sets access to the MySQL timeout:

 
   Options (mysql_opt_read_timeout,3), $mysqli->options (mysql_opt_write_timeout,1);//Connection Database $mysqli->real_ Connect ("localhost", "root", "root", "test"), if (Mysqli_connect_errno ()) {printf ("connectfailed:%s/n", Mysqli_ Connect_error ()); exit ();} Execute query sleep1 seconds does not time out printf ("hostinformation:%s/n", $mysqli->host_info); $res = $mysqli->query (' Selectsleep (1))) {echo "Query1error:". $mysqli->error. " /n ";} Else{echo "query1:querysuccess/n";} Execute query SLEEP9 seconds will time out if (! ( $res = $mysqli->query (' Selectsleep (9))) {echo "Query2error:". $mysqli->error. " /n ";} Else{echo "query2:querysuccess/n";} $mysqli->close (); echo "closemysqlconnection/n";? >

Extended reading:

http://blog.csdn.net/heiyeshuwu/article/details/5869813

Memcached

PHP extensions

Php_memcache Client:

Connection timeout: Boolmemcache::connect (String$host[,int$port[,int$timeout]])

There are no explicit timeout parameters at get and set.

Libmemcached client: There is no obvious timeout parameter in the PHP interface.

Description: So, access to memcached in PHP is a lot of problems, you need to hack part of the operation, or refer to the online patch.

c&c++ Access memcached

Client: libmemcached Client

Description: Memcache Timeout configuration can be configured with small points, such as 5, 10 milliseconds is enough, more than this time than the database query.

The following is a C + + example of a timeout to connect and read the set data:

Create connection timeout (connect to memcached) Memcached_st*memcacheproxy::_create_handle () {MEMCACHED_ST*MMC=NULL;MEMCACHED_RETURN_TPRC , if (_mpool!=null) {//getfrompoolmmc=memcached_pool_pop (_MPOOL,FALSE,&AMP;PRC), if (mmc==null) {__LOG_WARNING__ (" Memcacheproxy "," gethandlefrompoolerror[%d] ", (int) PRC); RETURNMMC;} Memcached_st*handle=memcached_create (NULL), if (handle==null) {__log_warning__ ("Memcacheproxy", "Create_ HandleError "); returnnull;} Set connection/Read Timeout memcached_behavior_set (handle,memcached_behavior_hash,memcached_hash_default); memcached_behavior_ Set (Handle,memcached_behavior_no_block,_noblock),///parameter Memcached_behavior_no_block is 1 to make the timeout configuration take effect, do not set the timeout will not take effect, the key is tragic, Easy to cause avalanche memcached_behavior_set (handle,memcached_behavior_connect_timeout,_connect_timeout);//Connection Timeout memcached_ Behavior_set (handle,memcached_behavior_rcv_timeout,_read_timeout);//Read Timeout Memcached_behavior_set (Handle,MEMCACHED _behavior_snd_timeout,_send_timeout);//write Timeout Memcached_behavior_set (Handle,memcached_behavior_poll_timeout,_poll_ timeout);//Set consistent hash//memcached_behavior_set_distRibution (handle,memcached_distribution_consistent); Memcached_behavior_set (handle,memcached_behavior_ distribution,memcached_distribution_consistent); Memcached_returnrc;for (uinti=0;i<_server_count;i++) {rc= Memcached_server_add (Handle,_ips[i],_ports[i]); if (MEMCACHED_SUCCESS!=RC) {__log_warning__ ("MemCacheProxy", " Addserver[%s:%d]failed. ", _ips[i],_ports[i]);}} _mpool=memcached_pool_create (Handle,_min_connect,_max_connect); if (_mpool==null) {__log_warning__ ("MemCacheProxy "," Create_poolerror "); returnnull;} Mmc=memcached_pool_pop (_MPOOL,FALSE,&AMP;PRC); if (mmc==null) {__log_warning__ ("Mymemcacheproxy", " GETHANDLEFROMPOOLERROR[%D] ", (int) PRC); __log_debug__ ("Memcacheproxy", "gethandle[%p", handle); RETURNMMC;} Set a key timeout (set a data to memcached) Boolmemcacheproxy::_add (Memcached_st*handle,unsignedint*key,constchar*value, Intlen,unsignedinttimeout) {memcached_returnrc;chartmp[1024];snprintf (tmp,sizeof (TMP), "%u#%u", key[0],key[1]);// There is a timeout value of Rc=memcached_set (Handle,tmp,strlen (TMP), (char*) value,len,timeout,0);MEMCACHED_SUCCESS!=RC) {returnfalse;} Returntrue;} Memcache Read Data timeout (not set)

Libmemcahed the interface definition in the source code:

Libmemcached_apichar*memcached_get (memcached_st*ptr,constchar*key,size_tkey_length,size_t*value_length,uint32_ T*FLAGS,MEMCACHED_RETURN_T*ERROR); Libmemcached_apimemcached_return_tmemcached_mget (Memcached_st*ptr,constchar*const*keys,constsize_t*key_length, Size_tnumber_of_keys);

It can be seen from the interface that there is no time-out setting when reading data.

Extended reading:

http://hi.baidu.com/chinauser/item/b30af90b23335dde73e67608 http://libmemcached.org/libMemcached.html

How to implement Timeouts

The program needs to have time-out this function, such as you access a back-end socket module, the socket module does not belong to any of the above described by the time, its protocol is also private, then this time may need to implement some time-out processing strategy, this time need some processing code.

Timeout implementation in PHP

First, beginner: The simplest time-out implementation (seconds timeout)

The idea is simple: Link a backend, then set to non-blocking mode, and if there is no connection on the loop, determine the difference between the current time and the time-out.

The original timeout is implemented in Phpsocket: (the current time of each loop is reduced, performance is poor, CPU usage is higher)

  _connect ($socket, $host, $port))//If there is no connection on the dead loop {$err =socket_last_error ($socket); if ($err ==115| | $err ==114) {if ((Time ()-$time) >= $timeout)//Every time you need to determine if you have timed out {socket_close ($socket); Die ("CONNECTIONTIMEDOUT.N");} Sleep (1); Continue } die (Socket_strerror ($err). n "); } socket_set_block ($this->socket)//restore blocking mode Ordie ("Unabletosetblockonsocketn");?>

Second, upgrade: Using PHP with asynchronous IO to implement (millisecond time-out)

Description

Asynchronous IO: The concept of asynchronous IO is relative to synchronous IO. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion. Asynchronous IO transfers bits into groups, and the group can be 8-bit 1 characters or longer. The sender can send these bit groups at any time, and the receiver never knows when they will arrive.

Multiplexing: A multiplexed model detects multiple IO operations and returns an actionable set that can be manipulated. This avoids the blocking IO not being able to handle the determination of the various IO and non-blocking system resources at any time.

Using Socket_select () to implement timeouts

Socket_select ($timeout), ceil ($timeout *1000000));

Select Features: Ability to set timeout to microsecond level!

Use the timeout code for Socket_select () (need to understand some of the knowledge of asynchronous IO programming)

Programmatic calling class programming

 
    Can_read (0) as$socket) {if ($socket = = $client->socket) {//newclientsocket$select->add (socket_accept ($client- >socket));} Else{//there ' ssomethingtoreadon$socket}}}?>

Programming Asynchronous Multiplexing IO & Timeout connection processing classes

 
    Sockets=array (); foreach ($socketsas $socket) {$this->add ($socket);}} Functionadd ($add _socket) {Array_push ($this->sockets, $add _socket);} Functionremove ($remove _socket) {$sockets =array (); foreach ($this->socketsas$socket) {if ($remove _socket!= $socket ) $sockets []= $socket;} $this->sockets= $sockets;} Functioncan_read ($timeout) {$read = $this->sockets;socket_select ($read, $write =null, $except =null, $timeout); Return$read;} Functioncan_write ($timeout) {$write = $this->sockets;socket_select ($read =null, $write, $except =null, $timeout); Return$write;}}? >

Time-out implementation in c&c++

Typically in linuxc/c++, you can use: Alarm () to set a timer for a second time-out, or: asynchronous multiplexing Io, such as SELECT (), poll (), Epoll (), to achieve a millisecond time-out. You can also use the two-time encapsulated asynchronous IO Library (Libevent,libev).

One, using the signal to achieve time-out (second-level timeout) alarm

Description: The Linux kernel connect timeout is typically 75 seconds, and we can set a smaller time, such as 10 seconds, to return from connect in advance. Here, using the signal processing mechanism, call alarm, generate SIGALRM signal after timeout (also can use Select implementation)

Example of setting the timeout code with the Alarym-second implementation of Connect:

Signal processing function Staticvoidconnect_alarm (Intsigno) {debug_printf ("Signalhandler"); return;} Alarm Timeout Connection Implementation Staticvoidconn_alarm () {sigfunc*sigfunc;//existing signal processing function sigfunc=signal (sigalrm,connect_alarm);// Establish the signal processing function connect_alarm, (if any) to save the existing signal processing function inttimeout=5;//Set the alarm if (alarm (timeout)!=0) {//... The alarm has been set to process}//for connection operation if (Connect (M_socket, (structsockaddr*) &addr,sizeof (addr)) <0) {if (ERRNO==EINTR) {/// If the error number is set to EINTR, the timeout is interrupted debug_printf ("timeout");
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.