| 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" | |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/numerics/safe_math.h" | 12 #include "base/numerics/safe_math.h" |
| 14 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 15 #include "base/time/tick_clock.h" | 14 #include "base/time/tick_clock.h" |
| 16 | 15 |
| 17 namespace net { | 16 namespace net { |
| 18 | 17 |
| 19 BackoffEntry::BackoffEntry(const BackoffEntry::Policy* policy) | 18 BackoffEntry::BackoffEntry(const BackoffEntry::Policy* policy) |
| 20 : BackoffEntry(policy, nullptr) {} | 19 : BackoffEntry(policy, nullptr) {} |
| 21 | 20 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // accounted for. Both cases are handled by using CheckedNumeric<int64_t> to | 137 // accounted for. Both cases are handled by using CheckedNumeric<int64_t> to |
| 139 // perform the conversion to integers. | 138 // perform the conversion to integers. |
| 140 double delay_ms = policy_->initial_delay_ms; | 139 double delay_ms = policy_->initial_delay_ms; |
| 141 delay_ms *= pow(policy_->multiply_factor, effective_failure_count - 1); | 140 delay_ms *= pow(policy_->multiply_factor, effective_failure_count - 1); |
| 142 delay_ms -= base::RandDouble() * policy_->jitter_factor * delay_ms; | 141 delay_ms -= base::RandDouble() * policy_->jitter_factor * delay_ms; |
| 143 | 142 |
| 144 // Do overflow checking in microseconds, the internal unit of TimeTicks. | 143 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
| 145 base::internal::CheckedNumeric<int64_t> backoff_duration_us = delay_ms + 0.5; | 144 base::internal::CheckedNumeric<int64_t> backoff_duration_us = delay_ms + 0.5; |
| 146 backoff_duration_us *= base::Time::kMicrosecondsPerMillisecond; | 145 backoff_duration_us *= base::Time::kMicrosecondsPerMillisecond; |
| 147 base::TimeDelta backoff_duration = base::TimeDelta::FromMicroseconds( | 146 base::TimeDelta backoff_duration = base::TimeDelta::FromMicroseconds( |
| 148 backoff_duration_us.ValueOrDefault(kint64max)); | 147 backoff_duration_us.ValueOrDefault(std::numeric_limits<int64_t>::max())); |
| 149 base::TimeTicks release_time = BackoffDurationToReleaseTime(backoff_duration); | 148 base::TimeTicks release_time = BackoffDurationToReleaseTime(backoff_duration); |
| 150 | 149 |
| 151 // Never reduce previously set release horizon, e.g. due to Retry-After | 150 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 152 // header. | 151 // header. |
| 153 return std::max(release_time, exponential_backoff_release_time_); | 152 return std::max(release_time, exponential_backoff_release_time_); |
| 154 } | 153 } |
| 155 | 154 |
| 156 base::TimeTicks BackoffEntry::BackoffDurationToReleaseTime( | 155 base::TimeTicks BackoffEntry::BackoffDurationToReleaseTime( |
| 157 base::TimeDelta backoff_duration) const { | 156 base::TimeDelta backoff_duration) const { |
| 158 const int64_t kTimeTicksNowUs = | 157 const int64_t kTimeTicksNowUs = |
| 159 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); | 158 (GetTimeTicksNow() - base::TimeTicks()).InMicroseconds(); |
| 160 // Do overflow checking in microseconds, the internal unit of TimeTicks. | 159 // Do overflow checking in microseconds, the internal unit of TimeTicks. |
| 161 base::internal::CheckedNumeric<int64_t> calculated_release_time_us = | 160 base::internal::CheckedNumeric<int64_t> calculated_release_time_us = |
| 162 backoff_duration.InMicroseconds(); | 161 backoff_duration.InMicroseconds(); |
| 163 calculated_release_time_us += kTimeTicksNowUs; | 162 calculated_release_time_us += kTimeTicksNowUs; |
| 164 | 163 |
| 165 base::internal::CheckedNumeric<int64_t> maximum_release_time_us = kint64max; | 164 base::internal::CheckedNumeric<int64_t> maximum_release_time_us = |
| 165 std::numeric_limits<int64_t>::max(); |
| 166 if (policy_->maximum_backoff_ms >= 0) { | 166 if (policy_->maximum_backoff_ms >= 0) { |
| 167 maximum_release_time_us = policy_->maximum_backoff_ms; | 167 maximum_release_time_us = policy_->maximum_backoff_ms; |
| 168 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | 168 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
| 169 maximum_release_time_us += kTimeTicksNowUs; | 169 maximum_release_time_us += kTimeTicksNowUs; |
| 170 } | 170 } |
| 171 | 171 |
| 172 // Decide between maximum release time and calculated release time, accounting | 172 // Decide between maximum release time and calculated release time, accounting |
| 173 // for overflow with both. | 173 // for overflow with both. |
| 174 int64_t release_time_us = | 174 int64_t release_time_us = std::min(calculated_release_time_us.ValueOrDefault( |
| 175 std::min(calculated_release_time_us.ValueOrDefault(kint64max), | 175 std::numeric_limits<int64_t>::max()), |
| 176 maximum_release_time_us.ValueOrDefault(kint64max)); | 176 maximum_release_time_us.ValueOrDefault( |
| 177 std::numeric_limits<int64_t>::max())); |
| 177 | 178 |
| 178 return base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us); | 179 return base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us); |
| 179 } | 180 } |
| 180 | 181 |
| 181 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { | 182 base::TimeTicks BackoffEntry::GetTimeTicksNow() const { |
| 182 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); | 183 return clock_ ? clock_->NowTicks() : base::TimeTicks::Now(); |
| 183 } | 184 } |
| 184 | 185 |
| 185 } // namespace net | 186 } // namespace net |
| OLD | NEW |