PHP: Detect and display "Server Overload" message
I have noticed several websites such as hypem.com show "You didn't receive" error message when the site is busy, rather than just letting people wait, time out, or refresh; aggravating possibly a server load issue.
We are too busy to process your request. Please press "back" in your browser and try what you are doing again.
How is this achieved before the server is overloaded? This sounds like a very neat way to manage user expectations if the site gets overloaded, and also allows time for the site to recover.
source to share
You can just create a 500.html file and use your webserver every time a 50 error is thrown.
those. in apache config:
ErrorDocument 500 /errors/500.html
Or use php's disable feature to check if the request timeout has been reached (which is 30 seconds by default), and if so, redirect / render something static (so the error itself can't cause problems).
Note that most sites where you see the message "This site is taking too long to respond" effectively generate this message using javascript.
source to share
Other options:
$load = sys_getloadavg();
if ($load[0] > 80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
I got it from the php site http://php.net/sys_getloadavg , although I'm not sure what the values ββrepresent what sys_getloadavg returns
source to share
You can use php's tick function to detect when the server hasn't loaded for a certain amount of time and then display error messages. Primary use:
<?php
$connection = false;
function checkConnection( $connectionWaitingTime = 3 )
{
// check connection & time
global $time,$connection;
if( ($t = (time() - $time)) >= $connectionWaitingTime && !$connection){
echo ("<p> Server not responding for <strong>$t</strong> seconds !! </p>");
die("Connection aborted");
}
}
register_tick_function("checkConnection");
$time = time();
declare (ticks=1)
{
require 'yourapp.php' // load your main app logic
$connection = true ;
}
While (true) is just to simulate a busy server. To implement the script on your site, you need to remove the while statement and add the EG page dispatch logic or fronts dispatcher events, etc.
And the $ connectionWaitingTime in checkCOnnection function is set to timeout after 3 seconds, but you can change that to whatever you want
source to share
This could be due to the timing out of the database connection, but it is assumed that your server has more DB load than the CPU load when the timing gets tough. If so, you can make the output of your DB connector a message if no connection occurs within 1 second.
You can also use a quick query on the logs table to find out how many impressions / seconds there are and not automatically respond to any else after a certain point in order to save the QOS for others. In this case, you will need to set this level manually based on the server logs. An alternative method can be seen here in the Drupal throttle module.
Another alternative would be to use the Apache status page to get information on how many child processes are free, and there is no one left for throttling the id as per @giltotherescue's answer to this question .
source to share
You can also limit the maximum connection in apache config ...
Contact
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#maxclients
http://www.howtoforge.com/configuring_apache_for_maximum_performance
source to share
This is not strictly a PHP solution, but you can do it like Twitter, i.e .:
- serves a mostly static HTML and Javascript application from a CDN or your other server
- the calls to the actual server-side (PHP in your case) / API work are actually done in AJAX from one of your static JS files
- so you can set the timeout for AJAX calls and return "It looks like the tweets may take longer than expected to load."
source to share