| 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 |
| 11 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/numerics/safe_math.h" |
| 12 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 13 | 15 |
| 14 namespace net { | 16 namespace net { |
| 15 | 17 |
| 16 BackoffEntry::BackoffEntry(const BackoffEntry::Policy* const policy) | 18 BackoffEntry::BackoffEntry(const BackoffEntry::Policy* const policy) |
| 17 : policy_(policy) { | 19 : policy_(policy) { |
| 18 DCHECK(policy_); | 20 DCHECK(policy_); |
| 19 Reset(); | 21 Reset(); |
| 20 } | 22 } |
| 21 | 23 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 127 |
| 126 if (effective_failure_count == 0) { | 128 if (effective_failure_count == 0) { |
| 127 // Never reduce previously set release horizon, e.g. due to Retry-After | 129 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 128 // header. | 130 // header. |
| 129 return std::max(ImplGetTimeNow(), exponential_backoff_release_time_); | 131 return std::max(ImplGetTimeNow(), exponential_backoff_release_time_); |
| 130 } | 132 } |
| 131 | 133 |
| 132 // The delay is calculated with this formula: | 134 // The delay is calculated with this formula: |
| 133 // delay = initial_backoff * multiply_factor^( | 135 // delay = initial_backoff * multiply_factor^( |
| 134 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] | 136 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] |
| 135 double delay = policy_->initial_delay_ms; | 137 // Note: if the failure count is too high, |delay_ms| will become infinity |
| 136 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); | 138 // after the exponential calculation, and then NaN after the jitter is |
| 137 delay -= base::RandDouble() * policy_->jitter_factor * delay; | 139 // accounted for. Both cases are handled by using CheckedNumeric<int64> to |
| 140 // perform the conversion to integers. |
| 141 double delay_ms = policy_->initial_delay_ms; |
| 142 delay_ms *= pow(policy_->multiply_factor, effective_failure_count - 1); |
| 143 delay_ms -= base::RandDouble() * policy_->jitter_factor * delay_ms; |
| 138 | 144 |
| 139 const int64 kMaxInt64 = std::numeric_limits<int64>::max(); | 145 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
| 140 int64 delay_int = (delay > kMaxInt64) ? | 146 const int64 kTimeTicksNowUs = |
| 141 kMaxInt64 : static_cast<int64>(delay + 0.5); | 147 (ImplGetTimeNow() - base::TimeTicks()).InMicroseconds(); |
| 148 base::internal::CheckedNumeric<int64> calculated_release_time_us = |
| 149 delay_ms + 0.5; |
| 150 calculated_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
| 151 calculated_release_time_us += kTimeTicksNowUs; |
| 142 | 152 |
| 143 // Ensure that we do not exceed maximum delay. | 153 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; |
| 144 if (policy_->maximum_backoff_ms >= 0) | 154 if (policy_->maximum_backoff_ms >= 0) { |
| 145 delay_int = std::min(delay_int, policy_->maximum_backoff_ms); | 155 maximum_release_time_us = policy_->maximum_backoff_ms; |
| 156 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
| 157 maximum_release_time_us += kTimeTicksNowUs; |
| 158 } |
| 159 |
| 160 // Decide between maximum release time and calculated release time, accounting |
| 161 // for overflow with both. |
| 162 int64 release_time_us = std::min( |
| 163 calculated_release_time_us.ValueOrDefault(kint64max), |
| 164 maximum_release_time_us.ValueOrDefault(kint64max)); |
| 146 | 165 |
| 147 // Never reduce previously set release horizon, e.g. due to Retry-After | 166 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 148 // header. | 167 // header. |
| 149 return std::max( | 168 return std::max( |
| 150 ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), | 169 base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us), |
| 151 exponential_backoff_release_time_); | 170 exponential_backoff_release_time_); |
| 152 } | 171 } |
| 153 | 172 |
| 154 } // namespace net | 173 } // namespace net |
| OLD | NEW |