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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // time set by SetCustomReleaseTime and on the other we would like | 43 // time set by SetCustomReleaseTime and on the other we would like |
44 // to push every request up to our "horizon" when dealing with | 44 // to push every request up to our "horizon" when dealing with |
45 // multiple in-flight requests. Ex: If we send three requests and | 45 // multiple in-flight requests. Ex: If we send three requests and |
46 // we receive 2 failures and 1 success. The success that follows | 46 // we receive 2 failures and 1 success. The success that follows |
47 // those failures will not reset the release time, further | 47 // those failures will not reset the release time, further |
48 // requests will then need to wait the delay caused by the 2 | 48 // requests will then need to wait the delay caused by the 2 |
49 // failures. | 49 // failures. |
50 base::TimeDelta delay; | 50 base::TimeDelta delay; |
51 if (policy_->always_use_initial_delay) | 51 if (policy_->always_use_initial_delay) |
52 delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms); | 52 delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms); |
53 exponential_backoff_release_time_ = std::max( | 53 exponential_backoff_release_time_ = |
54 ImplGetTimeNow() + delay, exponential_backoff_release_time_); | 54 std::max(ImplGetTimeNow() + delay, exponential_backoff_release_time_); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 bool BackoffEntry::ShouldRejectRequest() const { | 58 bool BackoffEntry::ShouldRejectRequest() const { |
59 return exponential_backoff_release_time_ > ImplGetTimeNow(); | 59 return exponential_backoff_release_time_ > ImplGetTimeNow(); |
60 } | 60 } |
61 | 61 |
62 base::TimeDelta BackoffEntry::GetTimeUntilRelease() const { | 62 base::TimeDelta BackoffEntry::GetTimeUntilRelease() const { |
63 base::TimeTicks now = ImplGetTimeNow(); | 63 base::TimeTicks now = ImplGetTimeNow(); |
64 if (exponential_backoff_release_time_ <= now) | 64 if (exponential_backoff_release_time_ <= now) |
(...skipping 18 matching lines...) Expand all Loading... |
83 int64 unused_since_ms = | 83 int64 unused_since_ms = |
84 (now - exponential_backoff_release_time_).InMilliseconds(); | 84 (now - exponential_backoff_release_time_).InMilliseconds(); |
85 | 85 |
86 // Release time is further than now, we are managing it. | 86 // Release time is further than now, we are managing it. |
87 if (unused_since_ms < 0) | 87 if (unused_since_ms < 0) |
88 return false; | 88 return false; |
89 | 89 |
90 if (failure_count_ > 0) { | 90 if (failure_count_ > 0) { |
91 // Need to keep track of failures until maximum back-off period | 91 // Need to keep track of failures until maximum back-off period |
92 // has passed (since further failures can add to back-off). | 92 // has passed (since further failures can add to back-off). |
93 return unused_since_ms >= std::max(policy_->maximum_backoff_ms, | 93 return unused_since_ms >= |
94 policy_->entry_lifetime_ms); | 94 std::max(policy_->maximum_backoff_ms, policy_->entry_lifetime_ms); |
95 } | 95 } |
96 | 96 |
97 // Otherwise, consider the entry is outdated if it hasn't been used for the | 97 // Otherwise, consider the entry is outdated if it hasn't been used for the |
98 // specified lifetime period. | 98 // specified lifetime period. |
99 return unused_since_ms >= policy_->entry_lifetime_ms; | 99 return unused_since_ms >= policy_->entry_lifetime_ms; |
100 } | 100 } |
101 | 101 |
102 void BackoffEntry::Reset() { | 102 void BackoffEntry::Reset() { |
103 failure_count_ = 0; | 103 failure_count_ = 0; |
104 | 104 |
(...skipping 25 matching lines...) Expand all Loading... |
130 } | 130 } |
131 | 131 |
132 // The delay is calculated with this formula: | 132 // The delay is calculated with this formula: |
133 // delay = initial_backoff * multiply_factor^( | 133 // delay = initial_backoff * multiply_factor^( |
134 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] | 134 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] |
135 double delay = policy_->initial_delay_ms; | 135 double delay = policy_->initial_delay_ms; |
136 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); | 136 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); |
137 delay -= base::RandDouble() * policy_->jitter_factor * delay; | 137 delay -= base::RandDouble() * policy_->jitter_factor * delay; |
138 | 138 |
139 const int64 kMaxInt64 = std::numeric_limits<int64>::max(); | 139 const int64 kMaxInt64 = std::numeric_limits<int64>::max(); |
140 int64 delay_int = (delay > kMaxInt64) ? | 140 int64 delay_int = |
141 kMaxInt64 : static_cast<int64>(delay + 0.5); | 141 (delay > kMaxInt64) ? kMaxInt64 : static_cast<int64>(delay + 0.5); |
142 | 142 |
143 // Ensure that we do not exceed maximum delay. | 143 // Ensure that we do not exceed maximum delay. |
144 if (policy_->maximum_backoff_ms >= 0) | 144 if (policy_->maximum_backoff_ms >= 0) |
145 delay_int = std::min(delay_int, policy_->maximum_backoff_ms); | 145 delay_int = std::min(delay_int, policy_->maximum_backoff_ms); |
146 | 146 |
147 // Never reduce previously set release horizon, e.g. due to Retry-After | 147 // Never reduce previously set release horizon, e.g. due to Retry-After |
148 // header. | 148 // header. |
149 return std::max( | 149 return std::max( |
150 ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), | 150 ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), |
151 exponential_backoff_release_time_); | 151 exponential_backoff_release_time_); |
152 } | 152 } |
153 | 153 |
154 } // namespace net | 154 } // namespace net |
OLD | NEW |