Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "net/base/backoff_entry.h" | 13 #include "net/base/backoff_entry.h" |
| 14 #include "net/url_request/url_request_throttler_entry_interface.h" | 14 #include "net/url_request/url_request_throttler_entry_interface.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 class URLRequestThrottlerManager; | |
| 19 | |
| 18 // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager. | 20 // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager. |
| 19 // It analyzes requests of a specific URL over some period of time, in order to | 21 // It analyzes requests of a specific URL over some period of time, in order to |
| 20 // deduce the back-off time for every request. | 22 // deduce the back-off time for every request. |
| 21 // The back-off algorithm consists of two parts. Firstly, exponential back-off | 23 // The back-off algorithm consists of two parts. Firstly, exponential back-off |
| 22 // is used when receiving 5XX server errors or malformed response bodies. | 24 // is used when receiving 5XX server errors or malformed response bodies. |
| 23 // The exponential back-off rule is enforced by URLRequestHttpJob. Any | 25 // The exponential back-off rule is enforced by URLRequestHttpJob. Any |
| 24 // request sent during the back-off period will be cancelled. | 26 // request sent during the back-off period will be cancelled. |
| 25 // Secondly, a sliding window is used to count recent requests to a given | 27 // Secondly, a sliding window is used to count recent requests to a given |
| 26 // destination and provide guidance (to the application level only) on whether | 28 // destination and provide guidance (to the application level only) on whether |
| 27 // too many requests have been sent and when a good time to send the next one | 29 // too many requests have been sent and when a good time to send the next one |
| 28 // would be. This is never used to deny requests at the network level. | 30 // would be. This is never used to deny requests at the network level. |
| 29 class URLRequestThrottlerEntry : public URLRequestThrottlerEntryInterface { | 31 class URLRequestThrottlerEntry : public URLRequestThrottlerEntryInterface { |
| 30 public: | 32 public: |
| 31 // Sliding window period. | 33 // Sliding window period. |
| 32 static const int kDefaultSlidingWindowPeriodMs; | 34 static const int kDefaultSlidingWindowPeriodMs; |
| 33 | 35 |
| 34 // Maximum number of requests allowed in sliding window period. | 36 // Maximum number of requests allowed in sliding window period. |
| 35 static const int kDefaultMaxSendThreshold; | 37 static const int kDefaultMaxSendThreshold; |
| 36 | 38 |
| 39 // Number of initial errors to ignore before starting exponential back-off. | |
| 40 static const int kDefaultNumErrorsToIgnore; | |
| 41 | |
| 37 // Initial delay for exponential back-off. | 42 // Initial delay for exponential back-off. |
| 38 static const int kDefaultInitialBackoffMs; | 43 static const int kDefaultInitialBackoffMs; |
| 39 | 44 |
| 40 // Factor by which the waiting time will be multiplied. | 45 // Factor by which the waiting time will be multiplied. |
| 41 static const double kDefaultMultiplyFactor; | 46 static const double kDefaultMultiplyFactor; |
| 42 | 47 |
| 43 // Fuzzing percentage. ex: 10% will spread requests randomly | 48 // Fuzzing percentage. ex: 10% will spread requests randomly |
| 44 // between 90%-100% of the calculated time. | 49 // between 90%-100% of the calculated time. |
| 45 static const double kDefaultJitterFactor; | 50 static const double kDefaultJitterFactor; |
| 46 | 51 |
| 47 // Maximum amount of time we are willing to delay our request. | 52 // Maximum amount of time we are willing to delay our request. |
| 48 static const int kDefaultMaximumBackoffMs; | 53 static const int kDefaultMaximumBackoffMs; |
| 49 | 54 |
| 50 // Time after which the entry is considered outdated. | 55 // Time after which the entry is considered outdated. |
| 51 static const int kDefaultEntryLifetimeMs; | 56 static const int kDefaultEntryLifetimeMs; |
| 52 | 57 |
| 53 // Name of the header that servers can use to ask clients to delay their next | 58 // Name of the header that servers can use to ask clients to delay their |
| 54 // request. | 59 // next request. |
| 55 static const char kRetryHeaderName[]; | 60 static const char kRetryHeaderName[]; |
| 56 | 61 |
| 57 URLRequestThrottlerEntry(); | 62 // Name of the header that sites can use to opt out of exponential back-off |
| 63 // throttling. | |
| 64 static const char kExponentialThrottlingHeader[]; | |
| 65 | |
| 66 // Value for exponential throttling header that can be used to opt out of | |
| 67 // exponential back-off throttling. | |
| 68 static const char kExponentialThrottlingDisableValue[]; | |
| 69 | |
| 70 // The manager object's lifetime must enclose the lifetime of this object. | |
|
yzshen1
2011/03/18 22:49:51
It is unlikely that the entries will outlive the m
Jói
2011/03/23 23:38:24
I'm not sure about making the entries not ref-coun
yzshen1
2011/03/24 20:06:37
Sounds good.
Another thing that might worth consi
Jói
2011/03/24 22:03:45
Thanks, that's a good suggestion, but seeing as th
| |
| 71 URLRequestThrottlerEntry(URLRequestThrottlerManager* manager); | |
|
yzshen1
2011/03/18 22:49:51
Please use explicit.
Jói
2011/03/23 23:38:24
Done.
| |
| 58 | 72 |
| 59 // The life span of instances created with this constructor is set to | 73 // The life span of instances created with this constructor is set to |
| 60 // infinite. | 74 // infinite. |
| 61 // It is only used by unit tests. | 75 // It is only used by unit tests. |
| 62 URLRequestThrottlerEntry(int sliding_window_period_ms, | 76 URLRequestThrottlerEntry(URLRequestThrottlerManager* manager, |
| 77 int sliding_window_period_ms, | |
| 63 int max_send_threshold, | 78 int max_send_threshold, |
| 64 int initial_backoff_ms, | 79 int initial_backoff_ms, |
| 65 double multiply_factor, | 80 double multiply_factor, |
| 66 double jitter_factor, | 81 double jitter_factor, |
| 67 int maximum_backoff_ms); | 82 int maximum_backoff_ms); |
| 68 | 83 |
| 69 // Used by the manager, returns true if the entry needs to be garbage | 84 // Used by the manager, returns true if the entry needs to be garbage |
| 70 // collected. | 85 // collected. |
| 71 bool IsEntryOutdated() const; | 86 bool IsEntryOutdated() const; |
| 72 | 87 |
| 88 // Causes this entry to never reject requests due to back-off. | |
| 89 void DisableBackoffThrottling(); | |
| 90 | |
| 73 // Implementation of URLRequestThrottlerEntryInterface. | 91 // Implementation of URLRequestThrottlerEntryInterface. |
| 74 virtual bool IsDuringExponentialBackoff() const; | 92 virtual bool IsDuringExponentialBackoff() const; |
| 75 virtual int64 ReserveSendingTimeForNextRequest( | 93 virtual int64 ReserveSendingTimeForNextRequest( |
| 76 const base::TimeTicks& earliest_time); | 94 const base::TimeTicks& earliest_time); |
| 77 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const; | 95 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const; |
| 78 virtual void UpdateWithResponse( | 96 virtual void UpdateWithResponse( |
| 97 const std::string& host, | |
| 79 const URLRequestThrottlerHeaderInterface* response); | 98 const URLRequestThrottlerHeaderInterface* response); |
| 80 virtual void ReceivedContentWasMalformed(); | 99 virtual void ReceivedContentWasMalformed(); |
| 81 | 100 |
| 82 protected: | 101 protected: |
| 83 virtual ~URLRequestThrottlerEntry(); | 102 virtual ~URLRequestThrottlerEntry(); |
| 84 | 103 |
| 85 void Initialize(); | 104 void Initialize(); |
| 86 | 105 |
| 87 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. | 106 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. |
| 88 virtual base::TimeTicks GetTimeNow() const; | 107 virtual base::TimeTicks GetTimeNow() const; |
| 89 | 108 |
| 90 // Used internally to increase release time following a retry-after header. | 109 // Used internally to increase release time following a retry-after header. |
| 91 void HandleCustomRetryAfter(const std::string& header_value); | 110 void HandleCustomRetryAfter(const std::string& header_value); |
| 92 | 111 |
| 93 // Retrieves the backoff entry object we're using. Used to enable a | 112 // Used internally to handle the opt-out header. |
| 113 void HandleThrottlingHeader(const std::string& header_value, | |
| 114 const std::string& host); | |
| 115 | |
| 116 // Retrieves the back-off entry object we're using. Used to enable a | |
| 94 // unit testing seam for dependency injection in tests. | 117 // unit testing seam for dependency injection in tests. |
| 95 virtual const BackoffEntry* GetBackoffEntry() const; | 118 virtual const BackoffEntry* GetBackoffEntry() const; |
| 96 virtual BackoffEntry* GetBackoffEntry(); | 119 virtual BackoffEntry* GetBackoffEntry(); |
| 97 | 120 |
| 98 // Used by tests. | 121 // Used by tests. |
| 99 base::TimeTicks sliding_window_release_time() const { | 122 base::TimeTicks sliding_window_release_time() const { |
| 100 return sliding_window_release_time_; | 123 return sliding_window_release_time_; |
| 101 } | 124 } |
| 102 | 125 |
| 103 // Used by tests. | 126 // Used by tests. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 114 // not used to deny requests. | 137 // not used to deny requests. |
| 115 base::TimeTicks sliding_window_release_time_; | 138 base::TimeTicks sliding_window_release_time_; |
| 116 | 139 |
| 117 // A list of the recent send events. We use them to decide whether there are | 140 // A list of the recent send events. We use them to decide whether there are |
| 118 // too many requests sent in sliding window. | 141 // too many requests sent in sliding window. |
| 119 std::queue<base::TimeTicks> send_log_; | 142 std::queue<base::TimeTicks> send_log_; |
| 120 | 143 |
| 121 const base::TimeDelta sliding_window_period_; | 144 const base::TimeDelta sliding_window_period_; |
| 122 const int max_send_threshold_; | 145 const int max_send_threshold_; |
| 123 | 146 |
| 147 // True if DisableBackoffThrottling() has been called on this object. | |
| 148 bool is_backoff_disabled_; | |
| 149 | |
| 124 // Access it through GetBackoffEntry() to allow a unit test seam. | 150 // Access it through GetBackoffEntry() to allow a unit test seam. |
| 125 BackoffEntry backoff_entry_; | 151 BackoffEntry backoff_entry_; |
| 126 | 152 |
| 153 // Weak back-reference to the manager object managing us. | |
| 154 URLRequestThrottlerManager* manager_; | |
| 155 | |
| 127 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry); | 156 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry); |
| 128 }; | 157 }; |
| 129 | 158 |
| 130 } // namespace net | 159 } // namespace net |
| 131 | 160 |
| 132 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ | 161 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ |
| OLD | NEW |