| 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> time_until_release_us = delay_ms + 0.5; |
| 146 time_until_release_us *= base::Time::kMicrosecondsPerMillisecond; |
| 147 base::TimeDelta time_until_release = base::TimeDelta::FromMicroseconds( |
| 148 time_until_release_us.ValueOrDefault(kint64max)); |
| 149 base::TimeTicks release_time = |
| 150 TimeUntilReleaseToReleaseTime(time_until_release); |
| 151 |
| 152 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 153 // header. |
| 154 return std::max(release_time, exponential_backoff_release_time_); |
| 155 } |
| 156 |
| 157 base::TimeTicks BackoffEntry::TimeUntilReleaseToReleaseTime( |
| 158 base::TimeDelta time_until_release) const { |
| 145 const int64 kTimeTicksNowUs = | 159 const int64 kTimeTicksNowUs = |
| 146 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); | 160 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); |
| 161 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
| 147 base::internal::CheckedNumeric<int64> calculated_release_time_us = | 162 base::internal::CheckedNumeric<int64> calculated_release_time_us = |
| 148 delay_ms + 0.5; | 163 time_until_release.InMicroseconds(); |
| 149 calculated_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | |
| 150 calculated_release_time_us += kTimeTicksNowUs; | 164 calculated_release_time_us += kTimeTicksNowUs; |
| 151 | 165 |
| 152 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; | 166 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; |
| 153 if (policy_->maximum_backoff_ms >= 0) { | 167 if (policy_->maximum_backoff_ms >= 0) { |
| 154 maximum_release_time_us = policy_->maximum_backoff_ms; | 168 maximum_release_time_us = policy_->maximum_backoff_ms; |
| 155 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | 169 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
| 156 maximum_release_time_us += kTimeTicksNowUs; | 170 maximum_release_time_us += kTimeTicksNowUs; |
| 157 } | 171 } |
| 158 | 172 |
| 159 // Decide between maximum release time and calculated release time, accounting | 173 // Decide between maximum release time and calculated release time, accounting |
| 160 // for overflow with both. | 174 // for overflow with both. |
| 161 int64 release_time_us = std::min( | 175 int64 release_time_us = std::min( |
| 162 calculated_release_time_us.ValueOrDefault(kint64max), | 176 calculated_release_time_us.ValueOrDefault(kint64max), |
| 163 maximum_release_time_us.ValueOrDefault(kint64max)); | 177 maximum_release_time_us.ValueOrDefault(kint64max)); |
| 164 | 178 |
| 165 // Never reduce previously set release horizon, e.g. due to Retry-After | 179 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 } | 180 } |
| 171 | 181 |
| 172 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { | 182 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { |
| 173 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); | 183 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); |
| 174 } | 184 } |
| 175 | 185 |
| 176 } // namespace net | 186 } // namespace net |
| OLD | NEW |