OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/backoff_entry.h" | 5 #include "net/base/backoff_entry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] | 135 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] |
136 // Note: if the failure count is too high, |delay_ms| will become infinity | 136 // Note: if the failure count is too high, |delay_ms| will become infinity |
137 // after the exponential calculation, and then NaN after the jitter is | 137 // after the exponential calculation, and then NaN after the jitter is |
138 // accounted for. Both cases are handled by using CheckedNumeric<int64> to | 138 // accounted for. Both cases are handled by using CheckedNumeric<int64> to |
139 // perform the conversion to integers. | 139 // perform the conversion to integers. |
140 double delay_ms = policy_->initial_delay_ms; | 140 double delay_ms = policy_->initial_delay_ms; |
141 delay_ms *= pow(policy_->multiply_factor, effective_failure_count - 1); | 141 delay_ms *= pow(policy_->multiply_factor, effective_failure_count - 1); |
142 delay_ms -= base::RandDouble() * policy_->jitter_factor * delay_ms; | 142 delay_ms -= base::RandDouble() * policy_->jitter_factor * delay_ms; |
143 | 143 |
144 // Do overflow checking in microseconds, the internal unit of TimeTicks. | 144 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
| 145 base::internal::CheckedNumeric<int64> backoff_duration_us = delay_ms + 0.5; |
| 146 backoff_duration_us *= base::Time::kMicrosecondsPerMillisecond; |
| 147 base::TimeDelta backoff_duration = base::TimeDelta::FromMicroseconds( |
| 148 backoff_duration_us.ValueOrDefault(kint64max)); |
| 149 base::TimeTicks release_time = BackoffDurationToReleaseTime(backoff_duration); |
| 150 |
| 151 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 152 // header. |
| 153 return std::max(release_time, exponential_backoff_release_time_); |
| 154 } |
| 155 |
| 156 base::TimeTicks BackoffEntry::BackoffDurationToReleaseTime( |
| 157 base::TimeDelta backoff_duration) const { |
145 const int64 kTimeTicksNowUs = | 158 const int64 kTimeTicksNowUs = |
146 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); | 159 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); |
| 160 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
147 base::internal::CheckedNumeric<int64> calculated_release_time_us = | 161 base::internal::CheckedNumeric<int64> calculated_release_time_us = |
148 delay_ms + 0.5; | 162 backoff_duration.InMicroseconds(); |
149 calculated_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | |
150 calculated_release_time_us += kTimeTicksNowUs; | 163 calculated_release_time_us += kTimeTicksNowUs; |
151 | 164 |
152 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; | 165 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; |
153 if (policy_->maximum_backoff_ms >= 0) { | 166 if (policy_->maximum_backoff_ms >= 0) { |
154 maximum_release_time_us = policy_->maximum_backoff_ms; | 167 maximum_release_time_us = policy_->maximum_backoff_ms; |
155 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | 168 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
156 maximum_release_time_us += kTimeTicksNowUs; | 169 maximum_release_time_us += kTimeTicksNowUs; |
157 } | 170 } |
158 | 171 |
159 // Decide between maximum release time and calculated release time, accounting | 172 // Decide between maximum release time and calculated release time, accounting |
160 // for overflow with both. | 173 // for overflow with both. |
161 int64 release_time_us = std::min( | 174 int64 release_time_us = std::min( |
162 calculated_release_time_us.ValueOrDefault(kint64max), | 175 calculated_release_time_us.ValueOrDefault(kint64max), |
163 maximum_release_time_us.ValueOrDefault(kint64max)); | 176 maximum_release_time_us.ValueOrDefault(kint64max)); |
164 | 177 |
165 // Never reduce previously set release horizon, e.g. due to Retry-After | 178 return base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us); |
166 // header. | |
167 return std::max( | |
168 base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us), | |
169 exponential_backoff_release_time_); | |
170 } | 179 } |
171 | 180 |
172 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { | 181 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { |
173 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); | 182 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); |
174 } | 183 } |
175 | 184 |
176 } // namespace net | 185 } // namespace net |
OLD | NEW |