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

Side by Side Diff: net/url_request/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/url_request/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/url_request/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 = 1.4;
19 const double RequestThrottlerEntry::kDefaultJitterFactor = 0.4;
20 const int RequestThrottlerEntry::kDefaultMaximumBackoffMs = 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_(
26 base::TimeDelta::FromMilliseconds(kDefaultSlidingWindowPeriodMs)),
27 max_send_threshold_(kDefaultMaxSendThreshold),
28 initial_backoff_ms_(kDefaultInitialBackoffMs),
29 additional_constant_ms_(kDefaultAdditionalConstantMs),
30 multiply_factor_(kDefaultMultiplyFactor),
31 jitter_factor_(kDefaultJitterFactor),
32 maximum_backoff_ms_(kDefaultMaximumBackoffMs),
33 entry_lifetime_ms_(kDefaultEntryLifetimeMs) {
34 Initialize();
35 }
36
37 RequestThrottlerEntry::RequestThrottlerEntry(
38 int sliding_window_period_ms,
39 int max_send_threshold,
40 int initial_backoff_ms,
41 int additional_constant_ms,
42 double multiply_factor,
43 double jitter_factor,
44 int maximum_backoff_ms)
45 : sliding_window_period_(
46 base::TimeDelta::FromMilliseconds(sliding_window_period_ms)),
47 max_send_threshold_(max_send_threshold),
48 initial_backoff_ms_(initial_backoff_ms),
49 additional_constant_ms_(additional_constant_ms),
50 multiply_factor_(multiply_factor),
51 jitter_factor_(jitter_factor),
52 maximum_backoff_ms_(maximum_backoff_ms),
53 entry_lifetime_ms_(-1) {
54 DCHECK(sliding_window_period_ms > 0 &&
55 max_send_threshold_ > 0 &&
56 initial_backoff_ms_ >= 0 &&
57 additional_constant_ms_ >= 0 &&
58 multiply_factor_ > 0 &&
59 jitter_factor_ >= 0 && jitter_factor_ < 1 &&
60 maximum_backoff_ms_ >= 0);
61
62 Initialize();
63 }
64
65 RequestThrottlerEntry::~RequestThrottlerEntry() {
66 }
67
68 void RequestThrottlerEntry::Initialize() {
69 // Since this method is called by the constructors, GetTimeNow() (a virtual
70 // method) is not used.
71 exponential_backoff_release_time_ = base::TimeTicks::Now();
72 failure_count_ = 0;
73 latest_response_was_failure_ = false;
74
75 sliding_window_release_time_ = base::TimeTicks::Now();
76 }
77
78 bool RequestThrottlerEntry::IsDuringExponentialBackoff() const {
79 AutoLock auto_lock(lock_);
80 return exponential_backoff_release_time_ > GetTimeNow();
81 }
82
83 int64 RequestThrottlerEntry::GetRecommendedDelayForNextRequest() {
84 AutoLock auto_lock(lock_);
85
86 base::TimeTicks now = GetTimeNow();
87 // If a lot of requests were successfully made recently,
88 // sliding_window_release_time_ may be greater than
89 // exponential_backoff_release_time_.
90 base::TimeTicks recommended_sending_time =
91 std::max(now, std::max(exponential_backoff_release_time_,
92 sliding_window_release_time_));
93
94 DCHECK(send_log_.empty() ||
95 recommended_sending_time >= send_log_.back());
96 // Log the new send event.
97 send_log_.push(recommended_sending_time);
98
99 sliding_window_release_time_ = recommended_sending_time;
100
101 // Drop the out-of-date events in the event list.
102 // We don't need to worry that the queue may become empty during this
103 // operation, since the last element is sliding_window_release_time_.
104 while ((send_log_.front() + sliding_window_period_ <=
105 sliding_window_release_time_) ||
106 send_log_.size() > static_cast<unsigned>(max_send_threshold_)) {
107 send_log_.pop();
108 }
109
110 // Check if there are too many send events in recent time.
111 if (send_log_.size() == static_cast<unsigned>(max_send_threshold_))
112 sliding_window_release_time_ = send_log_.front() + sliding_window_period_;
113
114 return (recommended_sending_time - now).InMillisecondsRoundedUp();
115 }
116
117 void RequestThrottlerEntry::UpdateWithResponse(
118 const RequestThrottlerHeaderInterface* response) {
119 AutoLock auto_lock(lock_);
120
121 if (response->GetResponseCode() >= 500) {
122 HandleFailure();
123 } else {
124 // We slowly decay the number of times delayed instead of resetting it to 0
125 // in order to stay stable if we received lots of requests with
126 // malformed bodies at the same time.
127 if (failure_count_ > 0)
128 failure_count_--;
129
130 latest_response_was_failure_ = false;
131
132 // The reason why we are not just cutting the release time to GetTimeNow()
133 // is on the one hand, it would unset delay put by our custom retry-after
134 // header and on the other we would like to push every request up to our
135 // "horizon" when dealing with multiple in-flight requests. Ex: If we send
136 // three requests and we receive 2 failures and 1 success. The success that
137 // follows those failures will not reset the release time, further requests
138 // will then need to wait the delay caused by the 2 failures.
139 exponential_backoff_release_time_ = std::max(
140 GetTimeNow(), exponential_backoff_release_time_);
141
142 std::string retry_header = response->GetNormalizedValue(kRetryHeaderName);
143 if (!retry_header.empty())
144 HandleCustomRetryAfter(retry_header);
145 }
146 }
147
148 bool RequestThrottlerEntry::IsEntryOutdated() const {
149 AutoLock auto_lock(lock_);
150
151 if (entry_lifetime_ms_ == -1)
152 return false;
153
154 base::TimeTicks now = GetTimeNow();
155
156 // If there are send events in the sliding window period, we still need this
157 // entry.
158 if (send_log_.size() > 0 &&
159 send_log_.back() + sliding_window_period_ > now) {
160 return false;
161 }
162
163 int64 unused_since_ms =
164 (now - exponential_backoff_release_time_).InMilliseconds();
165
166 // Release time is further than now, we are managing it.
167 if (unused_since_ms < 0)
168 return false;
169
170 // latest_response_was_failure_ is true indicates that the latest one or
171 // more requests encountered server errors or had malformed response bodies.
172 // In that case, we don't want to collect the entry unless it hasn't been used
173 // for longer than the maximum allowed back-off.
174 if (latest_response_was_failure_)
175 return unused_since_ms > std::max(maximum_backoff_ms_, entry_lifetime_ms_);
176
177 // Otherwise, consider the entry is outdated if it hasn't been used for the
178 // specified lifetime period.
179 return unused_since_ms > entry_lifetime_ms_;
180 }
181
182 void RequestThrottlerEntry::ReceivedContentWasMalformed() {
183 AutoLock auto_lock(lock_);
184 HandleFailure();
185 }
186
187 base::TimeTicks
188 RequestThrottlerEntry::CalculateExponentialBackoffReleaseTime() {
189 lock_.AssertAcquired();
190
191 double delay = initial_backoff_ms_;
192 delay *= pow(multiply_factor_, failure_count_);
193 delay += additional_constant_ms_;
194 delay -= base::RandDouble() * jitter_factor_ * delay;
195
196 // Ensure that we do not exceed maximum delay.
197 int64 delay_int = static_cast<int64>(delay + 0.5);
198 delay_int = std::min(delay_int, static_cast<int64>(maximum_backoff_ms_));
199
200 return std::max(GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int),
201 exponential_backoff_release_time_);
202 }
203
204 base::TimeTicks RequestThrottlerEntry::GetTimeNow() const {
205 return base::TimeTicks::Now();
206 }
207
208 void RequestThrottlerEntry::HandleCustomRetryAfter(
209 const std::string& header_value) {
210 lock_.AssertAcquired();
211
212 // Input parameter is the number of seconds to wait in a floating point value.
213 double time_in_sec = 0;
214 bool conversion_is_ok = base::StringToDouble(header_value, &time_in_sec);
215
216 // Conversion of custom retry-after header value failed.
217 if (!conversion_is_ok)
218 return;
219
220 // We must use an int value later so we transform this in milliseconds.
221 int64 value_ms = static_cast<int64>(0.5 + time_in_sec * 1000);
222
223 if (maximum_backoff_ms_ < value_ms || value_ms < 0)
224 return;
225
226 exponential_backoff_release_time_ = std::max(
227 (GetTimeNow() + base::TimeDelta::FromMilliseconds(value_ms)),
228 exponential_backoff_release_time_);
229 }
230
231 void RequestThrottlerEntry::HandleFailure() {
232 lock_.AssertAcquired();
233
234 failure_count_++;
235 latest_response_was_failure_ = true;
236 exponential_backoff_release_time_ = CalculateExponentialBackoffReleaseTime();
237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698