PDA

View Full Version : Restrict search when the server load is high


UK Jimbo
02-15-2005, 10:00 PM
From experience searches on large forums can slow the server down a lot.

Here's a dead simple hack that lets you disable searches when the load average reaches a defined level. Moderators are exempt from the search ban.

There's are just two changes to be made:

in search.php find:
if (!$vboptions['enablesearches'])
{
eval(print_standard_error('error_searchdisabled')) ;
}

add below it:
// automatically block search requests when the server is busy
$search_load_trigger=5;
if ( (!($permissions['adminpermissions'] & ISMODERATOR)) AND PHP_OS == 'Linux' AND @file_exists('/proc/loadavg') AND $filestuff = @file_get_contents('/proc/loadavg'))
{
$loadavg = explode(' ', $filestuff);
if (trim($loadavg[0]) > $search_load_trigger)
{
eval(print_standard_error('error_searchdisabled')) ;
}
}

The trigger level is based on the current (1 minute) load average. It can be changed by editing the $search_load_trigger variable.

You may also wish to change the searchdisabled phrase to something like:

Searching has been disabled. This is most likely automatic and due to high server load. Please try again later.

babolo
02-16-2005, 10:14 PM
nice hack maybe i'll install it

yoyoyoyo
02-16-2005, 10:36 PM
hmm.. interesting. thanks for sharing. I may install this later, though my site is on a shared server and loads can get pretty dang high :D

UK Jimbo
02-16-2005, 10:40 PM
Be really interested to get everyone's feedback on how well this has worked for you.

Jen
02-17-2005, 01:49 AM
I just installed it.. and the search feature stopped working alltogether. All I get now is a blank page.

Did some debugging and it's harfing somewhere after the first IF, won't even display a print after the if statement and nothing inside it.

Here's where it seems to die on me: @file_get_contents('/proc/loadavg').

UK Jimbo
02-17-2005, 08:22 AM
I just installed it.. and the search feature stopped working alltogether. All I get now is a blank page.

Did some debugging and it's harfing somewhere after the first IF, won't even display a print after the if statement and nothing inside it.

Here's where it seems to die on me: @file_get_contents('/proc/loadavg').

I should have pointed out that this will only work on Linux machines or *NIX flavours that support the proc file system and specificially /proc/loadavg

It'd be great if you could drop me a PM with some further error message/debug Jen. It definitely shouldn't present you with a blank page.

Jen
02-17-2005, 04:22 PM
PM Inbound UK. I'm running Redhat and /proc/loadavg exists, I'm wondering if it can get the file contents though.

Polo
02-17-2005, 08:05 PM
Interesting...

hollyboy
04-07-2005, 07:31 AM
where do I set the average server load number?

UK Jimbo
04-07-2005, 07:51 AM
where do I set the average server load number?

$search_load_trigger=5;

at the top.

Where 5 is a one minute load average of 5.

hollyboy
04-07-2005, 08:03 AM
$search_load_trigger=5;

at the top.

Where 5 is a one minute load average of 5.
I set it to 1 just to check if the hack worked and when my server load was at 1 the search still could be used. I'm on a linux server

UK Jimbo
04-07-2005, 09:01 AM
I set it to 1 just to check if the hack worked and when my server load was at 1 the search still could be used. I'm on a linux server

Admin users won't be search limited. Are you sure that you're weren't testing as an admin user?

hollyboy
04-07-2005, 09:04 AM
Admin users won't be search limited. Are you sure that you're weren't testing as an admin user?
as guest

UK Jimbo
04-07-2005, 09:08 AM
as guest

Sounds like something's broken then :)

Does the file /proc/loadavg exist? Is it readable?

hollyboy
04-07-2005, 09:11 AM
Sounds like something's broken then :)

Does the file /proc/loadavg exist? Is it readable?
where is this file?

UK Jimbo
04-07-2005, 09:17 AM
where is this file?

at /proc/loadavg (!)

Try creating a simple PHP file with the following code in:

<?php

if( file_exists('/proc/loadavg') ) {
echo("loadavg exists");
} else {
echo("can't find file");
}

?>

Now run this file and post up the output :)

hollyboy
04-07-2005, 09:22 AM
<?php

if( file_exists('/proc/loadavg') ) {
echo("loadavg exists");
} else {
echo("can't find file");
}

?>

http://www.interfans.org/forum/prop.php