Chromium Code Reviews| 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" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 16 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_throttler_header_interface.h" | |
| 20 #include "net/url_request/url_request_throttler_manager.h" | 19 #include "net/url_request/url_request_throttler_manager.h" |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 23 | 22 |
| 24 const int URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs = 2000; | 23 const int URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs = 2000; |
| 25 const int URLRequestThrottlerEntry::kDefaultMaxSendThreshold = 20; | 24 const int URLRequestThrottlerEntry::kDefaultMaxSendThreshold = 20; |
| 26 | 25 |
| 27 // This set of back-off parameters will (at maximum values, i.e. without | 26 // This set of back-off parameters will (at maximum values, i.e. without |
| 28 // the reduction caused by jitter) add 0-41% (distributed uniformly | 27 // the reduction caused by jitter) add 0-41% (distributed uniformly |
| 29 // in that range) to the "perceived downtime" of the remote server, once | 28 // in that range) to the "perceived downtime" of the remote server, once |
| 30 // exponential back-off kicks in and is throttling requests for more than | 29 // exponential back-off kicks in and is throttling requests for more than |
| 31 // about a second at a time. Once the maximum back-off is reached, the added | 30 // about a second at a time. Once the maximum back-off is reached, the added |
| 32 // perceived downtime decreases rapidly, percentage-wise. | 31 // perceived downtime decreases rapidly, percentage-wise. |
| 33 // | 32 // |
| 34 // Another way to put it is that the maximum additional perceived downtime | 33 // Another way to put it is that the maximum additional perceived downtime |
| 35 // with these numbers is a couple of seconds shy of 15 minutes, and such | 34 // with these numbers is a couple of seconds shy of 15 minutes, and such |
| 36 // a delay would not occur until the remote server has been actually | 35 // a delay would not occur until the remote server has been actually |
| 37 // unavailable at the end of each back-off period for a total of about | 36 // unavailable at the end of each back-off period for a total of about |
| 38 // 48 minutes. | 37 // 48 minutes. |
| 39 // | 38 // |
| 40 // Ignoring the first couple of errors is just a conservative measure to | 39 // Ignoring the first couple of errors is just a conservative measure to |
| 41 // avoid false positives. It should help avoid back-off from kicking in e.g. | 40 // avoid false positives. It should help avoid back-off from kicking in e.g. |
| 42 // on flaky connections. | 41 // on flaky connections. |
| 43 const int URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore = 2; | 42 const int URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore = 2; |
| 44 const int URLRequestThrottlerEntry::kDefaultInitialDelayMs = 700; | 43 const int URLRequestThrottlerEntry::kDefaultInitialDelayMs = 700; |
| 45 const double URLRequestThrottlerEntry::kDefaultMultiplyFactor = 1.4; | 44 const double URLRequestThrottlerEntry::kDefaultMultiplyFactor = 1.4; |
| 46 const double URLRequestThrottlerEntry::kDefaultJitterFactor = 0.4; | 45 const double URLRequestThrottlerEntry::kDefaultJitterFactor = 0.4; |
| 47 const int URLRequestThrottlerEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000; | 46 const int URLRequestThrottlerEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000; |
| 48 const int URLRequestThrottlerEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000; | 47 const int URLRequestThrottlerEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000; |
| 49 const char URLRequestThrottlerEntry::kExponentialThrottlingHeader[] = | |
| 50 "X-Chrome-Exponential-Throttling"; | |
| 51 const char URLRequestThrottlerEntry::kExponentialThrottlingDisableValue[] = | |
| 52 "disable"; | |
| 53 | 48 |
| 54 // Returns NetLog parameters when a request is rejected by throttling. | 49 // Returns NetLog parameters when a request is rejected by throttling. |
| 55 base::Value* NetLogRejectedRequestCallback( | 50 base::Value* NetLogRejectedRequestCallback( |
| 56 const std::string* url_id, | 51 const std::string* url_id, |
| 57 int num_failures, | 52 int num_failures, |
| 58 const base::TimeDelta& release_after, | 53 const base::TimeDelta& release_after, |
| 59 NetLogCaptureMode /* capture_mode */) { | 54 NetLogCaptureMode /* capture_mode */) { |
| 60 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 55 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 61 dict->SetString("url", *url_id); | 56 dict->SetString("url", *url_id); |
| 62 dict->SetInteger("num_failures", num_failures); | 57 dict->SetInteger("num_failures", num_failures); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 // the back-off mechanism when it shouldn't be triggered, in which case | 210 // the back-off mechanism when it shouldn't be triggered, in which case |
| 216 // returning the calculated back-off release time would probably be the | 211 // returning the calculated back-off release time would probably be the |
| 217 // wrong thing to do (i.e. it would likely be too long). Therefore, we | 212 // wrong thing to do (i.e. it would likely be too long). Therefore, we |
| 218 // return "now" so that retries are not delayed. | 213 // return "now" so that retries are not delayed. |
| 219 if (is_backoff_disabled_) | 214 if (is_backoff_disabled_) |
| 220 return ImplGetTimeNow(); | 215 return ImplGetTimeNow(); |
| 221 | 216 |
| 222 return GetBackoffEntry()->GetReleaseTime(); | 217 return GetBackoffEntry()->GetReleaseTime(); |
| 223 } | 218 } |
| 224 | 219 |
| 225 void URLRequestThrottlerEntry::UpdateWithResponse( | 220 void URLRequestThrottlerEntry::UpdateWithResponse(const std::string& host, |
|
mmenke
2015/05/19 19:18:37
This method no longer needs the "host" argument.
xunjieli
2015/05/19 20:06:44
Done. thanks!
| |
| 226 const std::string& host, | 221 int status_code) { |
| 227 const URLRequestThrottlerHeaderInterface* response) { | 222 if (IsConsideredError(status_code)) { |
| 228 if (IsConsideredError(response->GetResponseCode())) { | |
| 229 GetBackoffEntry()->InformOfRequest(false); | 223 GetBackoffEntry()->InformOfRequest(false); |
| 230 } else { | 224 } else { |
| 231 GetBackoffEntry()->InformOfRequest(true); | 225 GetBackoffEntry()->InformOfRequest(true); |
| 232 | |
| 233 std::string throttling_header = response->GetNormalizedValue( | |
| 234 kExponentialThrottlingHeader); | |
| 235 if (!throttling_header.empty()) | |
| 236 HandleThrottlingHeader(throttling_header, host); | |
| 237 } | 226 } |
|
mmenke
2015/05/19 19:18:37
nit: Can just collapse this block to:
GetBackoff
xunjieli
2015/05/19 20:06:44
Done.
| |
| 238 } | 227 } |
| 239 | 228 |
| 240 void URLRequestThrottlerEntry::ReceivedContentWasMalformed(int response_code) { | 229 void URLRequestThrottlerEntry::ReceivedContentWasMalformed(int response_code) { |
| 241 // A malformed body can only occur when the request to fetch a resource | 230 // A malformed body can only occur when the request to fetch a resource |
| 242 // was successful. Therefore, in such a situation, we will receive one | 231 // was successful. Therefore, in such a situation, we will receive one |
| 243 // call to ReceivedContentWasMalformed() and one call to | 232 // call to ReceivedContentWasMalformed() and one call to |
| 244 // UpdateWithResponse() with a response categorized as "good". To end | 233 // UpdateWithResponse() with a response categorized as "good". To end |
| 245 // up counting one failure, we need to count two failures here against | 234 // up counting one failure, we need to count two failures here against |
| 246 // the one success in UpdateWithResponse(). | 235 // the one success in UpdateWithResponse(). |
| 247 // | 236 // |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 // localhost, where you are not actually connected to the network. | 276 // localhost, where you are not actually connected to the network. |
| 288 return (response_code == 500 || | 277 return (response_code == 500 || |
| 289 response_code == 503 || | 278 response_code == 503 || |
| 290 response_code == 509); | 279 response_code == 509); |
| 291 } | 280 } |
| 292 | 281 |
| 293 base::TimeTicks URLRequestThrottlerEntry::ImplGetTimeNow() const { | 282 base::TimeTicks URLRequestThrottlerEntry::ImplGetTimeNow() const { |
| 294 return base::TimeTicks::Now(); | 283 return base::TimeTicks::Now(); |
| 295 } | 284 } |
| 296 | 285 |
| 297 void URLRequestThrottlerEntry::HandleThrottlingHeader( | |
| 298 const std::string& header_value, | |
| 299 const std::string& host) { | |
| 300 if (header_value == kExponentialThrottlingDisableValue) { | |
| 301 DisableBackoffThrottling(); | |
| 302 if (manager_) | |
| 303 manager_->AddToOptOutList(host); | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 const BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() const { | 286 const BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() const { |
| 308 return &backoff_entry_; | 287 return &backoff_entry_; |
| 309 } | 288 } |
| 310 | 289 |
| 311 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { | 290 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { |
| 312 return &backoff_entry_; | 291 return &backoff_entry_; |
| 313 } | 292 } |
| 314 | 293 |
| 315 // static | 294 // static |
| 316 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { | 295 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { |
| 317 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; | 296 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; |
| 318 } | 297 } |
| 319 | 298 |
| 320 } // namespace net | 299 } // namespace net |
| OLD | NEW |