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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 bool URLRequestThrottlerEntry::IsEntryOutdated() const { | 131 bool URLRequestThrottlerEntry::IsEntryOutdated() const { |
132 // This function is called by the URLRequestThrottlerManager to determine | 132 // This function is called by the URLRequestThrottlerManager to determine |
133 // whether entries should be discarded from its url_entries_ map. We | 133 // whether entries should be discarded from its url_entries_ map. We |
134 // want to ensure that it does not remove entries from the map while there | 134 // want to ensure that it does not remove entries from the map while there |
135 // are clients (objects other than the manager) holding references to | 135 // are clients (objects other than the manager) holding references to |
136 // the entry, otherwise separate clients could end up holding separate | 136 // the entry, otherwise separate clients could end up holding separate |
137 // entries for a request to the same URL, which is undesirable. Therefore, | 137 // entries for a request to the same URL, which is undesirable. Therefore, |
138 // if an entry has more than one reference (the map will always hold one), | 138 // if an entry has more than one reference (the map will always hold one), |
139 // it should not be considered outdated. | 139 // it should not be considered outdated. |
140 // | 140 // |
141 // TODO(joi): Once the manager is not a Singleton, revisit whether | 141 // We considered whether to make URLRequestThrottlerEntry objects |
142 // refcounting is needed at all. | 142 // non-refcounted, but since any means of knowing whether they are |
| 143 // currently in use by others than the manager would be more or less |
| 144 // equivalent to a refcount, we kept them refcounted. |
143 if (!HasOneRef()) | 145 if (!HasOneRef()) |
144 return false; | 146 return false; |
145 | 147 |
146 // If there are send events in the sliding window period, we still need this | 148 // If there are send events in the sliding window period, we still need this |
147 // entry. | 149 // entry. |
148 if (!send_log_.empty() && | 150 if (!send_log_.empty() && |
149 send_log_.back() + sliding_window_period_ > ImplGetTimeNow()) { | 151 send_log_.back() + sliding_window_period_ > ImplGetTimeNow()) { |
150 return false; | 152 return false; |
151 } | 153 } |
152 | 154 |
153 return GetBackoffEntry()->CanDiscard(); | 155 return GetBackoffEntry()->CanDiscard(); |
154 } | 156 } |
155 | 157 |
156 void URLRequestThrottlerEntry::DisableBackoffThrottling() { | 158 void URLRequestThrottlerEntry::DisableBackoffThrottling() { |
157 is_backoff_disabled_ = true; | 159 is_backoff_disabled_ = true; |
158 } | 160 } |
159 | 161 |
160 void URLRequestThrottlerEntry::DetachManager() { | 162 void URLRequestThrottlerEntry::DetachManager() { |
161 manager_ = NULL; | 163 manager_ = NULL; |
162 } | 164 } |
163 | 165 |
164 bool URLRequestThrottlerEntry::ShouldRejectRequest(int load_flags) const { | 166 bool URLRequestThrottlerEntry::ShouldRejectRequest(URLRequest* request, |
| 167 int load_flags) const { |
165 bool reject_request = false; | 168 bool reject_request = false; |
166 if (!is_backoff_disabled_ && !ExplicitUserRequest(load_flags) && | 169 if (manager_ && !is_backoff_disabled_ && !ExplicitUserRequest(load_flags) && |
| 170 manager_->delegate_->MayRejectRequest(request) && |
167 GetBackoffEntry()->ShouldRejectRequest()) { | 171 GetBackoffEntry()->ShouldRejectRequest()) { |
168 int num_failures = GetBackoffEntry()->failure_count(); | 172 int num_failures = GetBackoffEntry()->failure_count(); |
169 int release_after_ms = | 173 int release_after_ms = |
170 GetBackoffEntry()->GetTimeUntilRelease().InMilliseconds(); | 174 GetBackoffEntry()->GetTimeUntilRelease().InMilliseconds(); |
171 | 175 |
172 net_log_.AddEvent( | 176 net_log_.AddEvent( |
173 NetLog::TYPE_THROTTLING_REJECTED_REQUEST, | 177 NetLog::TYPE_THROTTLING_REJECTED_REQUEST, |
174 make_scoped_refptr( | 178 make_scoped_refptr( |
175 new RejectedRequestParameters(url_id_, | 179 new RejectedRequestParameters(url_id_, |
176 num_failures, | 180 num_failures, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // return "now" so that retries are not delayed. | 234 // return "now" so that retries are not delayed. |
231 if (is_backoff_disabled_) | 235 if (is_backoff_disabled_) |
232 return ImplGetTimeNow(); | 236 return ImplGetTimeNow(); |
233 | 237 |
234 return GetBackoffEntry()->GetReleaseTime(); | 238 return GetBackoffEntry()->GetReleaseTime(); |
235 } | 239 } |
236 | 240 |
237 void URLRequestThrottlerEntry::UpdateWithResponse( | 241 void URLRequestThrottlerEntry::UpdateWithResponse( |
238 const std::string& host, | 242 const std::string& host, |
239 const URLRequestThrottlerHeaderInterface* response) { | 243 const URLRequestThrottlerHeaderInterface* response) { |
240 int response_code = response->GetResponseCode(); | 244 if (IsConsideredError(response->GetResponseCode())) { |
241 HandleMetricsTracking(response_code); | |
242 | |
243 if (IsConsideredError(response_code)) { | |
244 GetBackoffEntry()->InformOfRequest(false); | 245 GetBackoffEntry()->InformOfRequest(false); |
245 } else { | 246 } else { |
246 GetBackoffEntry()->InformOfRequest(true); | 247 GetBackoffEntry()->InformOfRequest(true); |
247 | 248 |
248 std::string throttling_header = response->GetNormalizedValue( | 249 std::string throttling_header = response->GetNormalizedValue( |
249 kExponentialThrottlingHeader); | 250 kExponentialThrottlingHeader); |
250 if (!throttling_header.empty()) | 251 if (!throttling_header.empty()) |
251 HandleThrottlingHeader(throttling_header, host); | 252 HandleThrottlingHeader(throttling_header, host); |
252 } | 253 } |
253 } | 254 } |
(...skipping 19 matching lines...) Expand all Loading... |
273 | 274 |
274 void URLRequestThrottlerEntry::Initialize() { | 275 void URLRequestThrottlerEntry::Initialize() { |
275 sliding_window_release_time_ = base::TimeTicks::Now(); | 276 sliding_window_release_time_ = base::TimeTicks::Now(); |
276 backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore; | 277 backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore; |
277 backoff_policy_.initial_delay_ms = kDefaultInitialDelayMs; | 278 backoff_policy_.initial_delay_ms = kDefaultInitialDelayMs; |
278 backoff_policy_.multiply_factor = kDefaultMultiplyFactor; | 279 backoff_policy_.multiply_factor = kDefaultMultiplyFactor; |
279 backoff_policy_.jitter_factor = kDefaultJitterFactor; | 280 backoff_policy_.jitter_factor = kDefaultJitterFactor; |
280 backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs; | 281 backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs; |
281 backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs; | 282 backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs; |
282 backoff_policy_.always_use_initial_delay = false; | 283 backoff_policy_.always_use_initial_delay = false; |
283 | |
284 // We pretend we just had a successful response so that we have a | |
285 // starting point to our tracking. This is called from the | |
286 // constructor so we do not use the virtual ImplGetTimeNow(). | |
287 last_successful_response_time_ = base::TimeTicks::Now(); | |
288 last_response_was_success_ = true; | |
289 } | 284 } |
290 | 285 |
291 bool URLRequestThrottlerEntry::IsConsideredError(int response_code) { | 286 bool URLRequestThrottlerEntry::IsConsideredError(int response_code) { |
292 // We throttle only for the status codes most likely to indicate the server | 287 // We throttle only for the status codes most likely to indicate the server |
293 // is failing because it is too busy or otherwise are likely to be | 288 // is failing because it is too busy or otherwise are likely to be |
294 // because of DDoS. | 289 // because of DDoS. |
295 // | 290 // |
296 // 500 is the generic error when no better message is suitable, and | 291 // 500 is the generic error when no better message is suitable, and |
297 // as such does not necessarily indicate a temporary state, but | 292 // as such does not necessarily indicate a temporary state, but |
298 // other status codes cover most of the permanent error states. | 293 // other status codes cover most of the permanent error states. |
(...skipping 16 matching lines...) Expand all Loading... |
315 return base::TimeTicks::Now(); | 310 return base::TimeTicks::Now(); |
316 } | 311 } |
317 | 312 |
318 void URLRequestThrottlerEntry::HandleThrottlingHeader( | 313 void URLRequestThrottlerEntry::HandleThrottlingHeader( |
319 const std::string& header_value, | 314 const std::string& header_value, |
320 const std::string& host) { | 315 const std::string& host) { |
321 if (header_value == kExponentialThrottlingDisableValue) { | 316 if (header_value == kExponentialThrottlingDisableValue) { |
322 DisableBackoffThrottling(); | 317 DisableBackoffThrottling(); |
323 if (manager_) | 318 if (manager_) |
324 manager_->AddToOptOutList(host); | 319 manager_->AddToOptOutList(host); |
325 } else { | |
326 // TODO(joi): Log this. | |
327 } | 320 } |
328 } | 321 } |
329 | 322 |
330 void URLRequestThrottlerEntry::HandleMetricsTracking(int response_code) { | |
331 // Note that we are not interested in whether the code is considered | |
332 // an error for the backoff logic, but whether it is a 5xx error in | |
333 // general. This is because here, we are tracking the apparent total | |
334 // downtime of a server. | |
335 if (response_code >= 500) { | |
336 last_response_was_success_ = false; | |
337 } else { | |
338 base::TimeTicks now = ImplGetTimeNow(); | |
339 if (!last_response_was_success_) { | |
340 // We are transitioning from failure to success, so generate our stats. | |
341 base::TimeDelta down_time = now - last_successful_response_time_; | |
342 int failure_count = GetBackoffEntry()->failure_count(); | |
343 | |
344 UMA_HISTOGRAM_COUNTS("Throttling.FailureCountAtSuccess", failure_count); | |
345 UMA_HISTOGRAM_CUSTOM_TIMES( | |
346 "Throttling.PerceivedDowntime", down_time, | |
347 base::TimeDelta::FromMilliseconds(10), | |
348 base::TimeDelta::FromHours(6), 50); | |
349 } | |
350 | |
351 last_successful_response_time_ = now; | |
352 last_response_was_success_ = true; | |
353 } | |
354 } | |
355 | |
356 const BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() const { | 323 const BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() const { |
357 return &backoff_entry_; | 324 return &backoff_entry_; |
358 } | 325 } |
359 | 326 |
360 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { | 327 BackoffEntry* URLRequestThrottlerEntry::GetBackoffEntry() { |
361 return &backoff_entry_; | 328 return &backoff_entry_; |
362 } | 329 } |
363 | 330 |
364 // static | 331 // static |
365 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { | 332 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags) { |
366 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; | 333 return (load_flags & LOAD_MAYBE_USER_GESTURE) != 0; |
367 } | 334 } |
368 | 335 |
369 } // namespace net | 336 } // namespace net |
OLD | NEW |