Jump to content

Idea and implementation to reduce server load


Guest Fast Lane

Recommended Posts

Posted

Since I know that for me the search tool is what causes a majority of the load spikes I have come up with an idea to solve it (I hope). I am experimenting with this right now. I know the coding is not elegant and may not work on everyones server, you should get the idea and be able to alter it for your forum. Basically when the one minute server load is above a set limit the search feature is disabled. When it is below that limit it is enabled. This will basically result in the search tool being offline when the server load is too high so that search queries do not stall and lock up tables causing major problems.


alter search.php as follows:

Before:

if (! $this->ipsclass->vars['allow_search'])
        {
            if ( $this->xml_out )
            {
                @header( "Content-type: text/html;charset={$this->ipsclass->vars['gb_char_set']}" );
                print $this->ipsclass->lang['search_off'];
                exit();
            }
            else
            {
                $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'search_off') );
            }
        }

Insert:

$top_com = "top -n 1 -b";
$top = trim(`$top_com`);

$pattern = "/^.*\b(average)\b.*$/mi";
preg_match($pattern, $top, $hits);
$loadline = $hits[0];

$load_bits = explode("average:",$loadline);
$load_parts = explode(",",$load_bits[1]);
$serverload = trim($load_parts[0]);  //equals one minute server load average
$serverload_limit = 2.5;        //define

        if ($serverload > $serverload_limit)       
           {
            if ( $this->xml_out )
               {
                   @header( "Content-type: text/html;charset={$this->ipsclass->vars['gb_char_set']}" );
                   print "Server Load is too high now.  The Search Feature is offline and will return shortly.  Thank you for understanding.";
                   exit();
               }
               else
               {
                   $this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'search_off') );
               }
        }



There may be a more elegant way to get the current server load (I am sure there is a built in ipb function). If you need to define $serverload differently for your server than change that code. This seems to work for me so far and I will report on how much it helps. I think that this could be a great feature addition to IPB if it works :).

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...