url_request_throttler_entry.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
  5. #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/containers/queue.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. #include "net/base/backoff_entry.h"
  12. #include "net/base/net_export.h"
  13. #include "net/log/net_log_with_source.h"
  14. #include "net/url_request/url_request_throttler_entry_interface.h"
  15. namespace net {
  16. class URLRequestThrottlerManager;
  17. // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager.
  18. // It analyzes requests of a specific URL over some period of time, in order to
  19. // deduce the back-off time for every request.
  20. // The back-off algorithm consists of two parts. Firstly, exponential back-off
  21. // is used when receiving 5XX server errors or malformed response bodies.
  22. // The exponential back-off rule is enforced by URLRequestHttpJob. Any
  23. // request sent during the back-off period will be cancelled.
  24. // Secondly, a sliding window is used to count recent requests to a given
  25. // destination and provide guidance (to the application level only) on whether
  26. // too many requests have been sent and when a good time to send the next one
  27. // would be. This is never used to deny requests at the network level.
  28. class NET_EXPORT URLRequestThrottlerEntry
  29. : public URLRequestThrottlerEntryInterface {
  30. public:
  31. // Sliding window period.
  32. static const int kDefaultSlidingWindowPeriodMs;
  33. // Maximum number of requests allowed in sliding window period.
  34. static const int kDefaultMaxSendThreshold;
  35. // Number of initial errors to ignore before starting exponential back-off.
  36. static const int kDefaultNumErrorsToIgnore;
  37. // Initial delay for exponential back-off.
  38. static const int kDefaultInitialDelayMs;
  39. // Factor by which the waiting time will be multiplied.
  40. static const double kDefaultMultiplyFactor;
  41. // Fuzzing percentage. ex: 10% will spread requests randomly
  42. // between 90%-100% of the calculated time.
  43. static const double kDefaultJitterFactor;
  44. // Maximum amount of time we are willing to delay our request.
  45. static const int kDefaultMaximumBackoffMs;
  46. // Time after which the entry is considered outdated.
  47. static const int kDefaultEntryLifetimeMs;
  48. // The manager object's lifetime must enclose the lifetime of this object.
  49. URLRequestThrottlerEntry(URLRequestThrottlerManager* manager,
  50. const std::string& url_id);
  51. // The life span of instances created with this constructor is set to
  52. // infinite, and the number of initial errors to ignore is set to 0.
  53. // It is only used by unit tests.
  54. URLRequestThrottlerEntry(URLRequestThrottlerManager* manager,
  55. const std::string& url_id,
  56. int sliding_window_period_ms,
  57. int max_send_threshold,
  58. int initial_backoff_ms,
  59. double multiply_factor,
  60. double jitter_factor,
  61. int maximum_backoff_ms);
  62. URLRequestThrottlerEntry(const URLRequestThrottlerEntry&) = delete;
  63. URLRequestThrottlerEntry& operator=(const URLRequestThrottlerEntry&) = delete;
  64. // Used by the manager, returns true if the entry needs to be garbage
  65. // collected.
  66. bool IsEntryOutdated() const;
  67. // Causes this entry to never reject requests due to back-off.
  68. void DisableBackoffThrottling();
  69. // Causes this entry to NULL its manager pointer.
  70. void DetachManager();
  71. // Implementation of URLRequestThrottlerEntryInterface.
  72. bool ShouldRejectRequest(const URLRequest& request) const override;
  73. int64_t ReserveSendingTimeForNextRequest(
  74. const base::TimeTicks& earliest_time) override;
  75. base::TimeTicks GetExponentialBackoffReleaseTime() const override;
  76. void UpdateWithResponse(int status_code) override;
  77. void ReceivedContentWasMalformed(int response_code) override;
  78. protected:
  79. ~URLRequestThrottlerEntry() override;
  80. void Initialize();
  81. // Returns true if the given response code is considered a success for
  82. // throttling purposes.
  83. bool IsConsideredSuccess(int response_code);
  84. // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
  85. virtual base::TimeTicks ImplGetTimeNow() const;
  86. // Retrieves the back-off entry object we're using. Used to enable a
  87. // unit testing seam for dependency injection in tests.
  88. virtual const BackoffEntry* GetBackoffEntry() const;
  89. virtual BackoffEntry* GetBackoffEntry();
  90. // Used by tests.
  91. base::TimeTicks sliding_window_release_time() const {
  92. return sliding_window_release_time_;
  93. }
  94. // Used by tests.
  95. void set_sliding_window_release_time(const base::TimeTicks& release_time) {
  96. sliding_window_release_time_ = release_time;
  97. }
  98. // Valid and immutable after construction time.
  99. BackoffEntry::Policy backoff_policy_;
  100. private:
  101. // Timestamp calculated by the sliding window algorithm for when we advise
  102. // clients the next request should be made, at the earliest. Advisory only,
  103. // not used to deny requests.
  104. base::TimeTicks sliding_window_release_time_;
  105. // A list of the recent send events. We use them to decide whether there are
  106. // too many requests sent in sliding window.
  107. base::queue<base::TimeTicks> send_log_;
  108. const base::TimeDelta sliding_window_period_;
  109. const int max_send_threshold_;
  110. // True if DisableBackoffThrottling() has been called on this object.
  111. bool is_backoff_disabled_ = false;
  112. // Access it through GetBackoffEntry() to allow a unit test seam.
  113. BackoffEntry backoff_entry_;
  114. // Weak back-reference to the manager object managing us.
  115. raw_ptr<URLRequestThrottlerManager> manager_;
  116. // Canonicalized URL string that this entry is for; used for logging only.
  117. std::string url_id_;
  118. NetLogWithSource net_log_;
  119. };
  120. } // namespace net
  121. #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_