home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / drupal-6.0.exe / drupal-6.0 / modules / throttle / throttle.module < prev   
Encoding:
Text File  |  2007-12-14  |  5.7 KB  |  129 lines

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