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.
5 #include "net/url_request/url_request_throttler_entry.h"
9 #include "base/logging.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/values.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 #include "net/url_request/url_request_throttler_header_interface.h"
20 #include "net/url_request/url_request_throttler_manager.h"
24 const int URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs
= 2000;
25 const int URLRequestThrottlerEntry::kDefaultMaxSendThreshold
= 20;
27 // This set of back-off parameters will (at maximum values, i.e. without
28 // the reduction caused by jitter) add 0-41% (distributed uniformly
29 // 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
31 // about a second at a time. Once the maximum back-off is reached, the added
32 // perceived downtime decreases rapidly, percentage-wise.
34 // 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
36 // 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
40 // 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.
42 // on flaky connections.
43 const int URLRequestThrottlerEntry::kDefaultNumErrorsToIgnore
= 2;
44 const int URLRequestThrottlerEntry::kDefaultInitialDelayMs
= 700;
45 const double URLRequestThrottlerEntry::kDefaultMultiplyFactor
= 1.4;
46 const double URLRequestThrottlerEntry::kDefaultJitterFactor
= 0.4;
47 const int URLRequestThrottlerEntry::kDefaultMaximumBackoffMs
= 15 * 60 * 1000;
48 const int URLRequestThrottlerEntry::kDefaultEntryLifetimeMs
= 2 * 60 * 1000;
49 const char URLRequestThrottlerEntry::kExponentialThrottlingHeader
[] =
50 "X-Chrome-Exponential-Throttling";
51 const char URLRequestThrottlerEntry::kExponentialThrottlingDisableValue
[] =
54 // Returns NetLog parameters when a request is rejected by throttling.
55 base::Value
* NetLogRejectedRequestCallback(
56 const std::string
* url_id
,
58 const base::TimeDelta
& release_after
,
59 NetLogCaptureMode
/* capture_mode */) {
60 base::DictionaryValue
* dict
= new base::DictionaryValue();
61 dict
->SetString("url", *url_id
);
62 dict
->SetInteger("num_failures", num_failures
);
63 dict
->SetInteger("release_after_ms",
64 static_cast<int>(release_after
.InMilliseconds()));
68 URLRequestThrottlerEntry::URLRequestThrottlerEntry(
69 URLRequestThrottlerManager
* manager
,
70 const std::string
& url_id
)
71 : sliding_window_period_(
72 base::TimeDelta::FromMilliseconds(kDefaultSlidingWindowPeriodMs
)),
73 max_send_threshold_(kDefaultMaxSendThreshold
),
74 is_backoff_disabled_(false),
75 backoff_entry_(&backoff_policy_
),
78 net_log_(BoundNetLog::Make(
79 manager
->net_log(), NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING
)) {
84 URLRequestThrottlerEntry::URLRequestThrottlerEntry(
85 URLRequestThrottlerManager
* manager
,
86 const std::string
& url_id
,
87 int sliding_window_period_ms
,
88 int max_send_threshold
,
89 int initial_backoff_ms
,
90 double multiply_factor
,
92 int maximum_backoff_ms
)
93 : sliding_window_period_(
94 base::TimeDelta::FromMilliseconds(sliding_window_period_ms
)),
95 max_send_threshold_(max_send_threshold
),
96 is_backoff_disabled_(false),
97 backoff_entry_(&backoff_policy_
),
100 DCHECK_GT(sliding_window_period_ms
, 0);
101 DCHECK_GT(max_send_threshold_
, 0);
102 DCHECK_GE(initial_backoff_ms
, 0);
103 DCHECK_GT(multiply_factor
, 0);
104 DCHECK_GE(jitter_factor
, 0.0);
105 DCHECK_LT(jitter_factor
, 1.0);
106 DCHECK_GE(maximum_backoff_ms
, 0);
110 backoff_policy_
.initial_delay_ms
= initial_backoff_ms
;
111 backoff_policy_
.multiply_factor
= multiply_factor
;
112 backoff_policy_
.jitter_factor
= jitter_factor
;
113 backoff_policy_
.maximum_backoff_ms
= maximum_backoff_ms
;
114 backoff_policy_
.entry_lifetime_ms
= -1;
115 backoff_policy_
.num_errors_to_ignore
= 0;
116 backoff_policy_
.always_use_initial_delay
= false;
119 bool URLRequestThrottlerEntry::IsEntryOutdated() const {
120 // This function is called by the URLRequestThrottlerManager to determine
121 // whether entries should be discarded from its url_entries_ map. We
122 // want to ensure that it does not remove entries from the map while there
123 // are clients (objects other than the manager) holding references to
124 // the entry, otherwise separate clients could end up holding separate
125 // entries for a request to the same URL, which is undesirable. Therefore,
126 // if an entry has more than one reference (the map will always hold one),
127 // it should not be considered outdated.
129 // We considered whether to make URLRequestThrottlerEntry objects
130 // non-refcounted, but since any means of knowing whether they are
131 // currently in use by others than the manager would be more or less
132 // equivalent to a refcount, we kept them refcounted.
136 // If there are send events in the sliding window period, we still need this
138 if (!send_log_
.empty() &&
139 send_log_
.back() + sliding_window_period_
> ImplGetTimeNow()) {
143 return GetBackoffEntry()->CanDiscard();
146 void URLRequestThrottlerEntry::DisableBackoffThrottling() {
147 is_backoff_disabled_
= true;
150 void URLRequestThrottlerEntry::DetachManager() {
154 bool URLRequestThrottlerEntry::ShouldRejectRequest(
155 const URLRequest
& request
,
156 NetworkDelegate
* network_delegate
) const {
157 bool reject_request
= false;
158 if (!is_backoff_disabled_
&& !ExplicitUserRequest(request
.load_flags()) &&
159 (!network_delegate
|| network_delegate
->CanThrottleRequest(request
)) &&
160 GetBackoffEntry()->ShouldRejectRequest()) {
162 NetLog::TYPE_THROTTLING_REJECTED_REQUEST
,
163 base::Bind(&NetLogRejectedRequestCallback
,
165 GetBackoffEntry()->failure_count(),
166 GetBackoffEntry()->GetTimeUntilRelease()));
167 reject_request
= true;
170 int reject_count
= reject_request
? 1 : 0;
171 UMA_HISTOGRAM_ENUMERATION(
172 "Throttling.RequestThrottled", reject_count
, 2);
174 return reject_request
;
177 int64
URLRequestThrottlerEntry::ReserveSendingTimeForNextRequest(
178 const base::TimeTicks
& earliest_time
) {
179 base::TimeTicks now
= ImplGetTimeNow();
181 // If a lot of requests were successfully made recently,
182 // sliding_window_release_time_ may be greater than
183 // exponential_backoff_release_time_.
184 base::TimeTicks recommended_sending_time
=
185 std::max(std::max(now
, earliest_time
),
186 std::max(GetBackoffEntry()->GetReleaseTime(),
187 sliding_window_release_time_
));
189 DCHECK(send_log_
.empty() ||
190 recommended_sending_time
>= send_log_
.back());
191 // Log the new send event.
192 send_log_
.push(recommended_sending_time
);
194 sliding_window_release_time_
= recommended_sending_time
;
196 // Drop the out-of-date events in the event list.
197 // We don't need to worry that the queue may become empty during this
198 // operation, since the last element is sliding_window_release_time_.
199 while ((send_log_
.front() + sliding_window_period_
<=
200 sliding_window_release_time_
) ||
201 send_log_
.size() > static_cast<unsigned>(max_send_threshold_
)) {
205 // Check if there are too many send events in recent time.
206 if (send_log_
.size() == static_cast<unsigned>(max_send_threshold_
))
207 sliding_window_release_time_
= send_log_
.front() + sliding_window_period_
;
209 return (recommended_sending_time
- now
).InMillisecondsRoundedUp();
213 URLRequestThrottlerEntry::GetExponentialBackoffReleaseTime() const {
214 // If a site opts out, it's likely because they have problems that trigger
215 // 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
217 // wrong thing to do (i.e. it would likely be too long). Therefore, we
218 // return "now" so that retries are not delayed.
219 if (is_backoff_disabled_
)
220 return ImplGetTimeNow();
222 return GetBackoffEntry()->GetReleaseTime();
225 void URLRequestThrottlerEntry::UpdateWithResponse(
226 const std::string
& host
,
227 const URLRequestThrottlerHeaderInterface
* response
) {
228 if (IsConsideredError(response
->GetResponseCode())) {
229 GetBackoffEntry()->InformOfRequest(false);
231 GetBackoffEntry()->InformOfRequest(true);
233 std::string throttling_header
= response
->GetNormalizedValue(
234 kExponentialThrottlingHeader
);
235 if (!throttling_header
.empty())
236 HandleThrottlingHeader(throttling_header
, host
);
240 void URLRequestThrottlerEntry::ReceivedContentWasMalformed(int response_code
) {
241 // 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
243 // call to ReceivedContentWasMalformed() and one call to
244 // UpdateWithResponse() with a response categorized as "good". To end
245 // up counting one failure, we need to count two failures here against
246 // the one success in UpdateWithResponse().
248 // We do nothing for a response that is already being considered an error
249 // based on its status code (otherwise we would count 3 errors instead of 1).
250 if (!IsConsideredError(response_code
)) {
251 GetBackoffEntry()->InformOfRequest(false);
252 GetBackoffEntry()->InformOfRequest(false);
256 URLRequestThrottlerEntry::~URLRequestThrottlerEntry() {
259 void URLRequestThrottlerEntry::Initialize() {
260 sliding_window_release_time_
= base::TimeTicks::Now();
261 backoff_policy_
.num_errors_to_ignore
= kDefaultNumErrorsToIgnore
;
262 backoff_policy_
.initial_delay_ms
= kDefaultInitialDelayMs
;
263 backoff_policy_
.multiply_factor
= kDefaultMultiplyFactor
;
264 backoff_policy_
.jitter_factor
= kDefaultJitterFactor
;
265 backoff_policy_
.maximum_backoff_ms
= kDefaultMaximumBackoffMs
;
266 backoff_policy_
.entry_lifetime_ms
= kDefaultEntryLifetimeMs
;
267 backoff_policy_
.always_use_initial_delay
= false;
270 bool URLRequestThrottlerEntry::IsConsideredError(int response_code
) {
271 // We throttle only for the status codes most likely to indicate the server
272 // is failing because it is too busy or otherwise are likely to be
275 // 500 is the generic error when no better message is suitable, and
276 // as such does not necessarily indicate a temporary state, but
277 // other status codes cover most of the permanent error states.
278 // 503 is explicitly documented as a temporary state where the server
279 // is either overloaded or down for maintenance.
280 // 509 is the (non-standard but widely implemented) Bandwidth Limit Exceeded
281 // status code, which might indicate DDoS.
283 // We do not back off on 502 or 504, which are reported by gateways
284 // (proxies) on timeouts or failures, because in many cases these requests
285 // have not made it to the destination server and so we do not actually
286 // know that it is down or busy. One degenerate case could be a proxy on
287 // localhost, where you are not actually connected to the network.
288 return (response_code
== 500 ||
289 response_code
== 503 ||
290 response_code
== 509);
293 base::TimeTicks
URLRequestThrottlerEntry::ImplGetTimeNow() const {
294 return base::TimeTicks::Now();
297 void URLRequestThrottlerEntry::HandleThrottlingHeader(
298 const std::string
& header_value
,
299 const std::string
& host
) {
300 if (header_value
== kExponentialThrottlingDisableValue
) {
301 DisableBackoffThrottling();
303 manager_
->AddToOptOutList(host
);
307 const BackoffEntry
* URLRequestThrottlerEntry::GetBackoffEntry() const {
308 return &backoff_entry_
;
311 BackoffEntry
* URLRequestThrottlerEntry::GetBackoffEntry() {
312 return &backoff_entry_
;
316 bool URLRequestThrottlerEntry::ExplicitUserRequest(const int load_flags
) {
317 return (load_flags
& LOAD_MAYBE_USER_GESTURE
) != 0;