Commit 67675b49 authored by Dries's avatar Dries

- More code improvements by JonBob.

parent 436dcead
<?php
// $Id$
/* Call the throttle_status() function from your own modules, themes, blocks,
* etc, to determine the current throttle status. For example, in your theme
/**
* Determine the current load on the site.
*
* Call the throttle_status() function from your own modules, themes, blocks,
* etc. to determine the current throttle status. 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).
* To determine the current throttle level from your own php code, you can add
* the following line:
* $throttle_level = module_invoke("throttle", "status");
* This will return a number from 0 to 5. 0 meaning that the current load is
* very small, 5 meaning that the current load is as heavy as it gets. You
* should consider disabling logic when the throttle_level gets to 4 or 5.
*
* @return
* A number from 0 to 5. 0 means that the current load is very small; 5
* means that the current load is as heavy as it gets. You should
* consider disabling logic when the throttle level gets to 4 or 5.
*/
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 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 on additional database
** query. 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 (!rand(0, variable_get("throttle_probability_limiter", 9))) {
/*
** Note: The rand() function is supported by PHP 3+. However, prior to
** PHP 4.2.0 it needs to be seeded with a call to srand(). It is important
** that this only happens once, so this should be managed by the Drupal
** engine, not this module. The Drupal engine should use phpversion() to
** detect and automatically seed pre-4.2.0 systems.
*/
// The following logic determines what the current throttle level should
// be, and can be disabled by the admin. If enabled, the 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 on additional database
// query. 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 (!rand(0, variable_get('throttle_probability_limiter', 9))) {
// Note: The rand() function is supported by PHP 3+. However, prior to
// PHP 4.2.0 it needs to be seeded with a call to srand(). It is important
// that this only happens once, so this should be managed by the Drupal
// engine, not this module. The Drupal engine should use phpversion() to
// detect and automatically seed pre-4.2.0 systems.
$throttle = throttle_status();
$multiplier = variable_get('throttle_multiplier', 60);
// count all hits in past sixty seconds
$recent_activity = db_fetch_object(db_query("SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d", (time() - 60)));
// Count all hits in the past sixty seconds.
$recent_activity = db_fetch_object(db_query('SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d', (time() - 60)));
_throttle_update($recent_activity->hits);
}
}
/**
* Implementation of hook_perm().
*/
function throttle_perm() {
/*
** throttle module defines the following permissions:
** access throttle block - see throttle statistics
*/
return array('access throttle block');
}
function throttle_help($section = "admin/help#throttle") {
/**
* Implementation of hook_help().
*/
function throttle_help($section) {
switch ($section) {
case "admin/system/modules#description":
$output = t("Allows configuration of congestion control auto-throttle mechanism.");
break;
case "admin/system/modules/throttle":
return t("If your site gets linked to by a popular website, or otherwise comes under a \"Denial of Service\" (DoS) attack, your webserver might become overwhelmed. This module provides a mechanism for automatically detecting a surge in incoming traffic. This mechanism is utilized by other Drupal models to automatically optimize their performance by temporarily disabling CPU-intensive functionality. To use the auto-throttle, the access log must be enabled. It is advised that you carefully read the explainations below and then properly tune this module based on your site's requirements and your webserver's capabilities.", array("%access" => url("admin/system/modules/statistics")));
case "admin/help#throttle":
$output .= t("
case 'admin/system/modules#description':
return t('Allows configuration of congestion control auto-throttle mechanism.');
case 'admin/system/modules/throttle':
return t('If your site gets linked to by a popular website, or otherwise comes under a "Denial of Service" (DoS) attack, your webserver might become overwhelmed. This module provides a mechanism for automatically detecting a surge in incoming traffic. This mechanism is utilized by other Drupal models to automatically optimize their performance by temporarily disabling CPU-intensive functionality. To use the auto-throttle, the access log must be enabled. It is advised that you carefully read the explainations below and then properly tune this module based on your site\'s requirements and your webserver\'s capabilities.', array('%access' => url('admin/system/modules/statistics')));
case 'admin/help#throttle':
return t("
<h3>Introduction</h3>
<p>This Drupal module allows you to enable and configure the auto-throttle congestion control mechanism offered by the <a href=\"%statistics-module\">statistics module</a>. The auto-throttle mechanism allows your site to automatically adapt to different server levels.</p>
<p>This module also adds a block that displays the current status of the throttle. You must have \"<a href=\"%throttle-block\">access throttle block</a>\" privileges to view the block. As a general rule of thumb, only site administrators should be granted access to this block.</p>
......@@ -94,64 +96,66 @@ function throttle_help($section = "admin/help#throttle") {
else {
// throttle limit not reached, execute normally
}</pre>
</p>", array("%statistics-module" => url("admin/statistics"), "%throttle-block" => url("admin/user/permission"), "%modules-enable" => url("admin/system/modules"), "%throttle-config" => url("admin/system/modules/throttle"), "%statistics-config" => url("admin/system/modules/statistics"), "%throttle-access" => url("admin/user/permission"), "%throttle-block-enable" => url("admin/block"), "%permissions" => url("admin/user/permission")));
break;
</p>", array('%statistics-module' => url('admin/statistics'), '%throttle-block' => url('admin/user/permission'), '%modules-enable' => url('admin/system/modules'), '%throttle-config' => url('admin/system/modules/throttle'), '%statistics-config' => url('admin/system/modules/statistics'), '%throttle-access' => url('admin/user/permission'), '%throttle-block-enable' => url('admin/block'), '%permissions' => url('admin/user/permission')));
}
return $output;
}
// throttle module configuration options
/**
* Implementation of hook_settings().
*/
function throttle_settings() {
// tune auto-throttle
$throttles = array(1 => "1 (0,1,2,3,4,5)", 5 => "5 (0,5,10,15,20,25)", 10 => "10 (0,10,20,30,40,50)", 12 => "12 (0,12,24,36,48,60)", 15 => "15 (0,15,30,45,60,75)", 20 => "20 (0,20,40,60,80,100)", 30 => "30 (0,30,60,90,120,150)", 50 => "50 (0,50,100,150,200,250)", 60 => "60 (0,60,120,180,240,300)", 100 => "100 (0,100,200,300,400,500", 500 => "500 (0,500,1000,1500,2000,2500", 1000 => "1000 (0,1000,2000,3000,4000,5000)");
$group = form_select(t("Auto-throttle multiplier"), 'throttle_multiplier', variable_get('throttle_multiplier', 60), $throttles, "The 'auto-throttle multiplier' is the number of hits in the past 60 seconds required to trigger a higher throttle level. For example, if you set the multiplier to 60, and your site is getting less than 60 hits a minute, then the throttle will be at a level of 0. Only once you start getting more than 60 hits a minute will the throttle level go to 1. If you start getting more than 120 hits a minute, the throttle becomes 2. This continues until your site is sustaining more than 300 hits per minute, at which time the throttle reaches a maximum level of 5. In the pop down menu, the first number is the multiplier, and the numbers in parenthesis are how many hits are required to switch to each throttle level. The more powerful your server, the higher you should set the multiplier value.");
$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%");
$group .= form_select(t("Auto-throttle probability limiter"), "throttle_probability_limiter", variable_get("throttle_probability_limiter", 9), $probabilities, "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 query to update the current level 1 out of every 10 page views. The busier your site, the lower you should set the limiter value.");
$period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 64800, 86400, 172800, 259200, 604800), "format_interval");
$output .= form_group(t("Auto-throttle tuning"), $group);
// Tune auto-throttle.
$throttles = array(1 => '1 (0,1,2,3,4,5)', 5 => '5 (0,5,10,15,20,25)', 10 => '10 (0,10,20,30,40,50)', 12 => '12 (0,12,24,36,48,60)', 15 => '15 (0,15,30,45,60,75)', 20 => '20 (0,20,40,60,80,100)', 30 => '30 (0,30,60,90,120,150)', 50 => '50 (0,50,100,150,200,250)', 60 => '60 (0,60,120,180,240,300)', 100 => '100 (0,100,200,300,400,500', 500 => '500 (0,500,1000,1500,2000,2500', 1000 => '1000 (0,1000,2000,3000,4000,5000)');
$group = form_select(t('Auto-throttle multiplier'), 'throttle_multiplier', variable_get('throttle_multiplier', 60), $throttles, 'The "auto-throttle multiplier" is the number of hits in the past 60 seconds required to trigger a higher throttle level. For example, if you set the multiplier to 60, and your site is getting less than 60 hits a minute, then the throttle will be at a level of 0. Only once you start getting more than 60 hits a minute will the throttle level go to 1. If you start getting more than 120 hits a minute, the throttle becomes 2. This continues until your site is sustaining more than 300 hits per minute, at which time the throttle reaches a maximum level of 5. In the pop down menu, the first number is the multiplier, and the numbers in parenthesis are how many hits are required to switch to each throttle level. The more powerful your server, the higher you should set the multiplier value.');
$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%');
$group .= form_select(t('Auto-throttle probability limiter'), 'throttle_probability_limiter', variable_get('throttle_probability_limiter', 9), $probabilities, '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 query to update the current level 1 out of every 10 page views. The busier your site, the lower you should set the limiter value.');
$period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 64800, 86400, 172800, 259200, 604800), 'format_interval');
$output .= form_group(t('Auto-throttle tuning'), $group);
return $output;
}
// displays admin oriented "Throttle status" block
/**
* Displays admin-oriented "Throttle status" block.
*/
function throttle_display_throttle_block() {
if (user_access('access throttle block')) {
/* the throttle is enabled: display the status of all throttle config */
$throttle = module_invoke("throttle", "status");
// The throttle is enabled: display the status of all throttle config.
$throttle = module_invoke('throttle', 'status');
$multiplier = variable_get('throttle_multiplier', 60);
$minimum = $throttle * $multiplier;
$limiter = variable_get("throttle_probability_limiter", 9);
/* calculate probability limiter's odds of updating throttle */
$limiter = variable_get('throttle_probability_limiter', 9);
// Calculate probability limiter's odds of updating the throttle level.
$probability = substr((($limiter / ($limiter + 1) * 100) - 100) * -1, 0, 4);
if ($throttle < 5) {
$maximum = (($throttle + 1) * $multiplier) - 1;
$output .= t("Current level: %level (%min - %max)", array("%level" => $throttle, "%min" => $minimum, "%max" => $maximum)) ."<br />\n";
$output .= t('Current level: %level (%min - %max)', array('%level' => $throttle, '%min' => $minimum, '%max' => $maximum)) ."<br />\n";
}
else {
$output .= t("Current level: %level (%min+)", array("%level" => $throttle, "%min" => $minimum)) ."<br />\n";
$output .= t('Current level: %level (%min+)', array('%level' => $throttle, '%min' => $minimum)) ."<br />\n";
}
$output .= t("Probability: %probability%", array("%probability" => $probability)) ."<br />\n";
$output .= t('Probability: %probability%', array('%probability' => $probability)) ."<br />\n";
if ($throttle < 5) {
$recent_activity = db_fetch_object(db_query("SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d", (time() - 60)));
$output .= "<br />". t("This site has served %pages in the past minute.", array("%pages" => format_plural($recent_activity->hits , "1 page", "%count pages")));
$recent_activity = db_fetch_object(db_query('SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d', (time() - 60)));
$output .= '<br />'. t('This site has served %pages pages in the past minute.', array('%pages' => format_plural($recent_activity->hits , '1 page', '%count pages')));
_throttle_update($recent_activity->hits);
}
}
return $output;
}
// block hook
function throttle_block($op = "list", $delta = 0) {
if ($op == "list") {
$blocks[0]["info"] = t("Throttle status");
/**
* Implementation of hook_block().
*/
function throttle_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Throttle status');
return $blocks;
}
else {
$block["subject"] = t("Throttle status");
$block["content"] = throttle_display_throttle_block();
$block['subject'] = t('Throttle status');
$block['content'] = throttle_display_throttle_block();
return $block;
}
}
......@@ -171,11 +175,11 @@ function _throttle_update($hits) {
// log the change
if ($throttle_new < $throttle) {
variable_set('throttle_level', $throttle - 1);
watchdog($type, "throttle: $hits hits in past minute; throttle decreased to level ". ($throttle - 1));
watchdog($type, 'throttle: $hits hits in past minute; throttle decreased to level '. ($throttle - 1));
}
if ($throttle_new > $throttle) {
variable_set('throttle_level', $throttle + 1);
watchdog($type, "throttle: $hits hits in past minute; throttle increased to level ". ($throttle + 1));
watchdog($type, 'throttle: $hits hits in past minute; throttle increased to level '. ($throttle + 1));
}
}
......
<?php
// $Id$
/* Call the throttle_status() function from your own modules, themes, blocks,
* etc, to determine the current throttle status. For example, in your theme
/**
* Determine the current load on the site.
*
* Call the throttle_status() function from your own modules, themes, blocks,
* etc. to determine the current throttle status. 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).
* To determine the current throttle level from your own php code, you can add
* the following line:
* $throttle_level = module_invoke("throttle", "status");
* This will return a number from 0 to 5. 0 meaning that the current load is
* very small, 5 meaning that the current load is as heavy as it gets. You
* should consider disabling logic when the throttle_level gets to 4 or 5.
*
* @return
* A number from 0 to 5. 0 means that the current load is very small; 5
* means that the current load is as heavy as it gets. You should
* consider disabling logic when the throttle level gets to 4 or 5.
*/
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 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 on additional database
** query. 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 (!rand(0, variable_get("throttle_probability_limiter", 9))) {
/*
** Note: The rand() function is supported by PHP 3+. However, prior to
** PHP 4.2.0 it needs to be seeded with a call to srand(). It is important
** that this only happens once, so this should be managed by the Drupal
** engine, not this module. The Drupal engine should use phpversion() to
** detect and automatically seed pre-4.2.0 systems.
*/
// The following logic determines what the current throttle level should
// be, and can be disabled by the admin. If enabled, the 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 on additional database
// query. 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 (!rand(0, variable_get('throttle_probability_limiter', 9))) {
// Note: The rand() function is supported by PHP 3+. However, prior to
// PHP 4.2.0 it needs to be seeded with a call to srand(). It is important
// that this only happens once, so this should be managed by the Drupal
// engine, not this module. The Drupal engine should use phpversion() to
// detect and automatically seed pre-4.2.0 systems.
$throttle = throttle_status();
$multiplier = variable_get('throttle_multiplier', 60);
// count all hits in past sixty seconds
$recent_activity = db_fetch_object(db_query("SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d", (time() - 60)));
// Count all hits in the past sixty seconds.
$recent_activity = db_fetch_object(db_query('SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d', (time() - 60)));
_throttle_update($recent_activity->hits);
}
}
/**
* Implementation of hook_perm().
*/
function throttle_perm() {
/*
** throttle module defines the following permissions:
** access throttle block - see throttle statistics
*/
return array('access throttle block');
}
function throttle_help($section = "admin/help#throttle") {
/**
* Implementation of hook_help().
*/
function throttle_help($section) {
switch ($section) {
case "admin/system/modules#description":
$output = t("Allows configuration of congestion control auto-throttle mechanism.");
break;
case "admin/system/modules/throttle":
return t("If your site gets linked to by a popular website, or otherwise comes under a \"Denial of Service\" (DoS) attack, your webserver might become overwhelmed. This module provides a mechanism for automatically detecting a surge in incoming traffic. This mechanism is utilized by other Drupal models to automatically optimize their performance by temporarily disabling CPU-intensive functionality. To use the auto-throttle, the access log must be enabled. It is advised that you carefully read the explainations below and then properly tune this module based on your site's requirements and your webserver's capabilities.", array("%access" => url("admin/system/modules/statistics")));
case "admin/help#throttle":
$output .= t("
case 'admin/system/modules#description':
return t('Allows configuration of congestion control auto-throttle mechanism.');
case 'admin/system/modules/throttle':
return t('If your site gets linked to by a popular website, or otherwise comes under a "Denial of Service" (DoS) attack, your webserver might become overwhelmed. This module provides a mechanism for automatically detecting a surge in incoming traffic. This mechanism is utilized by other Drupal models to automatically optimize their performance by temporarily disabling CPU-intensive functionality. To use the auto-throttle, the access log must be enabled. It is advised that you carefully read the explainations below and then properly tune this module based on your site\'s requirements and your webserver\'s capabilities.', array('%access' => url('admin/system/modules/statistics')));
case 'admin/help#throttle':
return t("
<h3>Introduction</h3>
<p>This Drupal module allows you to enable and configure the auto-throttle congestion control mechanism offered by the <a href=\"%statistics-module\">statistics module</a>. The auto-throttle mechanism allows your site to automatically adapt to different server levels.</p>
<p>This module also adds a block that displays the current status of the throttle. You must have \"<a href=\"%throttle-block\">access throttle block</a>\" privileges to view the block. As a general rule of thumb, only site administrators should be granted access to this block.</p>
......@@ -94,64 +96,66 @@ function throttle_help($section = "admin/help#throttle") {
else {
// throttle limit not reached, execute normally
}</pre>
</p>", array("%statistics-module" => url("admin/statistics"), "%throttle-block" => url("admin/user/permission"), "%modules-enable" => url("admin/system/modules"), "%throttle-config" => url("admin/system/modules/throttle"), "%statistics-config" => url("admin/system/modules/statistics"), "%throttle-access" => url("admin/user/permission"), "%throttle-block-enable" => url("admin/block"), "%permissions" => url("admin/user/permission")));
break;
</p>", array('%statistics-module' => url('admin/statistics'), '%throttle-block' => url('admin/user/permission'), '%modules-enable' => url('admin/system/modules'), '%throttle-config' => url('admin/system/modules/throttle'), '%statistics-config' => url('admin/system/modules/statistics'), '%throttle-access' => url('admin/user/permission'), '%throttle-block-enable' => url('admin/block'), '%permissions' => url('admin/user/permission')));
}
return $output;
}
// throttle module configuration options
/**
* Implementation of hook_settings().
*/
function throttle_settings() {
// tune auto-throttle
$throttles = array(1 => "1 (0,1,2,3,4,5)", 5 => "5 (0,5,10,15,20,25)", 10 => "10 (0,10,20,30,40,50)", 12 => "12 (0,12,24,36,48,60)", 15 => "15 (0,15,30,45,60,75)", 20 => "20 (0,20,40,60,80,100)", 30 => "30 (0,30,60,90,120,150)", 50 => "50 (0,50,100,150,200,250)", 60 => "60 (0,60,120,180,240,300)", 100 => "100 (0,100,200,300,400,500", 500 => "500 (0,500,1000,1500,2000,2500", 1000 => "1000 (0,1000,2000,3000,4000,5000)");
$group = form_select(t("Auto-throttle multiplier"), 'throttle_multiplier', variable_get('throttle_multiplier', 60), $throttles, "The 'auto-throttle multiplier' is the number of hits in the past 60 seconds required to trigger a higher throttle level. For example, if you set the multiplier to 60, and your site is getting less than 60 hits a minute, then the throttle will be at a level of 0. Only once you start getting more than 60 hits a minute will the throttle level go to 1. If you start getting more than 120 hits a minute, the throttle becomes 2. This continues until your site is sustaining more than 300 hits per minute, at which time the throttle reaches a maximum level of 5. In the pop down menu, the first number is the multiplier, and the numbers in parenthesis are how many hits are required to switch to each throttle level. The more powerful your server, the higher you should set the multiplier value.");
$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%");
$group .= form_select(t("Auto-throttle probability limiter"), "throttle_probability_limiter", variable_get("throttle_probability_limiter", 9), $probabilities, "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 query to update the current level 1 out of every 10 page views. The busier your site, the lower you should set the limiter value.");
$period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 64800, 86400, 172800, 259200, 604800), "format_interval");
$output .= form_group(t("Auto-throttle tuning"), $group);
// Tune auto-throttle.
$throttles = array(1 => '1 (0,1,2,3,4,5)', 5 => '5 (0,5,10,15,20,25)', 10 => '10 (0,10,20,30,40,50)', 12 => '12 (0,12,24,36,48,60)', 15 => '15 (0,15,30,45,60,75)', 20 => '20 (0,20,40,60,80,100)', 30 => '30 (0,30,60,90,120,150)', 50 => '50 (0,50,100,150,200,250)', 60 => '60 (0,60,120,180,240,300)', 100 => '100 (0,100,200,300,400,500', 500 => '500 (0,500,1000,1500,2000,2500', 1000 => '1000 (0,1000,2000,3000,4000,5000)');
$group = form_select(t('Auto-throttle multiplier'), 'throttle_multiplier', variable_get('throttle_multiplier', 60), $throttles, 'The "auto-throttle multiplier" is the number of hits in the past 60 seconds required to trigger a higher throttle level. For example, if you set the multiplier to 60, and your site is getting less than 60 hits a minute, then the throttle will be at a level of 0. Only once you start getting more than 60 hits a minute will the throttle level go to 1. If you start getting more than 120 hits a minute, the throttle becomes 2. This continues until your site is sustaining more than 300 hits per minute, at which time the throttle reaches a maximum level of 5. In the pop down menu, the first number is the multiplier, and the numbers in parenthesis are how many hits are required to switch to each throttle level. The more powerful your server, the higher you should set the multiplier value.');
$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%');
$group .= form_select(t('Auto-throttle probability limiter'), 'throttle_probability_limiter', variable_get('throttle_probability_limiter', 9), $probabilities, '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 query to update the current level 1 out of every 10 page views. The busier your site, the lower you should set the limiter value.');
$period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 64800, 86400, 172800, 259200, 604800), 'format_interval');
$output .= form_group(t('Auto-throttle tuning'), $group);
return $output;
}
// displays admin oriented "Throttle status" block
/**
* Displays admin-oriented "Throttle status" block.
*/
function throttle_display_throttle_block() {
if (user_access('access throttle block')) {
/* the throttle is enabled: display the status of all throttle config */
$throttle = module_invoke("throttle", "status");
// The throttle is enabled: display the status of all throttle config.
$throttle = module_invoke('throttle', 'status');
$multiplier = variable_get('throttle_multiplier', 60);
$minimum = $throttle * $multiplier;
$limiter = variable_get("throttle_probability_limiter", 9);
/* calculate probability limiter's odds of updating throttle */
$limiter = variable_get('throttle_probability_limiter', 9);
// Calculate probability limiter's odds of updating the throttle level.
$probability = substr((($limiter / ($limiter + 1) * 100) - 100) * -1, 0, 4);
if ($throttle < 5) {
$maximum = (($throttle + 1) * $multiplier) - 1;
$output .= t("Current level: %level (%min - %max)", array("%level" => $throttle, "%min" => $minimum, "%max" => $maximum)) ."<br />\n";
$output .= t('Current level: %level (%min - %max)', array('%level' => $throttle, '%min' => $minimum, '%max' => $maximum)) ."<br />\n";
}
else {
$output .= t("Current level: %level (%min+)", array("%level" => $throttle, "%min" => $minimum)) ."<br />\n";
$output .= t('Current level: %level (%min+)', array('%level' => $throttle, '%min' => $minimum)) ."<br />\n";
}
$output .= t("Probability: %probability%", array("%probability" => $probability)) ."<br />\n";
$output .= t('Probability: %probability%', array('%probability' => $probability)) ."<br />\n";
if ($throttle < 5) {
$recent_activity = db_fetch_object(db_query("SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d", (time() - 60)));
$output .= "<br />". t("This site has served %pages in the past minute.", array("%pages" => format_plural($recent_activity->hits , "1 page", "%count pages")));
$recent_activity = db_fetch_object(db_query('SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d', (time() - 60)));
$output .= '<br />'. t('This site has served %pages pages in the past minute.', array('%pages' => format_plural($recent_activity->hits , '1 page', '%count pages')));
_throttle_update($recent_activity->hits);
}
}
return $output;
}
// block hook
function throttle_block($op = "list", $delta = 0) {
if ($op == "list") {
$blocks[0]["info"] = t("Throttle status");
/**
* Implementation of hook_block().
*/
function throttle_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Throttle status');
return $blocks;
}
else {
$block["subject"] = t("Throttle status");
$block["content"] = throttle_display_throttle_block();
$block['subject'] = t('Throttle status');
$block['content'] = throttle_display_throttle_block();
return $block;
}
}
......@@ -171,11 +175,11 @@ function _throttle_update($hits) {
// log the change
if ($throttle_new < $throttle) {
variable_set('throttle_level', $throttle - 1);
watchdog($type, "throttle: $hits hits in past minute; throttle decreased to level ". ($throttle - 1));
watchdog($type, 'throttle: $hits hits in past minute; throttle decreased to level '. ($throttle - 1));
}
if ($throttle_new > $throttle) {
variable_set('throttle_level', $throttle + 1);
watchdog($type, "throttle: $hits hits in past minute; throttle increased to level ". ($throttle + 1));
watchdog($type, 'throttle: $hits hits in past minute; throttle increased to level '. ($throttle + 1));
}
}
......
<?php
// $Id$
/**
* Implementation of hook_help().
*/
function title_help($section) {
$output = "";
switch ($section) {
case 'admin/system/modules#description':
$output = t("Enables users to link to stories, articles or similar content by title.");
break;
return t('Enables users to link to stories, articles or similar content by title.');
}
return $output;
}
/**
......@@ -22,71 +20,80 @@ function title_link($type) {
}
}
function title_page() {
$title = urldecode(arg(1));
/**
* Menu callback; displays the matching node or a list of search results.
*/
function title_page($query) {
$title = urldecode($query);
$result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.title = '%s' AND n.status = 1 ORDER BY n.created DESC", $title);
$title = trim(str_replace(array('_', '%', '*'), ' ', $title));
if (db_num_rows($result) == 0) {
// No node with exact title found, try substring.
// No node with exact title found; try a substring.
$result = db_query("SELECT n.*, u.name, u.uid FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.title LIKE '%%%s%%' AND n.status = 1 ORDER BY n.created DESC", $title);
}
if (db_num_rows($result) == 0 && module_exist("search")) {
// still no matches ... return a full text search
if (db_num_rows($result) == 0 && module_exist('search')) {
// Still no matches, so return a full-text search.
search_view($title);
}
else if (db_num_rows($result) == 1) {
$node = db_fetch_object($result);
$node = node_load(array("nid" => $node->nid));
print theme("page", node_show($node, NULL), $node->title);
$node = node_load(array('nid' => $node->nid));
print theme('page', node_show($node, NULL), $node->title);
}
else {
$header = array(t("Type"), t("Title"), t("Author"));
$header = array(t('Type'), t('Title'), t('Author'));
while ($node = db_fetch_object($result)) {
$type = ucfirst(module_invoke($node->type, "node", "name"));
$type = ucfirst(module_invoke($node->type, 'node', 'name'));
$title = l($node->title, "node/view/$node->nid");
$author = format_name($node);
$rows[] = array(array("data" => $type, "class" => "type"), array("data" => $title, "class" => "content"), array("data" => $author, "class" => "author"));
$rows[] = array(array('data' => $type, 'class' => 'type'), array('data' => $title, 'class' => 'content'), array('data' => $author, 'class' => 'author'));
}
$output = "<div id=\"title\">";
$output .= theme("table", $header, $rows);
$output .= "</div>";
$output = '<div id="title">';
$output .= theme('table', $header, $rows);
$output .= '</div>';
drupal_set_title(t("Matching Posts"));
print theme("page", $output);
drupal_set_title(t('Matching Posts'));
print theme('page', $output);
}
}
function title_filter($op, $text = "") {
/**
* Implementation of hook_filter().
*/
function title_filter($op, $text = '') {
switch ($op) {
case "name":
return t("Title filter");
case "process":
case 'name':
return t('Title filter');
case 'process':
return _title_filter_process($text);
case "settings":
case 'settings':
return _title_filter_settings($text);
default:
return $text;
}
}
// filter [node title|description] links. '|description' is optional.
/**
* Filter [Node title|Description] links. '|Description' is optional.
*/
function _title_filter_process($text) {
$pattern = '\[([^\|\]\n]+)(?>\|?)([^\]\n]*)\]'; // $1 == title: matches at least 1 char up to the first '|' or ']'
// $2 == text: matches all after a following '|' (if there is) up to the next ']'. may include '|'s.
$pattern = '\[([^\|\]\n]+)(?>\|?)([^\]\n]*)\]';
// $1 == title: matches at least 1 char up to the first '|' or ']'.
// $2 == text: matches all after a following '|' (if there is) up to the next ']'.
// May include '|'s.
$replacement = 'l(\'$2\' ? \'$2\' : \'$1\', \'title/\'. urlencode(\'$1\'))';
return preg_replace("/$pattern/e", $replacement, $text);
}
function _title_filter_settings() {
return form_group(t("Title filter"), t("Wiki-like [node title|text] links are enabled. These shortcuts generate a link labeled 'text' to the node with the title 'node title'. If you omit '|text', the label becomes 'node title'. You may use a substring of a node title if desired. When multiple matching titles are found, a list of matching nodes will be displayed. If no matching titles are found, a full-text search is returned."));
return form_group(t('Title filter'), t('Wiki-like [node title|text] links are enabled. These shortcuts generate a link labeled "text" to the node with the title "node title". If you omit "|text", the label becomes "node title". You may use a substring of a node title if desired. When multiple matching titles are found, a list of matching nodes will be displayed. If no matching titles are found, a full-text search is returned.'));
}
function title_compose_tips() {
return array(t("You may quickly link to another node using this syntax: <em>[node title|text]</em>. This will generate a link labeled 'text' to the node with the title 'node title'. If you omit '|text', the label becomes 'node title'."));
return array(t('You may quickly link to another node using this syntax: <em>[node title|text]</em>. This will generate a link labeled "text" to the node with the title "node title". If you omit "|text", the label becomes "node title".'));
}
?>
<?php
// $Id$
function tracker_help($section = 'admin/help#tracker') {
/**
* Implementation of hook_help().
*/
function tracker_help($section) {
switch ($section) {
case 'admin/help#tracker':
return t('<p>The tracker module is a handy module for displaying the most recent posts. By following the <em>recent posts</em> link in the user block, a user may quickly review all recent postings.</p>');
......@@ -19,26 +22,29 @@ function tracker_link($type) {
}
}