xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "extensions/browser/extension_throttle_entry.h" |
| 6 | |
| 7 | #include <cmath> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 8 | #include <utility> |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/rand_util.h" |
| 12 | #include "base/strings/string_number_conversions.h" |
| 13 | #include "base/values.h" |
| 14 | #include "extensions/browser/extension_throttle_manager.h" |
| 15 | #include "net/base/load_flags.h" |
| 16 | #include "net/log/net_log.h" |
| 17 | #include "net/url_request/url_request.h" |
| 18 | #include "net/url_request/url_request_context.h" |
| 19 | |
| 20 | namespace extensions { |
| 21 | |
| 22 | const int ExtensionThrottleEntry::kDefaultSlidingWindowPeriodMs = 2000; |
| 23 | const int ExtensionThrottleEntry::kDefaultMaxSendThreshold = 20; |
| 24 | |
| 25 | // This set of back-off parameters will (at maximum values, i.e. without |
| 26 | // the reduction caused by jitter) add 0-41% (distributed uniformly |
| 27 | // in that range) to the "perceived downtime" of the remote server, once |
| 28 | // exponential back-off kicks in and is throttling requests for more than |
| 29 | // about a second at a time. Once the maximum back-off is reached, the added |
| 30 | // perceived downtime decreases rapidly, percentage-wise. |
| 31 | // |
| 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 |
| 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 |
| 36 | // 48 minutes. |
| 37 | // |
| 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. |
| 40 | // on flaky connections. |
| 41 | const int ExtensionThrottleEntry::kDefaultNumErrorsToIgnore = 2; |
| 42 | const int ExtensionThrottleEntry::kDefaultInitialDelayMs = 700; |
| 43 | const double ExtensionThrottleEntry::kDefaultMultiplyFactor = 1.4; |
| 44 | const double ExtensionThrottleEntry::kDefaultJitterFactor = 0.4; |
| 45 | const int ExtensionThrottleEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000; |
| 46 | const int ExtensionThrottleEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000; |
| 47 | |
| 48 | // Returns NetLog parameters when a request is rejected by throttling. |
| 49 | scoped_ptr<base::Value> NetLogRejectedRequestCallback( |
| 50 | const std::string* url_id, |
| 51 | int num_failures, |
| 52 | const base::TimeDelta& release_after, |
| 53 | net::NetLogCaptureMode /* capture_mode */) { |
| 54 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 55 | dict->SetString("url", *url_id); |
| 56 | dict->SetInteger("num_failures", num_failures); |
| 57 | dict->SetInteger("release_after_ms", |
| 58 | static_cast<int>(release_after.InMilliseconds())); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 59 | return std::move(dict); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ExtensionThrottleEntry::ExtensionThrottleEntry( |
| 63 | ExtensionThrottleManager* manager, |
| 64 | const std::string& url_id) |
| 65 | : ExtensionThrottleEntry(manager, url_id, false) { |
| 66 | } |
| 67 | |
| 68 | ExtensionThrottleEntry::ExtensionThrottleEntry( |
| 69 | ExtensionThrottleManager* manager, |
| 70 | const std::string& url_id, |
| 71 | bool ignore_user_gesture_load_flag_for_tests) |
| 72 | : sliding_window_period_( |
| 73 | base::TimeDelta::FromMilliseconds(kDefaultSlidingWindowPeriodMs)), |
| 74 | max_send_threshold_(kDefaultMaxSendThreshold), |
| 75 | is_backoff_disabled_(false), |
| 76 | backoff_entry_(&backoff_policy_), |
| 77 | manager_(manager), |
| 78 | url_id_(url_id), |
| 79 | net_log_(net::BoundNetLog::Make( |
| 80 | manager->net_log(), |
| 81 | net::NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING)), |
| 82 | ignore_user_gesture_load_flag_for_tests_( |
| 83 | ignore_user_gesture_load_flag_for_tests) { |
| 84 | DCHECK(manager_); |
| 85 | Initialize(); |
| 86 | } |
| 87 | |
| 88 | ExtensionThrottleEntry::ExtensionThrottleEntry( |
| 89 | ExtensionThrottleManager* manager, |
| 90 | const std::string& url_id, |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 91 | const net::BackoffEntry::Policy* backoff_policy, |
| 92 | bool ignore_user_gesture_load_flag_for_tests) |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 93 | : sliding_window_period_( |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 94 | base::TimeDelta::FromMilliseconds(kDefaultSlidingWindowPeriodMs)), |
| 95 | max_send_threshold_(kDefaultMaxSendThreshold), |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 96 | is_backoff_disabled_(false), |
| 97 | backoff_entry_(&backoff_policy_), |
| 98 | manager_(manager), |
| 99 | url_id_(url_id), |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 100 | ignore_user_gesture_load_flag_for_tests_( |
| 101 | ignore_user_gesture_load_flag_for_tests) { |
| 102 | DCHECK_GE(backoff_policy->initial_delay_ms, 0); |
| 103 | DCHECK_GT(backoff_policy->multiply_factor, 0); |
| 104 | DCHECK_GE(backoff_policy->jitter_factor, 0.0); |
| 105 | DCHECK_LT(backoff_policy->jitter_factor, 1.0); |
| 106 | DCHECK_GE(backoff_policy->maximum_backoff_ms, 0); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 107 | DCHECK(manager_); |
| 108 | |
| 109 | Initialize(); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 110 | backoff_policy_ = *backoff_policy; |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool ExtensionThrottleEntry::IsEntryOutdated() const { |
| 114 | // This function is called by the ExtensionThrottleManager to determine |
| 115 | // whether entries should be discarded from its url_entries_ map. We |
| 116 | // want to ensure that it does not remove entries from the map while there |
| 117 | // are clients (objects other than the manager) holding references to |
| 118 | // the entry, otherwise separate clients could end up holding separate |
| 119 | // entries for a request to the same URL, which is undesirable. Therefore, |
| 120 | // if an entry has more than one reference (the map will always hold one), |
| 121 | // it should not be considered outdated. |
| 122 | // |
| 123 | // We considered whether to make ExtensionThrottleEntry objects |
| 124 | // non-refcounted, but since any means of knowing whether they are |
| 125 | // currently in use by others than the manager would be more or less |
| 126 | // equivalent to a refcount, we kept them refcounted. |
| 127 | if (!HasOneRef()) |
| 128 | return false; |
| 129 | |
| 130 | // If there are send events in the sliding window period, we still need this |
| 131 | // entry. |
| 132 | if (!send_log_.empty() && |
| 133 | send_log_.back() + sliding_window_period_ > ImplGetTimeNow()) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | return GetBackoffEntry()->CanDiscard(); |
| 138 | } |
| 139 | |
| 140 | void ExtensionThrottleEntry::DisableBackoffThrottling() { |
| 141 | is_backoff_disabled_ = true; |
| 142 | } |
| 143 | |
| 144 | void ExtensionThrottleEntry::DetachManager() { |
| 145 | manager_ = NULL; |
| 146 | } |
| 147 | |
| 148 | bool ExtensionThrottleEntry::ShouldRejectRequest( |
| 149 | const net::URLRequest& request) const { |
| 150 | bool reject_request = false; |
| 151 | if (!is_backoff_disabled_ && (ignore_user_gesture_load_flag_for_tests_ || |
| 152 | !ExplicitUserRequest(request.load_flags())) && |
| 153 | GetBackoffEntry()->ShouldRejectRequest()) { |
| 154 | net_log_.AddEvent(net::NetLog::TYPE_THROTTLING_REJECTED_REQUEST, |
| 155 | base::Bind(&NetLogRejectedRequestCallback, &url_id_, |
| 156 | GetBackoffEntry()->failure_count(), |
| 157 | GetBackoffEntry()->GetTimeUntilRelease())); |
| 158 | reject_request = true; |
| 159 | } |
| 160 | return reject_request; |
| 161 | } |
| 162 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 163 | int64_t ExtensionThrottleEntry::ReserveSendingTimeForNextRequest( |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 164 | const base::TimeTicks& earliest_time) { |
| 165 | base::TimeTicks now = ImplGetTimeNow(); |
| 166 | |
| 167 | // If a lot of requests were successfully made recently, |
| 168 | // sliding_window_release_time_ may be greater than |
| 169 | // exponential_backoff_release_time_. |
| 170 | base::TimeTicks recommended_sending_time = |
| 171 | std::max(std::max(now, earliest_time), |
| 172 | std::max(GetBackoffEntry()->GetReleaseTime(), |
| 173 | sliding_window_release_time_)); |
| 174 | |
| 175 | DCHECK(send_log_.empty() || recommended_sending_time >= send_log_.back()); |
| 176 | // Log the new send event. |
| 177 | send_log_.push(recommended_sending_time); |
| 178 | |
| 179 | sliding_window_release_time_ = recommended_sending_time; |
| 180 | |
| 181 | // Drop the out-of-date events in the event list. |
| 182 | // We don't need to worry that the queue may become empty during this |
| 183 | // operation, since the last element is sliding_window_release_time_. |
| 184 | while ((send_log_.front() + sliding_window_period_ <= |
| 185 | sliding_window_release_time_) || |
| 186 | send_log_.size() > static_cast<unsigned>(max_send_threshold_)) { |
| 187 | send_log_.pop(); |
| 188 | } |
| 189 | |
| 190 | // Check if there are too many send events in recent time. |
| 191 | if (send_log_.size() == static_cast<unsigned>(max_send_threshold_)) |
| 192 | sliding_window_release_time_ = send_log_.front() + sliding_window_period_; |
| 193 | |
| 194 | return (recommended_sending_time - now).InMillisecondsRoundedUp(); |
| 195 | } |
| 196 | |
| 197 | base::TimeTicks ExtensionThrottleEntry::GetExponentialBackoffReleaseTime() |
| 198 | const { |
| 199 | // If a site opts out, it's likely because they have problems that trigger |
| 200 | // the back-off mechanism when it shouldn't be triggered, in which case |
| 201 | // returning the calculated back-off release time would probably be the |
| 202 | // wrong thing to do (i.e. it would likely be too long). Therefore, we |
| 203 | // return "now" so that retries are not delayed. |
| 204 | if (is_backoff_disabled_) |
| 205 | return ImplGetTimeNow(); |
| 206 | |
| 207 | return GetBackoffEntry()->GetReleaseTime(); |
| 208 | } |
| 209 | |
| 210 | void ExtensionThrottleEntry::UpdateWithResponse(int status_code) { |
| 211 | GetBackoffEntry()->InformOfRequest(IsConsideredSuccess(status_code)); |
| 212 | } |
| 213 | |
| 214 | void ExtensionThrottleEntry::ReceivedContentWasMalformed(int response_code) { |
| 215 | // A malformed body can only occur when the request to fetch a resource |
| 216 | // was successful. Therefore, in such a situation, we will receive one |
| 217 | // call to ReceivedContentWasMalformed() and one call to |
| 218 | // UpdateWithResponse() with a response categorized as "good". To end |
| 219 | // up counting one failure, we need to count two failures here against |
| 220 | // the one success in UpdateWithResponse(). |
| 221 | // |
| 222 | // We do nothing for a response that is already being considered an error |
| 223 | // based on its status code (otherwise we would count 3 errors instead of 1). |
| 224 | if (IsConsideredSuccess(response_code)) { |
| 225 | GetBackoffEntry()->InformOfRequest(false); |
| 226 | GetBackoffEntry()->InformOfRequest(false); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | const std::string& ExtensionThrottleEntry::GetURLIdForDebugging() const { |
| 231 | return url_id_; |
| 232 | } |
| 233 | |
| 234 | ExtensionThrottleEntry::~ExtensionThrottleEntry() { |
| 235 | } |
| 236 | |
| 237 | void ExtensionThrottleEntry::Initialize() { |
| 238 | sliding_window_release_time_ = base::TimeTicks::Now(); |
| 239 | backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore; |
| 240 | backoff_policy_.initial_delay_ms = kDefaultInitialDelayMs; |
| 241 | backoff_policy_.multiply_factor = kDefaultMultiplyFactor; |
| 242 | backoff_policy_.jitter_factor = kDefaultJitterFactor; |
| 243 | backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs; |
| 244 | backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs; |
| 245 | backoff_policy_.always_use_initial_delay = false; |
| 246 | } |
| 247 | |
| 248 | bool ExtensionThrottleEntry::IsConsideredSuccess(int response_code) { |
| 249 | // We throttle only for the status codes most likely to indicate the server |
| 250 | // is failing because it is too busy or otherwise are likely to be |
| 251 | // because of DDoS. |
| 252 | // |
| 253 | // 500 is the generic error when no better message is suitable, and |
| 254 | // as such does not necessarily indicate a temporary state, but |
| 255 | // other status codes cover most of the permanent error states. |
| 256 | // 503 is explicitly documented as a temporary state where the server |
| 257 | // is either overloaded or down for maintenance. |
| 258 | // 509 is the (non-standard but widely implemented) Bandwidth Limit Exceeded |
| 259 | // status code, which might indicate DDoS. |
| 260 | // |
| 261 | // We do not back off on 502 or 504, which are reported by gateways |
| 262 | // (proxies) on timeouts or failures, because in many cases these requests |
| 263 | // have not made it to the destination server and so we do not actually |
| 264 | // know that it is down or busy. One degenerate case could be a proxy on |
| 265 | // localhost, where you are not actually connected to the network. |
| 266 | return !(response_code == 500 || response_code == 503 || |
| 267 | response_code == 509); |
| 268 | } |
| 269 | |
| 270 | base::TimeTicks ExtensionThrottleEntry::ImplGetTimeNow() const { |
| 271 | return base::TimeTicks::Now(); |
| 272 | } |
| 273 | |
| 274 | const net::BackoffEntry* ExtensionThrottleEntry::GetBackoffEntry() const { |
| 275 | return &backoff_entry_; |
| 276 | } |
| 277 | |
| 278 | net::BackoffEntry* ExtensionThrottleEntry::GetBackoffEntry() { |
| 279 | return &backoff_entry_; |
| 280 | } |
| 281 | |
| 282 | // static |
| 283 | bool ExtensionThrottleEntry::ExplicitUserRequest(const int load_flags) { |
| 284 | return (load_flags & net::LOAD_MAYBE_USER_GESTURE) != 0; |
| 285 | } |
| 286 | |
| 287 | } // namespace extensions |