Redirect users if server is full or busy using php?

Is it possible to use php to redirect users to a page i.e. busy.phpwhen the server is busy or full or something similar? thank:))

+3
source share
3 answers

You can use PHP internal functions like memory-get-usage http://php.net/manual/de/function.memory-get-usage.php or access the Script shell, which gives you some information about the current load to the server. And then, depending on this information, set the redirection through the headers.

However, remember that if your server crashes, most likely PHP Script will not be executed and redirection will not occur. Thus, depending on your infrastructure, you can handle this through an additional server (possibly load balancing).

If you can narrow down the most likely cause of the failure, try to find it there, for example, if your connection to MySQL fails, extract it and direct the user to your "loaded page".

+1
source

I second using a dedicated load balancer instead with PHP. But if for some reason this is not an option, you can try sys_getloadavg:

, ( ) 1, 5 15 , .

:

<?php
$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.');
}
?>

php-src getloadavg . , , cat /proc/loadavg uptime.

Linux http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages:

. "100% " 1,00 , 2,00, , 4,00 ..

. https://superuser.com/questions/23498/what-does-load-average-mean-on-unix-linux

+4

It is best to use a load balancer, but just in case, you can redirect to the loaded page (which cannot use the database connection at all) with this piece of code in your installation class:

class [...] {
    [...]
    public function connect(){
        $this->conn = @mysql_connect ([...]) 
            or $this->dbError("Failed MySQL connection");
        [...]
    }

    private function dbError($msg){
        include("busy.php");
        die();
    }
}
+1
source

Source: https://habr.com/ru/post/1771666/


All Articles