Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: net/url_request/request_throttler_entry.h

Issue 4194001: Implement exponential back-off mechanism and enforce it at the URLRequestHttpJob level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 <queue>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "net/url_request/request_throttler_entry_interface.h"
13
14 // RequestThrottlerEntry represents an entry of RequestThrottlerManager.
15 // It analyzes requests of a specific URL over some period of time, in order to
16 // deduce the back-off time for every request.
17 // The back-off algorithm consists of two parts. Firstly, exponential back-off
18 // is used when receiving 5XX server errors or malformed response bodies.
19 // The exponential back-off rule is enforced by URLRequestHttpJob. Any request
20 // sent during the back-off period will be cancelled.
21 // Secondly, a sliding window is used to count recent requests to a given
22 // destination and provide guidance (to the application level only) on whether
23 // too many requests have been sent and when a good time to send the next one
24 // would be. This is never used to deny requests at the network level.
25 class RequestThrottlerEntry : public RequestThrottlerEntryInterface {
26 public:
27 // Sliding window period.
28 static const int kDefaultSlidingWindowPeriodMs;
29
30 // Maximum number of requests allowed in sliding window period.
31 static const int kDefaultMaxSendThreshold;
32
33 // Initial delay for exponential back-off.
34 static const int kDefaultInitialBackoffMs;
35
36 // Additional constant to adjust back-off.
37 static const int kDefaultAdditionalConstantMs;
38
39 // Factor by which the waiting time will be multiplied.
40 static const double kDefaultMultiplyFactor;
41
42 // Fuzzing percentage. ex: 10% will spread requests randomly
43 // between 90%-100% of the calculated time.
44 static const double kDefaultJitterFactor;
45
46 // Maximum amount of time we are willing to delay our request.
47 static const int kDefaultMaximumBackoffMs;
48
49 // Time after which the entry is considered outdated.
50 static const int kDefaultEntryLifetimeMs;
51
52 // Name of the header that servers can use to ask clients to delay their next
53 // request.
54 static const char kRetryHeaderName[];
55
56 RequestThrottlerEntry();
57
58 // The life span of instances created with this constructor is set to
59 // infinite.
60 // It is only used by unit tests.
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 ReserveSendingTimeForNextRequest(
72 const base::TimeTicks& earliest_time);
73 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const;
74 virtual void UpdateWithResponse(
75 const RequestThrottlerHeaderInterface* response);
76 virtual void ReceivedContentWasMalformed();
77
78 // Used by the manager, returns true if the entry needs to be garbage
79 // collected.
80 bool IsEntryOutdated() const;
81
82 protected:
83 virtual ~RequestThrottlerEntry();
84
85 void Initialize();
86
87 // Calculates the release time for exponential back-off.
88 base::TimeTicks CalculateExponentialBackoffReleaseTime();
89
90 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
91 virtual base::TimeTicks GetTimeNow() const;
92
93 // Used internally to increase release time following a retry-after header.
94 void HandleCustomRetryAfter(const std::string& header_value);
95
96 // Used by tests.
97 void set_exponential_backoff_release_time(
98 const base::TimeTicks& release_time) {
99 exponential_backoff_release_time_ = release_time;
100 }
101
102 // Used by tests.
103 base::TimeTicks sliding_window_release_time() const {
104 return sliding_window_release_time_;
105 }
106
107 // Used by tests.
108 void set_sliding_window_release_time(const base::TimeTicks& release_time) {
109 sliding_window_release_time_ = release_time;
110 }
111
112 // Used by tests.
113 void set_failure_count(int failure_count) {
114 failure_count_ = failure_count;
115 }
116
117 private:
118 // Timestamp calculated by the exponential back-off algorithm at which we are
119 // allowed to start sending requests again.
120 base::TimeTicks exponential_backoff_release_time_;
121
122 // Number of times we encounter server errors or malformed response bodies.
123 int failure_count_;
124
125 // If true, the last request response was a failure.
126 // Note that this member can be false at the same time as failure_count_ can
127 // be greater than 0, since we gradually decrease failure_count_, instead of
128 // resetting it to 0 directly, when we receive successful responses.
129 bool latest_response_was_failure_;
130
131 // Timestamp calculated by the sliding window algorithm for when we advise
132 // clients the next request should be made, at the earliest. Advisory only,
133 // not used to deny requests.
134 base::TimeTicks sliding_window_release_time_;
135
136 // A list of the recent send events. We use them to decide whether there are
137 // too many requests sent in sliding window.
138 std::queue<base::TimeTicks> send_log_;
139
140 const base::TimeDelta sliding_window_period_;
141 const int max_send_threshold_;
142 const int initial_backoff_ms_;
143 const int additional_constant_ms_;
144 const double multiply_factor_;
145 const double jitter_factor_;
146 const int maximum_backoff_ms_;
147 // Set to -1 if the entry never expires.
148 const int entry_lifetime_ms_;
149
150 DISALLOW_COPY_AND_ASSIGN(RequestThrottlerEntry);
151 };
152
153 #endif // NET_URL_REQUEST_REQUEST_THROTTLER_ENTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698