extension_throttle_entry.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 EXTENSIONS_RENDERER_EXTENSION_THROTTLE_ENTRY_H_
  5. #define EXTENSIONS_RENDERER_EXTENSION_THROTTLE_ENTRY_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/containers/queue.h"
  9. #include "base/time/time.h"
  10. #include "net/base/backoff_entry.h"
  11. namespace extensions {
  12. // ExtensionThrottleEntry represents an entry of ExtensionThrottleManager.
  13. // It analyzes requests of a specific URL over some period of time, in order to
  14. // deduce the back-off time for every request.
  15. // The back-off algorithm consists of two parts. Firstly, exponential back-off
  16. // is used when receiving 5XX server errors or malformed response bodies.
  17. // The exponential back-off rule is enforced by URLRequestHttpJob. Any
  18. // request sent during the back-off period will be cancelled.
  19. // Secondly, a sliding window is used to count recent requests to a given
  20. // destination and provide guidance (to the application level only) on whether
  21. // too many requests have been sent and when a good time to send the next one
  22. // would be. This is never used to deny requests at the network level.
  23. class ExtensionThrottleEntry {
  24. public:
  25. // Sliding window period.
  26. static const int kDefaultSlidingWindowPeriodMs;
  27. // Maximum number of requests allowed in sliding window period.
  28. static const int kDefaultMaxSendThreshold;
  29. // Number of initial errors to ignore before starting exponential back-off.
  30. static const int kDefaultNumErrorsToIgnore;
  31. // Initial delay for exponential back-off.
  32. static const int kDefaultInitialDelayMs;
  33. // Factor by which the waiting time will be multiplied.
  34. static const double kDefaultMultiplyFactor;
  35. // Fuzzing percentage. ex: 10% will spread requests randomly
  36. // between 90%-100% of the calculated time.
  37. static const double kDefaultJitterFactor;
  38. // Maximum amount of time we are willing to delay our request.
  39. static const int kDefaultMaximumBackoffMs;
  40. // Time after which the entry is considered outdated.
  41. static const int kDefaultEntryLifetimeMs;
  42. // |url_id| is a unique entry ID.
  43. explicit ExtensionThrottleEntry(const std::string& url_id);
  44. // The life span of instances created with this constructor is set to
  45. // infinite, and the number of initial errors to ignore is set to 0.
  46. // It is only used by unit tests.
  47. ExtensionThrottleEntry(const std::string& url_id,
  48. const net::BackoffEntry::Policy* backoff_policy);
  49. ExtensionThrottleEntry(const ExtensionThrottleEntry&) = delete;
  50. ExtensionThrottleEntry& operator=(const ExtensionThrottleEntry&) = delete;
  51. virtual ~ExtensionThrottleEntry();
  52. // Used by the manager, returns true if the entry needs to be garbage
  53. // collected.
  54. bool IsEntryOutdated() const;
  55. // Causes this entry to never reject requests due to back-off.
  56. void DisableBackoffThrottling();
  57. // Returns true when we have encountered server errors and are doing
  58. // exponential back-off.
  59. bool ShouldRejectRequest() const;
  60. // Calculates a recommended sending time for the next request and reserves it.
  61. // The sending time is not earlier than the current exponential back-off
  62. // release time or |earliest_time|. Moreover, the previous results of
  63. // the method are taken into account, in order to make sure they are spread
  64. // properly over time.
  65. // Returns the recommended delay before sending the next request, in
  66. // milliseconds. The return value is always positive or 0.
  67. // Although it is not mandatory, respecting the value returned by this method
  68. // is helpful to avoid traffic overload.
  69. int64_t ReserveSendingTimeForNextRequest(
  70. const base::TimeTicks& earliest_time);
  71. // Returns the time after which requests are allowed.
  72. base::TimeTicks GetExponentialBackoffReleaseTime() const;
  73. // This method needs to be called each time a response is received.
  74. void UpdateWithResponse(int status_code);
  75. // Lets higher-level modules, that know how to parse particular response
  76. // bodies, notify of receiving malformed content for the given URL. This will
  77. // be handled by the throttler as if an HTTP 503 response had been received to
  78. // the request, i.e. it will count as a failure, unless the HTTP response code
  79. // indicated is already one of those that will be counted as an error.
  80. void ReceivedContentWasMalformed(int response_code);
  81. // Get the URL ID associated with this entry. Should only be used for
  82. // debugging purpose.
  83. const std::string& GetURLIdForDebugging() const;
  84. protected:
  85. void Initialize();
  86. // Returns true if the given response code is considered a success for
  87. // throttling purposes.
  88. bool IsConsideredSuccess(int response_code);
  89. // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
  90. virtual base::TimeTicks ImplGetTimeNow() const;
  91. // Retrieves the back-off entry object we're using. Used to enable a
  92. // unit testing seam for dependency injection in tests.
  93. virtual const net::BackoffEntry* GetBackoffEntry() const;
  94. virtual net::BackoffEntry* GetBackoffEntry();
  95. // Used by tests.
  96. base::TimeTicks sliding_window_release_time() const {
  97. return sliding_window_release_time_;
  98. }
  99. // Used by tests.
  100. void set_sliding_window_release_time(const base::TimeTicks& release_time) {
  101. sliding_window_release_time_ = release_time;
  102. }
  103. // Valid and immutable after construction time.
  104. net::BackoffEntry::Policy backoff_policy_;
  105. private:
  106. // Timestamp calculated by the sliding window algorithm for when we advise
  107. // clients the next request should be made, at the earliest. Advisory only,
  108. // not used to deny requests.
  109. base::TimeTicks sliding_window_release_time_;
  110. // A list of the recent send events. We use them to decide whether there are
  111. // too many requests sent in sliding window.
  112. base::queue<base::TimeTicks> send_log_;
  113. const base::TimeDelta sliding_window_period_;
  114. const int max_send_threshold_;
  115. // True if DisableBackoffThrottling() has been called on this object.
  116. bool is_backoff_disabled_;
  117. // Access it through GetBackoffEntry() to allow a unit test seam.
  118. net::BackoffEntry backoff_entry_;
  119. // Canonicalized URL string that this entry is for; used for logging only.
  120. const std::string url_id_;
  121. };
  122. } // namespace extensions
  123. #endif // EXTENSIONS_RENDERER_EXTENSION_THROTTLE_ENTRY_H_