annotate modules/throttle/throttle.module @ 3:165d43f946a8 6.1

Drupal 6.1
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:29:21 +0100
parents c1f4ac30525a
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: throttle.module,v 1.83 2007/12/14 18:08:49 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Allows configuration of congestion control auto-throttle mechanism.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 function throttle_menu() {
webmaster@1 10 $items['admin/settings/throttle'] = array(
webmaster@1 11 'title' => 'Throttle',
webmaster@1 12 'description' => 'Control how your site cuts out content during heavy load.',
webmaster@1 13 'page callback' => 'drupal_get_form',
webmaster@1 14 'page arguments' => array('throttle_admin_settings'),
webmaster@1 15 'access arguments' => array('administer site configuration'),
webmaster@1 16 'file' => 'throttle.admin.inc',
webmaster@1 17 );
webmaster@1 18 return $items;
webmaster@1 19 }
webmaster@1 20
webmaster@1 21 /**
webmaster@1 22 * Determine the current load on the site.
webmaster@1 23 *
webmaster@1 24 * Call the throttle_status() function from your own modules, themes, blocks,
webmaster@1 25 * etc. as follows:
webmaster@1 26 *
webmaster@1 27 * $throttle = module_invoke('throttle', 'status');
webmaster@1 28 *
webmaster@1 29 * to determine the current throttle status. Use module_invoke() so the
webmaster@1 30 * call will still work if the throttle module is disabled. For example, in
webmaster@1 31 * your theme you might choose to disable pictures when your site is too busy
webmaster@1 32 * (reducing bandwidth), or in your modules you might choose to disable
webmaster@1 33 * some complicated logic when your site is too busy (reducing CPU utilization).
webmaster@1 34 *
webmaster@1 35 * @return
webmaster@1 36 * 0 or 1. 0 means that the throttle is currently disabled. 1 means that
webmaster@1 37 * the throttle is currently enabled. When the throttle is enabled, CPU
webmaster@1 38 * and bandwidth intensive functionality should be disabled.
webmaster@1 39 */
webmaster@1 40 function throttle_status() {
webmaster@1 41 return variable_get('throttle_level', 0);
webmaster@1 42 }
webmaster@1 43
webmaster@1 44 /**
webmaster@1 45 * Implementation of hook_exit().
webmaster@1 46 *
webmaster@1 47 * Changes the current throttle level based on page hits.
webmaster@1 48 */
webmaster@1 49 function throttle_exit() {
webmaster@1 50 // The following logic determines what the current throttle level should
webmaster@1 51 // be, and can be disabled by the admin. If enabled, the mt_rand() function
webmaster@1 52 // returns a number between 0 and N, N being specified by the admin. If
webmaster@1 53 // 0 is returned, the throttle logic is run, adding two additional database
webmaster@1 54 // queries. Otherwise, the following logic is skipped. This mechanism is
webmaster@1 55 // referred to in the admin page as the 'probability limiter', roughly
webmaster@1 56 // limiting throttle related database calls to 1 in N.
webmaster@1 57 if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) {
webmaster@1 58
webmaster@1 59 // Count users with activity in the past n seconds.
webmaster@1 60 // This value is defined in the user module Who's Online block.
webmaster@1 61 $time_period = variable_get('user_block_seconds_online', 900);
webmaster@1 62
webmaster@1 63 // When determining throttle status in your own module or theme, use
webmaster@1 64 // $throttle = module_invoke('throttle', 'status');
webmaster@1 65 // as that will still work when throttle.module is disabled.
webmaster@1 66 // Clearly here the module is enabled so we call throttle_status() directly.
webmaster@1 67 $throttle = throttle_status();
webmaster@1 68
webmaster@1 69 if ($max_guests = variable_get('throttle_anonymous', 0)) {
webmaster@1 70 $guests = sess_count(time() - $time_period, TRUE);
webmaster@1 71 }
webmaster@1 72 else {
webmaster@1 73 $guests = 0;
webmaster@1 74 }
webmaster@1 75 if ($max_users = variable_get('throttle_user', 0)) {
webmaster@1 76 $users = sess_count(time() - $time_period, FALSE);
webmaster@1 77 }
webmaster@1 78 else {
webmaster@1 79 $users = 0;
webmaster@1 80 }
webmaster@1 81
webmaster@1 82 // update the throttle status
webmaster@1 83 $message = '';
webmaster@1 84 if ($max_users && $users > $max_users) {
webmaster@1 85 if (!$throttle) {
webmaster@1 86 variable_set('throttle_level', 1);
webmaster@1 87 $message = format_plural($users,
webmaster@1 88 '1 user accessing site; throttle enabled.',
webmaster@1 89 '@count users accessing site; throttle enabled.');
webmaster@1 90 }
webmaster@1 91 }
webmaster@1 92 elseif ($max_guests && $guests > $max_guests) {
webmaster@1 93 if (!$throttle) {
webmaster@1 94 variable_set('throttle_level', 1);
webmaster@1 95 $message = format_plural($guests,
webmaster@1 96 '1 guest accessing site; throttle enabled.',
webmaster@1 97 '@count guests accessing site; throttle enabled.');
webmaster@1 98 }
webmaster@1 99 }
webmaster@1 100 else {
webmaster@1 101 if ($throttle) {
webmaster@1 102 variable_set('throttle_level', 0);
webmaster@1 103 // Note: unorthodox format_plural() usage due to Gettext plural limitations.
webmaster@1 104 $message = format_plural($users, '1 user', '@count users') .', ';
webmaster@1 105 $message .= format_plural($guests, '1 guest accessing site; throttle disabled', '@count guests accessing site; throttle disabled');
webmaster@1 106 }
webmaster@1 107 }
webmaster@1 108 if ($message) {
webmaster@1 109 cache_clear_all();
webmaster@1 110 watchdog('throttle', 'Throttle: %message', array('%message' => $message));
webmaster@1 111 }
webmaster@1 112 }
webmaster@1 113 }
webmaster@1 114
webmaster@1 115 /**
webmaster@1 116 * Implementation of hook_help().
webmaster@1 117 */
webmaster@1 118 function throttle_help($path, $arg) {
webmaster@1 119 switch ($path) {
webmaster@1 120 case 'admin/help#throttle':
webmaster@1 121 $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>';
webmaster@1 122 $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>';
webmaster@1 123 $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>';
webmaster@1 124 return $output;
webmaster@1 125 case 'admin/settings/throttle':
webmaster@1 126 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>';
webmaster@1 127 }
webmaster@1 128 }