webmaster@1: 'Throttle', webmaster@1: 'description' => 'Control how your site cuts out content during heavy load.', webmaster@1: 'page callback' => 'drupal_get_form', webmaster@1: 'page arguments' => array('throttle_admin_settings'), webmaster@1: 'access arguments' => array('administer site configuration'), webmaster@1: 'file' => 'throttle.admin.inc', webmaster@1: ); webmaster@1: return $items; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Determine the current load on the site. webmaster@1: * webmaster@1: * Call the throttle_status() function from your own modules, themes, blocks, webmaster@1: * etc. as follows: webmaster@1: * webmaster@1: * $throttle = module_invoke('throttle', 'status'); webmaster@1: * webmaster@1: * to determine the current throttle status. Use module_invoke() so the webmaster@1: * call will still work if the throttle module is disabled. For example, in webmaster@1: * your theme you might choose to disable pictures when your site is too busy webmaster@1: * (reducing bandwidth), or in your modules you might choose to disable webmaster@1: * some complicated logic when your site is too busy (reducing CPU utilization). webmaster@1: * webmaster@1: * @return webmaster@1: * 0 or 1. 0 means that the throttle is currently disabled. 1 means that webmaster@1: * the throttle is currently enabled. When the throttle is enabled, CPU webmaster@1: * and bandwidth intensive functionality should be disabled. webmaster@1: */ webmaster@1: function throttle_status() { webmaster@1: return variable_get('throttle_level', 0); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_exit(). webmaster@1: * webmaster@1: * Changes the current throttle level based on page hits. webmaster@1: */ webmaster@1: function throttle_exit() { webmaster@1: // The following logic determines what the current throttle level should webmaster@1: // be, and can be disabled by the admin. If enabled, the mt_rand() function webmaster@1: // returns a number between 0 and N, N being specified by the admin. If webmaster@1: // 0 is returned, the throttle logic is run, adding two additional database webmaster@1: // queries. Otherwise, the following logic is skipped. This mechanism is webmaster@1: // referred to in the admin page as the 'probability limiter', roughly webmaster@1: // limiting throttle related database calls to 1 in N. webmaster@1: if (!mt_rand(0, variable_get('throttle_probability_limiter', 9))) { webmaster@1: webmaster@1: // Count users with activity in the past n seconds. webmaster@1: // This value is defined in the user module Who's Online block. webmaster@1: $time_period = variable_get('user_block_seconds_online', 900); webmaster@1: webmaster@1: // When determining throttle status in your own module or theme, use webmaster@1: // $throttle = module_invoke('throttle', 'status'); webmaster@1: // as that will still work when throttle.module is disabled. webmaster@1: // Clearly here the module is enabled so we call throttle_status() directly. webmaster@1: $throttle = throttle_status(); webmaster@1: webmaster@1: if ($max_guests = variable_get('throttle_anonymous', 0)) { webmaster@1: $guests = sess_count(time() - $time_period, TRUE); webmaster@1: } webmaster@1: else { webmaster@1: $guests = 0; webmaster@1: } webmaster@1: if ($max_users = variable_get('throttle_user', 0)) { webmaster@1: $users = sess_count(time() - $time_period, FALSE); webmaster@1: } webmaster@1: else { webmaster@1: $users = 0; webmaster@1: } webmaster@1: webmaster@1: // update the throttle status webmaster@1: $message = ''; webmaster@1: if ($max_users && $users > $max_users) { webmaster@1: if (!$throttle) { webmaster@1: variable_set('throttle_level', 1); webmaster@1: $message = format_plural($users, webmaster@1: '1 user accessing site; throttle enabled.', webmaster@1: '@count users accessing site; throttle enabled.'); webmaster@1: } webmaster@1: } webmaster@1: elseif ($max_guests && $guests > $max_guests) { webmaster@1: if (!$throttle) { webmaster@1: variable_set('throttle_level', 1); webmaster@1: $message = format_plural($guests, webmaster@1: '1 guest accessing site; throttle enabled.', webmaster@1: '@count guests accessing site; throttle enabled.'); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: if ($throttle) { webmaster@1: variable_set('throttle_level', 0); webmaster@1: // Note: unorthodox format_plural() usage due to Gettext plural limitations. webmaster@1: $message = format_plural($users, '1 user', '@count users') .', '; webmaster@1: $message .= format_plural($guests, '1 guest accessing site; throttle disabled', '@count guests accessing site; throttle disabled'); webmaster@1: } webmaster@1: } webmaster@1: if ($message) { webmaster@1: cache_clear_all(); webmaster@1: watchdog('throttle', 'Throttle: %message', array('%message' => $message)); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_help(). webmaster@1: */ webmaster@1: function throttle_help($path, $arg) { webmaster@1: switch ($path) { webmaster@1: case 'admin/help#throttle': webmaster@1: $output = '
'. 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.') .'
'; webmaster@1: $output .= ''. 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.') .'
'; webmaster@1: $output .= ''. t('For more information, see the online handbook entry for Throttle module.', array('@throttle' => 'http://drupal.org/handbook/modules/throttle/')) .'
'; webmaster@1: return $output; webmaster@1: case 'admin/settings/throttle': webmaster@1: return ''. 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.') .'
'; webmaster@1: } webmaster@1: }