| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // time set by SetCustomReleaseTime and on the other we would like | 45 // time set by SetCustomReleaseTime and on the other we would like |
| 46 // to push every request up to our "horizon" when dealing with | 46 // to push every request up to our "horizon" when dealing with |
| 47 // multiple in-flight requests. Ex: If we send three requests and | 47 // multiple in-flight requests. Ex: If we send three requests and |
| 48 // we receive 2 failures and 1 success. The success that follows | 48 // we receive 2 failures and 1 success. The success that follows |
| 49 // those failures will not reset the release time, further | 49 // those failures will not reset the release time, further |
| 50 // requests will then need to wait the delay caused by the 2 | 50 // requests will then need to wait the delay caused by the 2 |
| 51 // failures. | 51 // failures. |
| 52 base::TimeDelta delay; | 52 base::TimeDelta delay; |
| 53 if (policy_->always_use_initial_delay) | 53 if (policy_->always_use_initial_delay) |
| 54 delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms); | 54 delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms); |
| 55 exponential_backoff_release_time_ = std::max( | 55 exponential_backoff_release_time_ = |
| 56 ImplGetTimeNow() + delay, exponential_backoff_release_time_); | 56 std::max(ImplGetTimeNow() + delay, exponential_backoff_release_time_); |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool BackoffEntry::ShouldRejectRequest() const { | 60 bool BackoffEntry::ShouldRejectRequest() const { |
| 61 return exponential_backoff_release_time_ > ImplGetTimeNow(); | 61 return exponential_backoff_release_time_ > ImplGetTimeNow(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 base::TimeDelta BackoffEntry::GetTimeUntilRelease() const { | 64 base::TimeDelta BackoffEntry::GetTimeUntilRelease() const { |
| 65 base::TimeTicks now = ImplGetTimeNow(); | 65 base::TimeTicks now = ImplGetTimeNow(); |
| 66 if (exponential_backoff_release_time_ <= now) | 66 if (exponential_backoff_release_time_ <= now) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 85 int64 unused_since_ms = | 85 int64 unused_since_ms = |
| 86 (now - exponential_backoff_release_time_).InMilliseconds(); | 86 (now - exponential_backoff_release_time_).InMilliseconds(); |
| 87 | 87 |
| 88 // Release time is further than now, we are managing it. | 88 // Release time is further than now, we are managing it. |
| 89 if (unused_since_ms < 0) | 89 if (unused_since_ms < 0) |
| 90 return false; | 90 return false; |
| 91 | 91 |
| 92 if (failure_count_ > 0) { | 92 if (failure_count_ > 0) { |
| 93 // Need to keep track of failures until maximum back-off period | 93 // Need to keep track of failures until maximum back-off period |
| 94 // has passed (since further failures can add to back-off). | 94 // has passed (since further failures can add to back-off). |
| 95 return unused_since_ms >= std::max(policy_->maximum_backoff_ms, | 95 return unused_since_ms >= |
| 96 policy_->entry_lifetime_ms); | 96 std::max(policy_->maximum_backoff_ms, policy_->entry_lifetime_ms); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Otherwise, consider the entry is outdated if it hasn't been used for the | 99 // Otherwise, consider the entry is outdated if it hasn't been used for the |
| 100 // specified lifetime period. | 100 // specified lifetime period. |
| 101 return unused_since_ms >= policy_->entry_lifetime_ms; | 101 return unused_since_ms >= policy_->entry_lifetime_ms; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void BackoffEntry::Reset() { | 104 void BackoffEntry::Reset() { |
| 105 failure_count_ = 0; | 105 failure_count_ = 0; |
| 106 | 106 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; | 153 base::internal::CheckedNumeric<int64> maximum_release_time_us = kint64max; |
| 154 if (policy_->maximum_backoff_ms >= 0) { | 154 if (policy_->maximum_backoff_ms >= 0) { |
| 155 maximum_release_time_us = policy_->maximum_backoff_ms; | 155 maximum_release_time_us = policy_->maximum_backoff_ms; |
| 156 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; | 156 maximum_release_time_us *= base::Time::kMicrosecondsPerMillisecond; |
| 157 maximum_release_time_us += kTimeTicksNowUs; | 157 maximum_release_time_us += kTimeTicksNowUs; |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Decide between maximum release time and calculated release time, accounting | 160 // Decide between maximum release time and calculated release time, accounting |
| 161 // for overflow with both. | 161 // for overflow with both. |
| 162 int64 release_time_us = std::min( | 162 int64 release_time_us = |
| 163 calculated_release_time_us.ValueOrDefault(kint64max), | 163 std::min(calculated_release_time_us.ValueOrDefault(kint64max), |
| 164 maximum_release_time_us.ValueOrDefault(kint64max)); | 164 maximum_release_time_us.ValueOrDefault(kint64max)); |
| 165 | 165 |
| 166 // 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 |
| 167 // header. | 167 // header. |
| 168 return std::max( | 168 return std::max( |
| 169 base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us), | 169 base::TimeTicks() + base::TimeDelta::FromMicroseconds(release_time_us), |
| 170 exponential_backoff_release_time_); | 170 exponential_backoff_release_time_); |
| 171 } | 171 } |
| 172 | 172 |
| 173 } // namespace net | 173 } // namespace net |
| OLD | NEW |