| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 |
| 5 #ifndef NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_ |
| 6 #define NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_ |
| 7 |
| 8 #include "net/url_request/request_throttler_entry_interface.h" |
| 9 |
| 10 #include <queue> |
| 11 #include <string> |
| 12 |
| 13 #include "base/lock.h" |
| 14 |
| 15 // RequestThrottlerEntry represents an entry of RequestThrottlerManager. |
| 16 // It analyzes requests of a specific URL over some period of time, in order to |
| 17 // deduce the back-off time for every request. |
| 18 // The back-off algorithm consists of two parts. Firstly, exponential back-off |
| 19 // is used when receiving 5XX server errors or malformed response bodies. |
| 20 // The exponential back-off rule is enforced by URLRequestHttpJob. Any request |
| 21 // sent during the back-off period will be cancelled. Secondly, a sliding window |
| 22 // is used to avoid too many send events in a short period of time. |
| 23 class RequestThrottlerEntry : public RequestThrottlerEntryInterface { |
| 24 public: |
| 25 // Sliding window period. |
| 26 static const int kDefaultSlidingWindowPeriodMs; |
| 27 |
| 28 // Maximum number of requests allowed in sliding window period. |
| 29 static const int kDefaultMaxSendThreshold; |
| 30 |
| 31 // Initial delay for exponential back-off. |
| 32 static const int kDefaultInitialBackoffMs; |
| 33 |
| 34 // Additional constant to adjust back-off. |
| 35 static const int kDefaultAdditionalConstantMs; |
| 36 |
| 37 // Factor by which the waiting time will be multiplied. |
| 38 static const double kDefaultMultiplyFactor; |
| 39 |
| 40 // Fuzzing percentage. ex: 10% will spread requests randomly |
| 41 // between 90%-100% of the calculated time. |
| 42 static const double kDefaultJitterFactor; |
| 43 |
| 44 // Maximum amount of time we are willing to delay our request. |
| 45 static const int kDefaultMaximumBackoffMs; |
| 46 |
| 47 // Time after which the entry is considered outdated. |
| 48 static const int kDefaultEntryLifetimeMs; |
| 49 |
| 50 // Name of the header that servers can use to ask clients to delay their next |
| 51 // request. ex: "X-Retry-After" |
| 52 static const char kRetryHeaderName[]; |
| 53 |
| 54 RequestThrottlerEntry(); |
| 55 |
| 56 // The life span of instances created with this constructor is set to |
| 57 // infinite. |
| 58 RequestThrottlerEntry(int sliding_window_period_ms, |
| 59 int max_send_threshold, |
| 60 int initial_backoff_ms, |
| 61 int additional_constant_ms, |
| 62 double multiply_factor, |
| 63 double jitter_factor, |
| 64 int maximum_backoff_ms); |
| 65 |
| 66 // Implementation of RequestThrottlerEntryInterface. |
| 67 virtual bool IsDuringExponentialBackoff() const; |
| 68 virtual int64 GetRecommendedDelayForNextRequest(); |
| 69 virtual void UpdateWithResponse( |
| 70 const RequestThrottlerHeaderInterface* response); |
| 71 |
| 72 // Used by the manager, returns true if the entry needs to be garbage |
| 73 // collected. |
| 74 bool IsEntryOutdated() const; |
| 75 |
| 76 // Used by the manager, enables the manager to flag the last successful |
| 77 // request as a failure. |
| 78 void ReceivedContentWasMalformed(); |
| 79 |
| 80 protected: |
| 81 // This struct is used to save the state of the entry each time we are updated |
| 82 // with a response header so we can regenerate it if we are informed that one |
| 83 // of our bodies was malformed. |
| 84 struct OldValues { |
| 85 base::TimeTicks exponential_backoff_release_time; |
| 86 int failure_count; |
| 87 }; |
| 88 |
| 89 virtual ~RequestThrottlerEntry(); |
| 90 |
| 91 void Initialize(); |
| 92 |
| 93 // Calculates the release time for exponential back-off. |
| 94 base::TimeTicks CalculateExponentialBackoffReleaseTime(); |
| 95 |
| 96 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. |
| 97 virtual base::TimeTicks GetTimeNow() const; |
| 98 |
| 99 // Used internally to increase release time following a retry-after header. |
| 100 void HandleCustomRetryAfter(const std::string& header_value); |
| 101 |
| 102 // Saves the state of the object to be able to regenerate it. |
| 103 // Must be informed of the state of the response. |
| 104 void SaveState(); |
| 105 |
| 106 // Timestamp calculated by the exponential back-off algorithm at which we are |
| 107 // allowed to start sending requests again. |
| 108 base::TimeTicks exponential_backoff_release_time_; |
| 109 |
| 110 // Number of times we encounter server errors or malformed response bodies. |
| 111 int failure_count_; |
| 112 |
| 113 // Set to true if we are currently tracking requests with server errors or |
| 114 // malformed response bodies, in order to calculate the exponential back-off |
| 115 // release time. |
| 116 bool tracking_exponential_backoff_; |
| 117 |
| 118 OldValues old_values_; |
| 119 |
| 120 // Timestamp calculated by the sliding window algorithm at which we are |
| 121 // allowed to start sending requests again. |
| 122 base::TimeTicks sliding_window_release_time_; |
| 123 |
| 124 // A list of the recent send events. We use them to decide whether there are |
| 125 // too many requests sent in sliding window. |
| 126 std::queue<base::TimeTicks> send_log_; |
| 127 |
| 128 const int sliding_window_period_ms_; |
| 129 const int max_send_threshold_; |
| 130 const int initial_backoff_ms_; |
| 131 const int additional_constant_ms_; |
| 132 const double multiply_factor_; |
| 133 const double jitter_factor_; |
| 134 const int maximum_backoff_ms_; |
| 135 // Set to -1 if the entry never expires. |
| 136 const int entry_lifetime_ms_; |
| 137 |
| 138 // Lock to protect the non-static data members. |
| 139 mutable Lock lock_; |
| 140 |
| 141 private: |
| 142 DISALLOW_COPY_AND_ASSIGN(RequestThrottlerEntry); |
| 143 }; |
| 144 |
| 145 #endif // NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_ |
| OLD | NEW |