2002-10-13 12:00:50 +00:00
<?php
2003-09-25 07:27:22 +00:00
// $Id$
2002-10-13 12:00:50 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Allows configuration of congestion control auto-throttle mechanism.
*/
2007-01-24 14:48:36 +00:00
function throttle_menu() {
$items['admin/settings/throttle'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Throttle',
'description' => 'Control how your site cuts out content during heavy load.',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('throttle_admin_settings'),
'access arguments' => array('administer site configuration'),
2007-08-22 13:35:02 +00:00
'file' => 'throttle.admin.inc',
2007-01-24 14:48:36 +00:00
);
2006-07-10 19:27:52 +00:00
return $items;
}
2004-05-24 18:37:50 +00:00
/**
* Determine the current load on the site.
*
* Call the throttle_status() function from your own modules, themes, blocks,
2006-12-20 10:29:31 +00:00
* etc. as follows:
*
* $throttle = module_invoke('throttle', 'status');
*
* to determine the current throttle status. Use module_invoke() so the
* call will still work if the throttle module is disabled. For example, in
* your theme you might choose to disable pictures when your site is too busy
* (reducing bandwidth), or in your modules you might choose to disable
* some complicated logic when your site is too busy (reducing CPU utilization).
2004-05-24 18:37:50 +00:00
*
* @return
2006-05-07 00:08:36 +00:00
* 0 or 1. 0 means that the throttle is currently disabled. 1 means that
* the throttle is currently enabled. When the throttle is enabled, CPU
2004-11-07 22:47:00 +00:00
* and bandwidth intensive functionality should be disabled.
2003-11-30 10:37:07 +00:00
*/
function throttle_status() {
2004-01-17 11:19:10 +00:00
return variable_get('throttle_level', 0);
2003-11-30 10:37:07 +00:00
}
2004-05-24 18:37:50 +00:00
/**
* Implementation of hook_exit().
*
* Changes the current throttle level based on page hits.
*/
2003-11-30 10:37:07 +00:00
function throttle_exit() {
2004-05-24 18:37:50 +00:00
// The following logic determines what the current throttle level should
2006-05-07 00:08:36 +00:00
// be, and can be disabled by the admin. If enabled, the mt_rand() function
2004-05-24 18:37:50 +00:00
// returns a number between 0 and N, N being specified by the admin. If
2004-11-07 22:47:00 +00:00
// 0 is returned, the throttle logic is run, adding two additional database
2006-05-07 00:08:36 +00:00
// queries. Otherwise, the following logic is skipped. This mechanism is
2004-05-24 18:37:50 +00:00
// referred to in the admin page as the 'probability limiter', roughly
// limiting throttle related database calls to 1 in N.
2006-01-17 18:32:33 +00:00
if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) {
2003-11-30 10:37:07 +00:00
2006-12-20 10:29:31 +00:00
// Count users with activity in the past n seconds.
// This value is defined in the user module Who's Online block.
$time_period = variable_get('user_block_seconds_online', 900);
2004-11-07 22:47:00 +00:00
2006-12-20 10:29:31 +00:00
// When determining throttle status in your own module or theme, use
// $throttle = module_invoke('throttle', 'status');
// as that will still work when throttle.module is disabled.
// Clearly here the module is enabled so we call throttle_status() directly.
$throttle = throttle_status();
2004-11-07 22:47:00 +00:00
if ($max_guests = variable_get('throttle_anonymous', 0)) {
2006-08-31 19:52:39 +00:00
$guests = sess_count(time() - $time_period, TRUE);
2004-11-07 22:47:00 +00:00
}
else {
$guests = 0;
}
if ($max_users = variable_get('throttle_user', 0)) {
2006-08-31 19:52:39 +00:00
$users = sess_count(time() - $time_period, FALSE);
2004-11-07 22:47:00 +00:00
}
else {
$users = 0;
}
// update the throttle status
2005-05-31 21:14:27 +00:00
$message = '';
2004-11-14 20:20:09 +00:00
if ($max_users && $users > $max_users) {
2004-11-07 22:47:00 +00:00
if (!$throttle) {
variable_set('throttle_level', 1);
2005-05-31 21:14:27 +00:00
$message = format_plural($users,
'1 user accessing site; throttle enabled.',
2006-08-18 12:17:00 +00:00
'@count users accessing site; throttle enabled.');
2004-11-07 22:47:00 +00:00
}
}
2004-11-14 20:20:09 +00:00
elseif ($max_guests && $guests > $max_guests) {
2004-11-07 22:47:00 +00:00
if (!$throttle) {
variable_set('throttle_level', 1);
2005-05-31 21:14:27 +00:00
$message = format_plural($guests,
'1 guest accessing site; throttle enabled.',
2006-08-18 12:17:00 +00:00
'@count guests accessing site; throttle enabled.');
2004-11-07 22:47:00 +00:00
}
}
else {
if ($throttle) {
variable_set('throttle_level', 0);
2005-05-31 21:14:27 +00:00
// Note: unorthodox format_plural() usage due to Gettext plural limitations.
2006-08-18 12:17:00 +00:00
$message = format_plural($users, '1 user', '@count users') .', ';
$message .= format_plural($guests, '1 guest accessing site; throttle disabled', '@count guests accessing site; throttle disabled');
2004-11-07 22:47:00 +00:00
}
}
2005-05-31 21:14:27 +00:00
if ($message) {
cache_clear_all();
2007-04-24 13:55:36 +00:00
watchdog('throttle', 'Throttle: %message', array('%message' => $message));
2005-05-31 21:14:27 +00:00
}
2003-11-30 10:37:07 +00:00
}
}
2004-05-24 18:37:50 +00:00
/**
* Implementation of hook_help().
*/
2007-06-30 19:46:58 +00:00
function throttle_help($path, $arg) {
switch ($path) {
2005-11-01 10:17:34 +00:00
case 'admin/help#throttle':
2007-12-13 09:34:40 +00:00
$output = '<p>'. t('The throttle module provides a congestion control mechanism that automatically adjusts to a surge in incoming traffic. If your site is referenced by a popular website, or experiences a "Denial of Service" (DoS) attack, your webserver might become overwhelmed. The throttle mechanism is utilized by modules to temporarily disable CPU-intensive functionality, increasing performance. For instance, via the throttle module, modules may choose to disable resource-intensive blocks or the code within the site theme may temporarily disable user pictures in posts.') .'</p>';
$output .= '<p>'. t('The congestion control throttle can be automatically enabled when the number of anonymous or authenticated users currently visiting the site exceeds a specified threshold.') .'</p>';
2007-12-14 18:08:50 +00:00
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@throttle">Throttle module</a>.', array('@throttle' => 'http://drupal.org/handbook/modules/throttle/')) .'</p>';
2005-11-01 10:17:34 +00:00
return $output;
2004-06-20 19:49:14 +00:00
case 'admin/settings/throttle':
2007-12-13 09:34:40 +00:00
return '<p>'. t('The throttle module provides a congestion control mechanism that automatically adjusts to a surge in incoming traffic. If your site is referenced by a popular website, or experiences a "Denial of Service" (DoS) attack, your webserver might become overwhelmed. The throttle mechanism is utilized by modules to temporarily disable CPU-intensive functionality, increasing performance.') .'</p>';
2003-08-20 21:19:17 +00:00
}
2002-10-13 12:00:50 +00:00
}