[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/throttle/ -> throttle.module (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7