| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_throttler_entry.h" | 5 #include "net/url_request/url_request_throttler_entry.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // Another way to put it is that the maximum additional perceived downtime | 32 // Another way to put it is that the maximum additional perceived downtime |
| 33 // with these numbers is a couple of seconds shy of 15 minutes, and such | 33 // with these numbers is a couple of seconds shy of 15 minutes, and such |
| 34 // a delay would not occur until the remote server has been actually | 34 // a delay would not occur until the remote server has been actually |
| 35 // unavailable at the end of each back-off period for a total of about | 35 // unavailable at the end of each back-off period for a total of about |
| 36 // 48 minutes. | 36 // 48 minutes. |
| 37 // | 37 // |
| 38 // Ignoring the first couple of errors is just a conservative measure to | 38 // Ignoring the first couple of errors is just a conservative measure to |
| 39 // avoid false positives. It should help avoid back-off from kicking in e.g. | 39 // avoid false positives. It should help avoid back-off from kicking in e.g. |
| 40 // on flaky connections. | 40 // on flaky connections. |
| 41 const int URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore = 2; | 41 const int URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore = 2; |
| 42 const int URLRequestThrottlerEntry::kDefaultInitialBackoffMs = 700; | 42 const int URLRequestThrottlerEntry::kDefaultInitialDelayMs = 700; |
| 43 const double URLRequestThrottlerEntry::kDefaultMultiplyFactor = 1.4; | 43 const double URLRequestThrottlerEntry::kDefaultMultiplyFactor = 1.4; |
| 44 const double URLRequestThrottlerEntry::kDefaultJitterFactor = 0.4; | 44 const double URLRequestThrottlerEntry::kDefaultJitterFactor = 0.4; |
| 45 const int URLRequestThrottlerEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000; | 45 const int URLRequestThrottlerEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000; |
| 46 const int URLRequestThrottlerEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000; | 46 const int URLRequestThrottlerEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000; |
| 47 const char URLRequestThrottlerEntry::kExponentialThrottlingHeader[] = | 47 const char URLRequestThrottlerEntry::kExponentialThrottlingHeader[] = |
| 48 "X-Chrome-Exponential-Throttling"; | 48 "X-Chrome-Exponential-Throttling"; |
| 49 const char URLRequestThrottlerEntry::kExponentialThrottlingDisableValue[] = | 49 const char URLRequestThrottlerEntry::kExponentialThrottlingDisableValue[] = |
| 50 "disable"; | 50 "disable"; |
| 51 | 51 |
| 52 // NetLog parameters when a request is rejected by throttling. | 52 // NetLog parameters when a request is rejected by throttling. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 DCHECK_GT(sliding_window_period_ms, 0); | 109 DCHECK_GT(sliding_window_period_ms, 0); |
| 110 DCHECK_GT(max_send_threshold_, 0); | 110 DCHECK_GT(max_send_threshold_, 0); |
| 111 DCHECK_GE(initial_backoff_ms, 0); | 111 DCHECK_GE(initial_backoff_ms, 0); |
| 112 DCHECK_GT(multiply_factor, 0); | 112 DCHECK_GT(multiply_factor, 0); |
| 113 DCHECK_GE(jitter_factor, 0.0); | 113 DCHECK_GE(jitter_factor, 0.0); |
| 114 DCHECK_LT(jitter_factor, 1.0); | 114 DCHECK_LT(jitter_factor, 1.0); |
| 115 DCHECK_GE(maximum_backoff_ms, 0); | 115 DCHECK_GE(maximum_backoff_ms, 0); |
| 116 DCHECK(manager_); | 116 DCHECK(manager_); |
| 117 | 117 |
| 118 Initialize(); | 118 Initialize(); |
| 119 backoff_policy_.initial_backoff_ms = initial_backoff_ms; | 119 backoff_policy_.initial_delay_ms = initial_backoff_ms; |
| 120 backoff_policy_.multiply_factor = multiply_factor; | 120 backoff_policy_.multiply_factor = multiply_factor; |
| 121 backoff_policy_.jitter_factor = jitter_factor; | 121 backoff_policy_.jitter_factor = jitter_factor; |
| 122 backoff_policy_.maximum_backoff_ms = maximum_backoff_ms; | 122 backoff_policy_.maximum_backoff_ms = maximum_backoff_ms; |
| 123 backoff_policy_.entry_lifetime_ms = -1; | 123 backoff_policy_.entry_lifetime_ms = -1; |
| 124 backoff_policy_.num_errors_to_ignore = 0; | 124 backoff_policy_.num_errors_to_ignore = 0; |
| 125 backoff_policy_.always_use_initial_delay = false; |
| 125 } | 126 } |
| 126 | 127 |
| 127 bool URLRequestThrottlerEntry::IsEntryOutdated() const { | 128 bool URLRequestThrottlerEntry::IsEntryOutdated() const { |
| 128 // This function is called by the URLRequestThrottlerManager to determine | 129 // This function is called by the URLRequestThrottlerManager to determine |
| 129 // whether entries should be discarded from its url_entries_ map. We | 130 // whether entries should be discarded from its url_entries_ map. We |
| 130 // want to ensure that it does not remove entries from the map while there | 131 // want to ensure that it does not remove entries from the map while there |
| 131 // are clients (objects other than the manager) holding references to | 132 // are clients (objects other than the manager) holding references to |
| 132 // the entry, otherwise separate clients could end up holding separate | 133 // the entry, otherwise separate clients could end up holding separate |
| 133 // entries for a request to the same URL, which is undesirable. Therefore, | 134 // entries for a request to the same URL, which is undesirable. Therefore, |
| 134 // if an entry has more than one reference (the map will always hold one), | 135 // if an entry has more than one reference (the map will always hold one), |
| (...skipping 21 matching lines...) Expand all Loading... |
| 156 void URLRequestThrottlerEntry::DetachManager() { | 157 void URLRequestThrottlerEntry::DetachManager() { |
| 157 manager_ = NULL; | 158 manager_ = NULL; |
| 158 } | 159 } |
| 159 | 160 |
| 160 bool URLRequestThrottlerEntry::ShouldRejectRequest(int load_flags) const { | 161 bool URLRequestThrottlerEntry::ShouldRejectRequest(int load_flags) const { |
| 161 bool reject_request = false; | 162 bool reject_request = false; |
| 162 if (!is_backoff_disabled_ && !ExplicitUserRequest(load_flags) && | 163 if (!is_backoff_disabled_ && !ExplicitUserRequest(load_flags) && |
| 163 GetBackoffEntry()->ShouldRejectRequest()) { | 164 GetBackoffEntry()->ShouldRejectRequest()) { |
| 164 int num_failures = GetBackoffEntry()->failure_count(); | 165 int num_failures = GetBackoffEntry()->failure_count(); |
| 165 int release_after_ms = | 166 int release_after_ms = |
| 166 (GetBackoffEntry()->GetReleaseTime() - base::TimeTicks::Now()) | 167 GetBackoffEntry()->GetTimeUntilRelease().InMilliseconds(); |
| 167 .InMilliseconds(); | |
| 168 | 168 |
| 169 net_log_.AddEvent( | 169 net_log_.AddEvent( |
| 170 NetLog::TYPE_THROTTLING_REJECTED_REQUEST, | 170 NetLog::TYPE_THROTTLING_REJECTED_REQUEST, |
| 171 make_scoped_refptr( | 171 make_scoped_refptr( |
| 172 new RejectedRequestParameters(url_id_, | 172 new RejectedRequestParameters(url_id_, |
| 173 num_failures, | 173 num_failures, |
| 174 release_after_ms))); | 174 release_after_ms))); |
| 175 | 175 |
| 176 reject_request = true; | 176 reject_request = true; |
| 177 } | 177 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 GetBackoffEntry()->InformOfRequest(false); | 264 GetBackoffEntry()->InformOfRequest(false); |
| 265 } | 265 } |
| 266 } | 266 } |
| 267 | 267 |
| 268 URLRequestThrottlerEntry::~URLRequestThrottlerEntry() { | 268 URLRequestThrottlerEntry::~URLRequestThrottlerEntry() { |
| 269 } | 269 } |
| 270 | 270 |
| 271 void URLRequestThrottlerEntry::Initialize() { | 271 void URLRequestThrottlerEntry::Initialize() { |
| 272 sliding_window_release_time_ = base::TimeTicks::Now(); | 272 sliding_window_release_time_ = base::TimeTicks::Now(); |
| 273 backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore; | 273 backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore; |
| 274 backoff_policy_.initial_backoff_ms = kDefaultInitialBackoffMs; | 274 backoff_policy_.initial_delay_ms = kDefaultInitialDelayMs; |
| 275 backoff_policy_.multiply_factor = kDefaultMultiplyFactor; | 275 backoff_policy_.multiply_factor = kDefaultMultiplyFactor; |
| 276 backoff_policy_.jitter_factor = kDefaultJitterFactor; | 276 backoff_policy_.jitter_factor = kDefaultJitterFactor; |
| 277 backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs; | 277 backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs; |
| 278 backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs; | 278 backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs; |
| 279 backoff_policy_.always_use_initial_delay = false; |
| 279 | 280 |
| 280 // We pretend we just had a successful response so that we have a | 281 // We pretend we just had a successful response so that we have a |
| 281 // starting point to our tracking. This is called from the | 282 // starting point to our tracking. This is called from the |
| 282 // constructor so we do not use the virtual ImplGetTimeNow(). | 283 // constructor so we do not use the virtual ImplGetTimeNow(). |
| 283 last_successful_response_time_ = base::TimeTicks::Now(); | 284 last_successful_response_time_ = base::TimeTicks::Now(); |
| 284 last_response_was_success_ = true; | 285 last_response_was_success_ = true; |
| 285 } | 286 } |
| 286 | 287 |
| 287 bool URLRequestThrottlerEntry::IsConsideredError(int response_code) { | 288 bool URLRequestThrottlerEntry::IsConsideredError(int response_code) { |
| 288 // We throttle only for the status codes most likely to indicate the server | 289 // We throttle only for the status codes most likely to indicate the server |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { | 357 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { |
| 357 return &backoff_entry_; | 358 return &backoff_entry_; |
| 358 } | 359 } |
| 359 | 360 |
| 360 // static | 361 // static |
| 361 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { | 362 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { |
| 362 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; | 363 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; |
| 363 } | 364 } |
| 364 | 365 |
| 365 } // namespace net | 366 } // namespace net |
| OLD | NEW |