Chromium Code Reviews
|
| 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. | |
| 22 // Secondly, a sliding window is used to count recent requests to a given | |
| 23 // destination and provide guidance (to the application level only) on whether | |
| 24 // too many requests have been sent and when a good time to send the next one | |
| 25 // would be. This is never used to deny requests at the network level. | |
| 26 class RequestThrottlerEntry : public RequestThrottlerEntryInterface { | |
| 27 public: | |
| 28 // Sliding window period. | |
| 29 static const int kDefaultSlidingWindowPeriodMs; | |
| 30 | |
| 31 // Maximum number of requests allowed in sliding window period. | |
| 32 static const int kDefaultMaxSendThreshold; | |
| 33 | |
| 34 // Initial delay for exponential back-off. | |
| 35 static const int kDefaultInitialBackoffMs; | |
| 36 | |
| 37 // Additional constant to adjust back-off. | |
| 38 static const int kDefaultAdditionalConstantMs; | |
| 39 | |
| 40 // Factor by which the waiting time will be multiplied. | |
| 41 static const double kDefaultMultiplyFactor; | |
| 42 | |
| 43 // Fuzzing percentage. ex: 10% will spread requests randomly | |
| 44 // between 90%-100% of the calculated time. | |
| 45 static const double kDefaultJitterFactor; | |
| 46 | |
| 47 // Maximum amount of time we are willing to delay our request. | |
| 48 static const int kDefaultMaximumBackoffMs; | |
| 49 | |
| 50 // Time after which the entry is considered outdated. | |
| 51 static const int kDefaultEntryLifetimeMs; | |
| 52 | |
| 53 // Name of the header that servers can use to ask clients to delay their next | |
| 54 // request. | |
| 55 static const char kRetryHeaderName[]; | |
| 56 | |
| 57 RequestThrottlerEntry(); | |
| 58 | |
| 59 // The life span of instances created with this constructor is set to | |
| 60 // infinite. | |
| 61 RequestThrottlerEntry(int sliding_window_period_ms, | |
| 62 int max_send_threshold, | |
| 63 int initial_backoff_ms, | |
| 64 int additional_constant_ms, | |
| 65 double multiply_factor, | |
| 66 double jitter_factor, | |
| 67 int maximum_backoff_ms); | |
| 68 | |
| 69 // Implementation of RequestThrottlerEntryInterface. | |
| 70 virtual bool IsDuringExponentialBackoff() const; | |
| 71 virtual int64 GetRecommendedDelayForNextRequest(); | |
| 72 virtual void UpdateWithResponse( | |
| 73 const RequestThrottlerHeaderInterface* response); | |
| 74 | |
| 75 // Used by the manager, returns true if the entry needs to be garbage | |
| 76 // collected. | |
| 77 bool IsEntryOutdated() const; | |
| 78 | |
| 79 // Used by the manager, enables the manager to flag the last successful | |
| 80 // request as a failure. | |
| 81 void ReceivedContentWasMalformed(); | |
| 82 | |
| 83 protected: | |
| 84 // This struct is used to save the state of the entry each time we are updated | |
| 85 // with a response header so we can regenerate it if we are informed that one | |
| 86 // of our bodies was malformed. | |
| 87 struct OldValues { | |
| 88 base::TimeTicks exponential_backoff_release_time; | |
| 89 int failure_count; | |
| 90 }; | |
| 91 | |
| 92 virtual ~RequestThrottlerEntry(); | |
| 93 | |
| 94 void Initialize(); | |
| 95 | |
| 96 // Calculates the release time for exponential back-off. | |
| 97 base::TimeTicks CalculateExponentialBackoffReleaseTime(); | |
| 98 | |
| 99 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. | |
| 100 virtual base::TimeTicks GetTimeNow() const; | |
| 101 | |
| 102 // Used internally to increase release time following a retry-after header. | |
| 103 void HandleCustomRetryAfter(const std::string& header_value); | |
| 104 | |
| 105 // Saves the state of the object to be able to regenerate it. | |
| 106 // Must be informed of the state of the response. | |
| 107 void SaveState(); | |
| 108 | |
| 109 // Timestamp calculated by the exponential back-off algorithm at which we are | |
| 110 // allowed to start sending requests again. | |
| 111 base::TimeTicks exponential_backoff_release_time_; | |
| 112 | |
| 113 // Number of times we encounter server errors or malformed response bodies. | |
| 114 int failure_count_; | |
| 115 | |
| 116 // Set to true if we are currently tracking requests with server errors or | |
| 117 // malformed response bodies, in order to calculate the exponential back-off | |
| 118 // release time. | |
| 119 bool tracking_exponential_backoff_; | |
| 120 | |
| 121 OldValues old_values_; | |
|
Jói
2010/11/12 19:14:24
(Outdated comment because of my comment in the uni
yzshen
2010/11/17 07:31:43
Removed OldValues and will keep your suggestion in
| |
| 122 | |
| 123 // Timestamp calculated by the sliding window algorithm at which we are | |
| 124 // allowed to start sending requests again. | |
|
Jói
2010/11/12 19:14:24
This comment makes it sound like we will deny requ
yzshen
2010/11/17 07:31:43
Done.
| |
| 125 base::TimeTicks sliding_window_release_time_; | |
| 126 | |
| 127 // A list of the recent send events. We use them to decide whether there are | |
| 128 // too many requests sent in sliding window. | |
| 129 std::queue<base::TimeTicks> send_log_; | |
| 130 | |
| 131 const int sliding_window_period_ms_; | |
|
Jói
2010/11/12 19:14:24
for any of these constants in milliseconds that ge
yzshen
2010/11/17 07:31:43
Done. I think only the first one is always used as
| |
| 132 const int max_send_threshold_; | |
| 133 const int initial_backoff_ms_; | |
| 134 const int additional_constant_ms_; | |
| 135 const double multiply_factor_; | |
| 136 const double jitter_factor_; | |
| 137 const int maximum_backoff_ms_; | |
| 138 // Set to -1 if the entry never expires. | |
| 139 const int entry_lifetime_ms_; | |
| 140 | |
| 141 // Lock to protect the non-static data members. | |
| 142 mutable Lock lock_; | |
| 143 | |
| 144 private: | |
| 145 DISALLOW_COPY_AND_ASSIGN(RequestThrottlerEntry); | |
| 146 }; | |
| 147 | |
| 148 #endif // NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_ | |
| OLD | NEW |