From f10cf4756e1ba8f07c78ab919de09c28db212650 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 30 Nov 2003 10:37:07 +0000 Subject: - Better separation between throttle and statistics module code. Patch by Jeremy. --- modules/throttle.module | 126 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 110 insertions(+), 16 deletions(-) (limited to 'modules/throttle.module') diff --git a/modules/throttle.module b/modules/throttle.module index cfd8ad9d6..5186fd60e 100644 --- a/modules/throttle.module +++ b/modules/throttle.module @@ -1,6 +1,58 @@ = %d", (time() - 60))); + _throttle_update($recent_activity->hits); + } + } +} + function throttle_perm() { /* ** throttle module defines the following permissions: @@ -52,10 +104,10 @@ function throttle_help($section = "admin/help#throttle") { } -/* Adds configure option to the main configure site admin page */ +// throttle module configuration options function throttle_settings() { // enable/disable auto-throttle - $group = form_radios(t("Enable auto-throttle"), "throttle_enable", variable_get("throttle_enable", 0), array("1" => t("enabled"), "0" => t("disabled")), "Enable auto-throttling congestion control mechanism. Allows your site to adapt under extreme user loads, such as being linked by Slashdot. To use the auto-throttle you must also enable the 'access log' from the statistics module."); + $group = form_radios(t("Enable auto-throttle"), "throttle_enable", variable_get("throttle_enable", 0), array("1" => t("enabled"), "0" => t("disabled")), t("Enable auto-throttling congestion control mechanism. Allows your site to adapt under extreme user loads, such as being linked by Slashdot. To use the auto-throttle you must also enable the '%access_log' from the statistics module.", array("%access_log" => l(t("access log"), "admin/system/modules/statistics")))); $output = form_group(t("Auto-throttle status"), $group); // tune auto-throttle @@ -70,11 +122,20 @@ function throttle_settings() { return $output; } +function throttle_cron() { + $throttle = throttle_status(); + /* check if throttle is currently on and if it's time to drop level */ + if (($throttle) && ((time() - variable_get("throttle_cron_timer", 10800)) > variable_get("throttle_cron_timestamp", 0))) { + /* If throttle is on, back off one notch to test server load */ + variable_set("throttle_level", $throttle - 1); + variable_set("throttle_cron_timestamp", time()); + watchdog("warning", t("cron: decreasing throttle to level '%level' to test server load.", array("%level" => ($throttle - 1)))); + } +} + -/* This displays admin oriented "Throttle status" block */ +// displays admin oriented "Throttle status" block function throttle_display_throttle_block() { - global $recent_activity; - if (user_access("access throttle block")) { if (variable_get("throttle_enable", 0)) { /* the throttle is enabled: display the status of all throttle config */ @@ -85,29 +146,27 @@ function throttle_display_throttle_block() { /* calculate probability limiter's odds of updating throttle */ $probability = substr((($limiter / ($limiter + 1) * 100) - 100) * -1, 0, 4); - $output .= "Throttle: ". l(t("enabled"), "admin/system/modules/throttle") ."
\n"; + $output .= t("Throttle: %status", array("%status" => l(t("enabled"), "admin/system/modules/throttle"))) ."
\n"; if ($throttle < 5) { $maximum = (($throttle + 1) * $multiplier) - 1; - $output .= "Current level: $throttle ($minimum - $maximum)
\n"; + $output .= t("Current level: %level (%min - %max)", array("%level" => $throttle, "%min" => $minimum, "%max" => $maximum)) ."
\n"; } else { - $output .= "Current level: $throttle ($minimum+)
\n"; - } - $output .= "Probability: $probability%
\n"; - if ($recent_activity["hits"]) { - $output .= "
This site has served "; - $output .= format_plural($recent_activity["hits"] , "1 page", "%count pages"); - $output .= " in the past minute."; + $output .= t("Current level: %level (%min+)", array("%level" => $throttle, "%min" => $minimum)) ."
\n"; } + $output .= t("Probability: %probability%", array("%probability" => $probability)) ."
\n"; + $recent_activity = db_fetch_object(db_query("SELECT COUNT(timestamp) AS hits FROM {accesslog} WHERE timestamp >= %d", (time() - 60))); + $output .= "
". t("This site has served %hits in the past minute.", array("%hits" => format_plural($recent_activity->hits , "1 page", "%count pages"))); + _throttle_update($recent_activity->hits); } else { - $output .= "Throttle: ". l(t("disabled"), "admin/system/modules/throttle") ."
\n"; + $output .= t("Throttle: %status", array("%status" => l(t("disabled"), "admin/system/modules/throttle"))) ."
\n"; } } return $output; } -/* Block hook */ +// block hook function throttle_block($op = "list", $delta = 0) { if ($op == "list") { $blocks[0]["info"] = t("Throttle status"); @@ -120,4 +179,39 @@ function throttle_block($op = "list", $delta = 0) { } } +function _throttle_update($hits) { + $throttle = throttle_status(); + $multiplier = variable_get("throttle_multiplier", 60); + + for ($i = 0; $i <= 5; $i++) { + if (($i * $multiplier) <= $hits) { + $throttle_new = $i; + } + } + + if ($throttle_new != $throttle) { + /* + ** reduce throttle if new throttle would be 3+ less than current throttle, + ** (all other throttle reduction done by _cron hook), increase throttle if + ** new throttle would be greater than current throttle. + */ + if (($throttle_new < ($throttle - 2)) || ($throttle_new > $throttle)) { + /* update throttle level */ + variable_set("throttle_level", $throttle_new); + /* + ** update the global timestamp, preventing cron.php from jumping in + ** too quickly, allowing for user defined period to first pass. + */ + variable_set("throttle_cron_timestamp", time()); + /* log the change */ + if ($throttle_new < $throttle) { + watchdog("message", "message: '". $hits ."' hits in past minute; throttle decreased to level ". $throttle_new); + } + else { + watchdog("warning", "warning: '". $hits ."' hits in past minute; throttle increased to level ". $throttle_new); + } + } + } +} + ?> -- cgit v1.2.3