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" | |
|
willchan no longer on Chromium
2010/11/17 20:32:34
http://google-styleguide.googlecode.com/svn/trunk/
yzshen
2010/11/19 23:51:36
Done.
| |
| 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 { | |
|
willchan no longer on Chromium
2010/11/17 20:32:34
Why is this in the global namespace? In general,
yzshen
2010/11/19 23:51:36
Since URLRequest/URLRequest.*Job are not in the ne
| |
| 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 // It is only used by unit tests. | |
| 62 RequestThrottlerEntry(int sliding_window_period_ms, | |
| 63 int max_send_threshold, | |
| 64 int initial_backoff_ms, | |
| 65 int additional_constant_ms, | |
| 66 double multiply_factor, | |
| 67 double jitter_factor, | |
| 68 int maximum_backoff_ms); | |
| 69 | |
| 70 // Implementation of RequestThrottlerEntryInterface. | |
| 71 virtual bool IsDuringExponentialBackoff() const; | |
| 72 virtual int64 GetRecommendedDelayForNextRequest(); | |
| 73 virtual void UpdateWithResponse( | |
| 74 const RequestThrottlerHeaderInterface* response); | |
| 75 virtual void ReceivedContentWasMalformed(); | |
| 76 | |
| 77 // Used by the manager, returns true if the entry needs to be garbage | |
| 78 // collected. | |
| 79 bool IsEntryOutdated() const; | |
| 80 | |
| 81 protected: | |
| 82 virtual ~RequestThrottlerEntry(); | |
| 83 | |
| 84 void Initialize(); | |
| 85 | |
| 86 // Calculates the release time for exponential back-off. | |
| 87 base::TimeTicks CalculateExponentialBackoffReleaseTime(); | |
| 88 | |
| 89 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. | |
| 90 virtual base::TimeTicks GetTimeNow() const; | |
| 91 | |
| 92 // Used internally to increase release time following a retry-after header. | |
| 93 void HandleCustomRetryAfter(const std::string& header_value); | |
| 94 | |
| 95 // Handles 5XX server errors or malformed responses. | |
| 96 void HandleFailure(); | |
| 97 | |
|
willchan no longer on Chromium
2010/11/17 20:32:34
All member variables should be private. See http:
yzshen
2010/11/19 23:51:36
Thanks! Will keep in mind. :)
| |
| 98 // Timestamp calculated by the exponential back-off algorithm at which we are | |
| 99 // allowed to start sending requests again. | |
| 100 base::TimeTicks exponential_backoff_release_time_; | |
| 101 | |
| 102 // Number of times we encounter server errors or malformed response bodies. | |
| 103 int failure_count_; | |
| 104 | |
| 105 // If true, the last request response was a failure. | |
| 106 // Note that this member can be false at the same time as failure_count_ can | |
| 107 // be greater than 0, since we gradually decrease failure_count_, instead of | |
| 108 // resetting it to 0 directly, when we receive successful responses. | |
| 109 bool latest_response_was_failure_; | |
| 110 | |
| 111 // Timestamp calculated by the sliding window algorithm for when we advise | |
| 112 // clients the next request should be made, at the earliest. Advisory only, | |
| 113 // not used to deny requests. | |
| 114 base::TimeTicks sliding_window_release_time_; | |
| 115 | |
| 116 // A list of the recent send events. We use them to decide whether there are | |
| 117 // too many requests sent in sliding window. | |
| 118 std::queue<base::TimeTicks> send_log_; | |
| 119 | |
| 120 const base::TimeDelta sliding_window_period_; | |
| 121 const int max_send_threshold_; | |
| 122 const int initial_backoff_ms_; | |
| 123 const int additional_constant_ms_; | |
| 124 const double multiply_factor_; | |
| 125 const double jitter_factor_; | |
| 126 const int maximum_backoff_ms_; | |
| 127 // Set to -1 if the entry never expires. | |
| 128 const int entry_lifetime_ms_; | |
| 129 | |
| 130 // Lock to protect the non-static data members. | |
| 131 mutable Lock lock_; | |
| 132 | |
| 133 private: | |
| 134 DISALLOW_COPY_AND_ASSIGN(RequestThrottlerEntry); | |
|
willchan no longer on Chromium
2010/11/17 20:32:34
You need basictypes.h for this macro. You're tran
yzshen
2010/11/19 23:51:36
Done.
| |
| 135 }; | |
| 136 | |
| 137 #endif // NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_ | |
| OLD | NEW |