[ Index ] |
|
Code source de Drupal 5.3 |
1 <?php 2 // $Id: throttle.module,v 1.72 2006/12/20 10:29:31 dries Exp $ 3 4 /** 5 * @file 6 * Allows configuration of congestion control auto-throttle mechanism. 7 */ 8 9 function throttle_menu($may_cache) { 10 $items = array(); 11 12 if ($may_cache) { 13 $items[] = array( 14 'path' => 'admin/settings/throttle', 15 'description' => t('Control how your site cuts out content during heavy load.'), 16 'title' => t('Throttle'), 17 'callback' => 'drupal_get_form', 18 'callback arguments' => array('throttle_admin_settings'), 19 'access' => user_access('administer site configuration'), 20 'type' => MENU_NORMAL_ITEM 21 ); 22 } 23 24 return $items; 25 } 26 27 /** 28 * Determine the current load on the site. 29 * 30 * Call the throttle_status() function from your own modules, themes, blocks, 31 * etc. as follows: 32 * 33 * $throttle = module_invoke('throttle', 'status'); 34 * 35 * to determine the current throttle status. Use module_invoke() so the 36 * call will still work if the throttle module is disabled. For example, in 37 * your theme you might choose to disable pictures when your site is too busy 38 * (reducing bandwidth), or in your modules you might choose to disable 39 * some complicated logic when your site is too busy (reducing CPU utilization). 40 * 41 * @return 42 * 0 or 1. 0 means that the throttle is currently disabled. 1 means that 43 * the throttle is currently enabled. When the throttle is enabled, CPU 44 * and bandwidth intensive functionality should be disabled. 45 */ 46 function throttle_status() { 47 return variable_get('throttle_level', 0); 48 } 49 50 /** 51 * Implementation of hook_exit(). 52 * 53 * Changes the current throttle level based on page hits. 54 */ 55 function throttle_exit() { 56 // The following logic determines what the current throttle level should 57 // be, and can be disabled by the admin. If enabled, the mt_rand() function 58 // returns a number between 0 and N, N being specified by the admin. If 59 // 0 is returned, the throttle logic is run, adding two additional database 60 // queries. Otherwise, the following logic is skipped. This mechanism is 61 // referred to in the admin page as the 'probability limiter', roughly 62 // limiting throttle related database calls to 1 in N. 63 if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) { 64 65 // Count users with activity in the past n seconds. 66 // This value is defined in the user module Who's Online block. 67 $time_period = variable_get('user_block_seconds_online', 900); 68 69 // When determining throttle status in your own module or theme, use 70 // $throttle = module_invoke('throttle', 'status'); 71 // as that will still work when throttle.module is disabled. 72 // Clearly here the module is enabled so we call throttle_status() directly. 73 $throttle = throttle_status(); 74 75 if ($max_guests = variable_get('throttle_anonymous', 0)) { 76 $guests = sess_count(time() - $time_period, TRUE); 77 } 78 else { 79 $guests = 0; 80 } 81 if ($max_users = variable_get('throttle_user', 0)) { 82 $users = sess_count(time() - $time_period, FALSE); 83 } 84 else { 85 $users = 0; 86 } 87 88 // update the throttle status 89 $message = ''; 90 if ($max_users && $users > $max_users) { 91 if (!$throttle) { 92 variable_set('throttle_level', 1); 93 $message = format_plural($users, 94 '1 user accessing site; throttle enabled.', 95 '@count users accessing site; throttle enabled.'); 96 } 97 } 98 elseif ($max_guests && $guests > $max_guests) { 99 if (!$throttle) { 100 variable_set('throttle_level', 1); 101 $message = format_plural($guests, 102 '1 guest accessing site; throttle enabled.', 103 '@count guests accessing site; throttle enabled.'); 104 } 105 } 106 else { 107 if ($throttle) { 108 variable_set('throttle_level', 0); 109 // Note: unorthodox format_plural() usage due to Gettext plural limitations. 110 $message = format_plural($users, '1 user', '@count users') .', '; 111 $message .= format_plural($guests, '1 guest accessing site; throttle disabled', '@count guests accessing site; throttle disabled'); 112 } 113 } 114 if ($message) { 115 cache_clear_all(); 116 watchdog('throttle', t('Throttle') .': '. $message); 117 } 118 } 119 } 120 121 /** 122 * Implementation of hook_help(). 123 */ 124 function throttle_help($section) { 125 switch ($section) { 126 case 'admin/help#throttle': 127 $output = '<p>'. t('The throttle module provides a congestion control throttling mechanism for automatically detecting a surge in incoming traffic. If the site gets linked to by a popular website, or otherwise comes under a "Denial of Service" (DoS) attack, your webserver might become overwhelmed. This mechanism is utilized by other modules to automatically optimize their performance by temporarily disabling CPU-intensive functionality. For example, in the site theme, you might choose to disable pictures when the site is too busy (reducing bandwidth), or in modules, you might choose to disable some complicated logic (reducing CPU utilization).') .'</p>'; 128 $output .= '<p>'. t('The congestion control throttle can be automatically enabled when the number of anonymous or authenticated users currently visiting the site exceeds the specified threshold. ') .'</p>'; 129 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@throttle">Throttle page</a>.', array('@throttle' => 'http://drupal.org/handbook/modules/throttle/')) .'</p>'; 130 return $output; 131 case 'admin/settings/throttle': 132 return '<p>'. 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 congestion control throttling mechanism for automatically detecting a surge in incoming traffic. This mechanism is utilized by other Drupal modules to automatically optimize their performance by temporarily disabling CPU-intensive functionality.') .'</p>'; 133 } 134 } 135 136 function throttle_admin_settings() { 137 $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%'); 138 139 $form['throttle_anonymous'] = array( 140 '#type' => 'textfield', 141 '#title' => t('Auto-throttle on anonymous users'), 142 '#default_value' => variable_get('throttle_anonymous', 0), 143 '#size' => 5, 144 '#maxlength' => 6, 145 '#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.') 146 ); 147 $form['throttle_user'] = array( 148 '#type' => 'textfield', 149 '#title' => t('Auto-throttle on authenticated users'), 150 '#default_value' => variable_get('throttle_user', 0), 151 '#size' => 5, 152 '#maxlength' => 6, 153 '#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.') 154 ); 155 $form['throttle_probability_limiter'] = array( 156 '#type' => 'select', 157 '#title' => t('Auto-throttle probability limiter'), 158 '#default_value' => variable_get('throttle_probability_limiter', 9), 159 '#options' => $probabilities, 160 '#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.') 161 ); 162 163 return system_settings_form($form); 164 } 165 166 function throttle_admin_settings_validate($form_id, $form_values) { 167 if (!is_numeric($form_values['throttle_anonymous']) || $form_values['throttle_anonymous'] < 0) { 168 form_set_error('throttle_anonymous', t("%value is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $form_values['throttle_anonymous']))); 169 } 170 if (!is_numeric($form_values['throttle_user']) || $form_values['throttle_user'] < 0) { 171 form_set_error('throttle_user', t("%value is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $form_values['throttle_user']))); 172 } 173 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Nov 30 16:20:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |