throttle.module

Allows configuration of congestion control auto-throttle mechanism.

File

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