Initial code using Drupal 6.38
This commit is contained in:
commit
4824608a33
467 changed files with 90887 additions and 0 deletions
54
modules/throttle/throttle.admin.inc
Normal file
54
modules/throttle/throttle.admin.inc
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Admin page callbacks for the throttle module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Form builder; Configure the throttle system.
|
||||
*
|
||||
* @ingroup forms
|
||||
* @see system_settings_form()
|
||||
* @see throttle_admin_settings_validate()
|
||||
*/
|
||||
function throttle_admin_settings() {
|
||||
$probabilities = array(0 => '100%', 1 => '50%', 2 => '33.3%', 3 => '25%', 4 => '20%', 5 => '16.6%', 7 => '12.5%', 9 => '10%', 19 => '5%', 99 => '1%', 199 => '.5%', 399 => '.25%', 989 => '.1%');
|
||||
|
||||
$form['throttle_anonymous'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Auto-throttle on anonymous users'),
|
||||
'#default_value' => variable_get('throttle_anonymous', 0),
|
||||
'#size' => 5,
|
||||
'#maxlength' => 6,
|
||||
'#description' => t('The congestion control throttle can be automatically enabled when the number of anonymous users currently visiting your site exceeds the specified threshold. For example, to start the throttle when your site has 250 anonymous users online at once, enter \'250\' in this field. Leave this value blank or set to "0" if you do not wish to auto-throttle on anonymous users. You can inspect the current number of anonymous users using the "Who\'s online" block.')
|
||||
);
|
||||
$form['throttle_user'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Auto-throttle on authenticated users'),
|
||||
'#default_value' => variable_get('throttle_user', 0),
|
||||
'#size' => 5,
|
||||
'#maxlength' => 6,
|
||||
'#description' => t('The congestion control throttle can be automatically enabled when the number of authenticated users currently visiting your site exceeds the specified threshold. For example, to start the throttle when your site has 50 registered users online at once, enter \'50\' in this field. Leave this value blank or set to "0" if you do not wish to auto-throttle on authenticated users. You can inspect the current number of authenticated users using the "Who\'s online" block.')
|
||||
);
|
||||
$form['throttle_probability_limiter'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Auto-throttle probability limiter'),
|
||||
'#default_value' => variable_get('throttle_probability_limiter', 9),
|
||||
'#options' => $probabilities,
|
||||
'#description' => t('The auto-throttle probability limiter is an efficiency mechanism to statistically reduce the overhead of the auto-throttle. The limiter is expressed as a percentage of page views, so for example if set to the default of 10% we only perform the extra database queries to update the throttle status 1 out of every 10 page views. The busier your site, the lower you should set the limiter value.')
|
||||
);
|
||||
|
||||
$form['#validate'] = array('throttle_admin_settings_validate');
|
||||
|
||||
return system_settings_form($form);
|
||||
}
|
||||
|
||||
function throttle_admin_settings_validate($form, &$form_state) {
|
||||
if (!is_numeric($form_state['values']['throttle_anonymous']) || $form_state['values']['throttle_anonymous'] < 0) {
|
||||
form_set_error('throttle_anonymous', t("%value is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $form_state['values']['throttle_anonymous'])));
|
||||
}
|
||||
if (!is_numeric($form_state['values']['throttle_user']) || $form_state['values']['throttle_user'] < 0) {
|
||||
form_set_error('throttle_user', t("%value is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $form_state['values']['throttle_user'])));
|
||||
}
|
||||
}
|
11
modules/throttle/throttle.info
Normal file
11
modules/throttle/throttle.info
Normal file
|
@ -0,0 +1,11 @@
|
|||
name = Throttle
|
||||
description = Handles the auto-throttling mechanism, to control site congestion.
|
||||
package = Core - optional
|
||||
version = VERSION
|
||||
core = 6.x
|
||||
|
||||
; Information added by Drupal.org packaging script on 2016-02-24
|
||||
version = "6.38"
|
||||
project = "drupal"
|
||||
datestamp = "1456343372"
|
||||
|
127
modules/throttle/throttle.module
Normal file
127
modules/throttle/throttle.module
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Allows configuration of congestion control auto-throttle mechanism.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current load on the site.
|
||||
*
|
||||
* Call the throttle_status() function from your own modules, themes, blocks,
|
||||
* 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).
|
||||
*
|
||||
* @return
|
||||
* 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
|
||||
* and bandwidth intensive functionality should be disabled.
|
||||
*/
|
||||
function throttle_status() {
|
||||
return variable_get('throttle_level', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_exit().
|
||||
*
|
||||
* Changes the current throttle level based on page hits.
|
||||
*/
|
||||
function throttle_exit() {
|
||||
// The following logic determines what the current throttle level should
|
||||
// be, and can be disabled by the admin. If enabled, the mt_rand() function
|
||||
// returns a number between 0 and N, N being specified by the admin. If
|
||||
// 0 is returned, the throttle logic is run, adding two additional database
|
||||
// queries. Otherwise, the following logic is skipped. This mechanism is
|
||||
// referred to in the admin page as the 'probability limiter', roughly
|
||||
// limiting throttle related database calls to 1 in N.
|
||||
if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) {
|
||||
|
||||
// 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);
|
||||
|
||||
// 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();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// update the throttle status
|
||||
$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);
|
||||
// Note: unorthodox format_plural() usage due to Gettext plural limitations.
|
||||
$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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_help().
|
||||
*/
|
||||
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>';
|
||||
}
|
||||
}
|
Reference in a new issue