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

Side by Side Diff: net/request_throttler/request_throttler_entry.cc

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 #include "net/request_throttler/request_throttler_entry.h"
6
7 #include <cmath>
8
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/string_number_conversions.h"
12 #include "net/request_throttler/request_throttler_header_interface.h"
13
14 const int RequestThrottlerEntry::kDefaultSlidingWindowPeriodMs = 2000;
15 const int RequestThrottlerEntry::kDefaultMaxSendThreshold = 20;
16 const int RequestThrottlerEntry::kDefaultInitialBackoffMs = 700;
17 const int RequestThrottlerEntry::kDefaultAdditionalConstantMs = 100;
18 const double RequestThrottlerEntry::kDefaultMultiplyFactor = 2.0;
19 const double RequestThrottlerEntry::kDefaultJitterFactor = 0.4;
20 const int RequestThrottlerEntry::kDefaultMaximumBackoffMs = 24 * 60 * 60 * 1000;
21 const int RequestThrottlerEntry::kDefaultEntryLifetimeMs = 120000;
22 const char RequestThrottlerEntry::kRetryHeaderName[] = "X-Retry-After";
23
24 RequestThrottlerEntry::RequestThrottlerEntry()
25 : sliding_window_period_ms_(kDefaultSlidingWindowPeriodMs),
26 max_send_threshold_(kDefaultMaxSendThreshold),
27 initial_backoff_ms_(kDefaultInitialBackoffMs),
28 additional_constant_ms_(kDefaultAdditionalConstantMs),
29 multiply_factor_(kDefaultMultiplyFactor),
30 jitter_factor_(kDefaultJitterFactor),
31 maximum_backoff_ms_(kDefaultMaximumBackoffMs),
32 entry_lifetime_ms_(kDefaultEntryLifetimeMs) {
33 Initialize();
34 }
35
36 RequestThrottlerEntry::RequestThrottlerEntry(
37 int sliding_window_period_ms,
38 int max_send_threshold,
39 int initial_backoff_ms,
40 int additional_constant_ms,
41 double multiply_factor,
42 double jitter_factor,
43 int maximum_backoff_ms,
44 int entry_lifetime_ms)
45 : sliding_window_period_ms_(sliding_window_period_ms),
46 max_send_threshold_(max_send_threshold),
47 initial_backoff_ms_(initial_backoff_ms),
48 additional_constant_ms_(additional_constant_ms),
49 multiply_factor_(multiply_factor),
50 jitter_factor_(jitter_factor),
51 maximum_backoff_ms_(maximum_backoff_ms),
52 entry_lifetime_ms_(entry_lifetime_ms) {
53 DCHECK(sliding_window_period_ms_ > 0 &&
54 max_send_threshold_ > 0 &&
55 initial_backoff_ms_ >= 0 &&
56 additional_constant_ms_ >= 0 &&
57 multiply_factor_ > 0 &&
58 jitter_factor_ >= 0 &&
59 maximum_backoff_ms_ >= 0 &&
60 entry_lifetime_ms_ > 0);
61
62 Initialize();
63 }
64
65 RequestThrottlerEntry::~RequestThrottlerEntry() {
66 }
67
68 void RequestThrottlerEntry::Initialize() {
69 release_time_ = base::TimeTicks::Now();
70 num_times_delayed_ = 0;
71 is_managed_ = false;
72
73 old_values_.release_time = release_time_;
74 old_values_.number_of_failed_requests = num_times_delayed_;
75 }
76
77 bool RequestThrottlerEntry::IsRequestAllowed() const {
78 AutoLock auto_lock(lock_);
79 return release_time_ <= GetTimeNow();
80 }
81
82 void RequestThrottlerEntry::UpdateWithResponse(
83 const RequestThrottlerHeaderInterface* response) {
84 AutoLock auto_lock(lock_);
85
86 SaveState();
87 if (response->GetResponseCode() >= 500) {
88 num_times_delayed_++;
89 release_time_ = std::max(CalculateReleaseTime(), release_time_);
90 is_managed_ = true;
91 } else {
92 // We slowly decay the number of times delayed instead of resetting it to 0
93 // in order to stay stable if we received lots of requests with
94 // malformed bodies at the same time.
95 if (num_times_delayed_ > 0)
96 num_times_delayed_--;
97 is_managed_ = false;
98 // The reason why we are not just cutting release_time to GetTimeNow() is
99 // on the one hand, it would unset delay put by our custom retry-after
100 // header and on the other we would like to push every request up to our
101 // "horizon" when dealing with multiple in-flight request. Ex: If we send
102 // three request and we receive 2 failures and 1 success. The success that
103 // follows those failures will not reset release time further request will
104 // then need to wait the delay caused by the 2 failures.
105 release_time_ = std::max(GetTimeNow(), release_time_);
106 std::string retry_header = response->GetNormalizedValue(kRetryHeaderName);
107 if (!retry_header.empty())
108 HandleCustomRetryAfter(retry_header);
109 }
110 }
111
112 void RequestThrottlerEntry::NotifyRequestStart() {
113 AutoLock auto_lock(lock_);
114
115 base::TimeDelta sliding_window_period = base::TimeDelta::FromMilliseconds(
116 sliding_window_period_ms_);
117 release_time_ = std::max(release_time_, GetTimeNow());
118 if (send_log_.size() > 0)
119 release_time_ = std::max(release_time_, send_log_.back());
120
121 // Log the new send event.
122 send_log_.push(release_time_);
123
124 // Drop the out-of-date events in the event list.
125 // We don't need to worry that the queue may become empty during this
126 // operation, since the last element is release_time_.
127 while (send_log_.front() + sliding_window_period <= release_time_) {
128 send_log_.pop();
129 }
130
131 // Check if there are too many send events in recent time.
132 if (send_log_.size() >= static_cast<unsigned>(max_send_threshold_))
133 release_time_ = send_log_.front() + sliding_window_period;
134 }
135
136 bool RequestThrottlerEntry::IsEntryOutdated() const {
137 AutoLock auto_lock(lock_);
138
139 base::TimeTicks now = GetTimeNow();
140 int64 unused_since_ms = (now - release_time_).InMilliseconds();
141
142 // Release time is further than now, we are managing it.
143 if (unused_since_ms < 0)
144 return false;
145
146 // If there are send events in the sliding window period, we still need this
147 // entry.
148 base::TimeDelta sliding_window_period = base::TimeDelta::FromMilliseconds(
149 sliding_window_period_ms_);
150 if (send_log_.size() > 0 &&
151 send_log_.back() + sliding_window_period > now) {
152 return false;
153 }
154
155 // There are two cases. First one, when the entry is currently being managed
156 // and should not be collected unless it is older than the maximum allowed
157 // back-off. The other one, when the entry is outdated, unmanaged and should
158 // be collected.
159 if (is_managed_)
160 return unused_since_ms > std::max(maximum_backoff_ms_, entry_lifetime_ms_);
161
162 return unused_since_ms > entry_lifetime_ms_;
163 }
164
165 void RequestThrottlerEntry::ReceivedContentWasMalformed() {
166 AutoLock auto_lock(lock_);
167
168 // We should never revert to less back-off or else an attacker could put a
169 // malformed body in cache and replay it to decrease delay.
170 num_times_delayed_ =
171 std::max(old_values_.number_of_failed_requests, num_times_delayed_);
172 num_times_delayed_++;
173 is_managed_ = true;
174 release_time_ = std::max(CalculateReleaseTime(),
175 std::max(old_values_.release_time, release_time_));
176 }
177
178 base::TimeTicks RequestThrottlerEntry::release_time() const {
179 AutoLock auto_lock(lock_);
180 return release_time_;
181 }
182
183 base::TimeTicks RequestThrottlerEntry::CalculateReleaseTime() {
184 lock_.AssertAcquired();
185
186 double delay = initial_backoff_ms_;
187 delay *= pow(multiply_factor_, num_times_delayed_);
188 delay += additional_constant_ms_;
189 delay -= base::RandDouble() * jitter_factor_ * delay;
190
191 // Ensure that we do not exceed maximum delay.
192 int64 delay_int = static_cast<int64>(delay + 0.5);
193 delay_int = std::min(delay_int, static_cast<int64>(maximum_backoff_ms_));
194
195 return GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int);
196 }
197
198 base::TimeTicks RequestThrottlerEntry::GetTimeNow() const {
199 return base::TimeTicks::Now();
200 }
201
202 void RequestThrottlerEntry::HandleCustomRetryAfter(
203 const std::string& header_value) {
204 lock_.AssertAcquired();
205
206 // Input parameter is the number of seconds to wait in a floating point value.
207 double time_in_sec = 0;
208 bool conversion_is_ok = base::StringToDouble(header_value, &time_in_sec);
209
210 // Conversion of custom retry-after header value failed.
211 if (!conversion_is_ok)
212 return;
213
214 // We must use an int value later so we transform this in milliseconds.
215 int64 value_ms = static_cast<int64>(0.5 + time_in_sec * 1000);
216
217 if (maximum_backoff_ms_ < value_ms || value_ms < 0)
218 return;
219
220 release_time_ = std::max(
221 (GetTimeNow() + base::TimeDelta::FromMilliseconds(value_ms)),
222 release_time_);
223 }
224
225 void RequestThrottlerEntry::SaveState() {
226 lock_.AssertAcquired();
227
228 old_values_.release_time = release_time_;
229 old_values_.number_of_failed_requests = num_times_delayed_;
230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698