<?php
function throttle_menu() {
$items['admin/settings/throttle'] = array(
'title' => 'Throttle',
'description' => 'Control how your site cuts out content during heavy load.',
'page callback' => 'drupal_get_form',
'page arguments' => array('throttle_admin_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'throttle.admin.inc',
);
return $items;
}
function throttle_status() {
return variable_get('throttle_level', 0);
}
function throttle_exit() {
if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) {
$time_period = variable_get('user_block_seconds_online', 900);
$throttle = throttle_status();
if ($max_guests = variable_get('throttle_anonymous', 0)) {
$guests = sess_count(time() - $time_period, TRUE);
}
else {
$guests = 0;
}
if ($max_users = variable_get('throttle_user', 0)) {
$users = sess_count(time() - $time_period, FALSE);
}
else {
$users = 0;
}
$message = '';
if ($max_users && $users > $max_users) {
if (!$throttle) {
variable_set('throttle_level', 1);
$message = format_plural($users,
'1 user accessing site; throttle enabled.',
'@count users accessing site; throttle enabled.');
}
}
elseif ($max_guests && $guests > $max_guests) {
if (!$throttle) {
variable_set('throttle_level', 1);
$message = format_plural($guests,
'1 guest accessing site; throttle enabled.',
'@count guests accessing site; throttle enabled.');
}
}
else {
if ($throttle) {
variable_set('throttle_level', 0);
$message = format_plural($users, '1 user', '@count users') .', ';
$message .= format_plural($guests, '1 guest accessing site; throttle disabled', '@count guests accessing site; throttle disabled');
}
}
if ($message) {
cache_clear_all();
watchdog('throttle', 'Throttle: %message', array('%message' => $message));
}
}
}
function throttle_help($path, $arg) {
switch ($path) {
case 'admin/help#throttle':
$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>';
$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>';
return $output;
case 'admin/settings/throttle':
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>';
}
}