url_request_throttler_entry.cc 11 KB

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