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