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

Side by Side Diff: net/request_throttler/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_REQUEST_THROTTLER_REQUEST_THROTTLER_ENTRY_H_
6 #define NET_REQUEST_THROTTLER_REQUEST_THROTTLER_ENTRY_H_
7
8 #include "net/request_throttler/request_throttler_entry_interface.h"
9
10 #include <queue>
11 #include <string>
12
13 #include "base/lock.h"
14
15 // Represents an entry of the Request Throttler Manager.
16 class RequestThrottlerEntry : public RequestThrottlerEntryInterface {
17 public:
18 // Sliding window period.
19 static const int kDefaultSlidingWindowPeriodMs;
20
21 // Maximum number of requests allowed in sliding window period.
22 static const int kDefaultMaxSendThreshold;
23
24 // Initial delay.
25 static const int kDefaultInitialBackoffMs;
26
27 // Additional constant to adjust back-off.
28 static const int kDefaultAdditionalConstantMs;
29
30 // Factor by which the waiting time will be multiplied.
31 static const double kDefaultMultiplyFactor;
32
33 // Fuzzing percentage. ex: 10% will spread requests randomly
34 // between 90%-100% of the calculated time.
35 static const double kDefaultJitterFactor;
36
37 // Maximum amount of time we are willing to delay our request.
38 static const int kDefaultMaximumBackoffMs;
39
40 // Time after which the entry is considered outdated.
41 static const int kDefaultEntryLifetimeMs;
42
43 // Name of the header that servers can use to ask clients to delay their next
44 // request. ex: "X-Retry-After"
Jói 2010/11/12 00:12:08 Suggest removing the example name.
yzshen 2010/11/12 02:05:46 Done.
45 static const char kRetryHeaderName[];
46
47 RequestThrottlerEntry();
48
49 // It is used by unit tests.
50 RequestThrottlerEntry(int sliding_window_period_ms,
51 int max_send_threshold,
52 int initial_backoff_ms,
53 int additional_constant_ms,
54 double multiply_factor,
55 double jitter_factor,
56 int maximum_backoff_ms,
57 int entry_lifetime_ms);
58
59 ////// Implementation of RequestThrottlerEntryInterface ///////
60
61 // This method needs to be called prior to every request; if it returns
62 // false, the calling module must cancel its current request.
63 virtual bool IsRequestAllowed() const;
64
65 // This method needs to be called each time a response is received.
66 virtual void UpdateWithResponse(
67 const RequestThrottlerHeaderInterface* response);
68
69 // This method needs to be called each time when a request is actually sent.
70 void NotifyRequestStart();
71
72 ////////// Specific methods of RequestThrottlerEntry ////////////////
73
74 // Used by the manager, returns if the entry needs to be garbage collected.
75 bool IsEntryOutdated() const;
76
77 // Used by the manager, enables the manager to flag the last successful
78 // request as a failure.
79 void ReceivedContentWasMalformed();
80
81 base::TimeTicks release_time() const;
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 release_time;
89 int number_of_failed_requests;
90 };
91
92 virtual ~RequestThrottlerEntry();
93
94 void Initialize();
95
96 // Calculates when we should start sending requests again. Follows a failure
97 // response.
98 base::TimeTicks CalculateReleaseTime();
99
100 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
101 virtual base::TimeTicks GetTimeNow() const;
102
103 // Used internally to increase release time following a retry-after header.
104 void HandleCustomRetryAfter(const std::string& header_value);
105
106 // Saves the state of the object to be able to regenerate it.
107 // Must be informed of the state of the response.
108 void SaveState();
109
110 // This contains the timestamp at which we are allowed to start sending
111 // requests again.
112 base::TimeTicks release_time_;
113
114 // Number of times we were delayed.
115 int num_times_delayed_;
116
117 // Are we currently managing this request.
118 bool is_managed_;
119
120 OldValues old_values_;
121
122 // A list of the recent send events. We use them to decide whether there are
joi 2010/11/09 20:07:17 I haven't looked in detail yet, but if I understan
yzshen 2010/11/09 22:32:22 Yes, that is how it works.
123 // too many requests sent in sliding window.
124 std::queue<base::TimeTicks> send_log_;
125
126 const int sliding_window_period_ms_;
127 const int max_send_threshold_;
128 const int initial_backoff_ms_;
129 const int additional_constant_ms_;
130 const double multiply_factor_;
131 const double jitter_factor_;
132 const int maximum_backoff_ms_;
133 const int entry_lifetime_ms_;
134
135 // Lock to protect the non-static data members.
136 mutable Lock lock_;
137
138 private:
139 DISALLOW_COPY_AND_ASSIGN(RequestThrottlerEntry);
140 };
141
142 #endif // NET_REQUEST_THROTTLER_REQUEST_THROTTLER_ENTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698