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 29 matching lines...) Expand all Loading... |
40 | 40 |
41 // The reason why we are not just cutting the release time to | 41 // The reason why we are not just cutting the release time to |
42 // ImplGetTimeNow() is on the one hand, it would unset a release | 42 // ImplGetTimeNow() is on the one hand, it would unset a release |
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; |
| 51 if (policy_->always_use_initial_delay) |
| 52 delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms); |
50 exponential_backoff_release_time_ = std::max( | 53 exponential_backoff_release_time_ = std::max( |
51 ImplGetTimeNow(), exponential_backoff_release_time_); | 54 ImplGetTimeNow() + delay, exponential_backoff_release_time_); |
52 } | 55 } |
53 } | 56 } |
54 | 57 |
55 bool BackoffEntry::ShouldRejectRequest() const { | 58 bool BackoffEntry::ShouldRejectRequest() const { |
56 return exponential_backoff_release_time_ > ImplGetTimeNow(); | 59 return exponential_backoff_release_time_ > ImplGetTimeNow(); |
57 } | 60 } |
58 | 61 |
| 62 base::TimeDelta BackoffEntry::GetTimeUntilRelease() const { |
| 63 base::TimeTicks now = ImplGetTimeNow(); |
| 64 if (exponential_backoff_release_time_ <= now) |
| 65 return base::TimeDelta(); |
| 66 return exponential_backoff_release_time_ - now; |
| 67 } |
| 68 |
59 base::TimeTicks BackoffEntry::GetReleaseTime() const { | 69 base::TimeTicks BackoffEntry::GetReleaseTime() const { |
60 return exponential_backoff_release_time_; | 70 return exponential_backoff_release_time_; |
61 } | 71 } |
62 | 72 |
63 void BackoffEntry::SetCustomReleaseTime(const base::TimeTicks& release_time) { | 73 void BackoffEntry::SetCustomReleaseTime(const base::TimeTicks& release_time) { |
64 exponential_backoff_release_time_ = release_time; | 74 exponential_backoff_release_time_ = release_time; |
65 } | 75 } |
66 | 76 |
67 bool BackoffEntry::CanDiscard() const { | 77 bool BackoffEntry::CanDiscard() const { |
68 if (policy_->entry_lifetime_ms == -1) | 78 if (policy_->entry_lifetime_ms == -1) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 exponential_backoff_release_time_ = base::TimeTicks(); | 110 exponential_backoff_release_time_ = base::TimeTicks(); |
101 } | 111 } |
102 | 112 |
103 base::TimeTicks BackoffEntry::ImplGetTimeNow() const { | 113 base::TimeTicks BackoffEntry::ImplGetTimeNow() const { |
104 return base::TimeTicks::Now(); | 114 return base::TimeTicks::Now(); |
105 } | 115 } |
106 | 116 |
107 base::TimeTicks BackoffEntry::CalculateReleaseTime() const { | 117 base::TimeTicks BackoffEntry::CalculateReleaseTime() const { |
108 int effective_failure_count = | 118 int effective_failure_count = |
109 std::max(0, failure_count_ - policy_->num_errors_to_ignore); | 119 std::max(0, failure_count_ - policy_->num_errors_to_ignore); |
| 120 |
| 121 // If always_use_initial_delay is true, it's equivalent to |
| 122 // the effective_failure_count always being one greater than when it's false. |
| 123 if (policy_->always_use_initial_delay) |
| 124 ++effective_failure_count; |
| 125 |
110 if (effective_failure_count == 0) { | 126 if (effective_failure_count == 0) { |
111 // Never reduce previously set release horizon, e.g. due to Retry-After | 127 // Never reduce previously set release horizon, e.g. due to Retry-After |
112 // header. | 128 // header. |
113 return std::max(ImplGetTimeNow(), exponential_backoff_release_time_); | 129 return std::max(ImplGetTimeNow(), exponential_backoff_release_time_); |
114 } | 130 } |
115 | 131 |
116 // The delay is calculated with this formula: | 132 // The delay is calculated with this formula: |
117 // delay = initial_backoff * multiply_factor^( | 133 // delay = initial_backoff * multiply_factor^( |
118 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] | 134 // effective_failure_count - 1) * Uniform(1 - jitter_factor, 1] |
119 double delay = policy_->initial_backoff_ms; | 135 double delay = policy_->initial_delay_ms; |
120 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); | 136 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); |
121 delay -= base::RandDouble() * policy_->jitter_factor * delay; | 137 delay -= base::RandDouble() * policy_->jitter_factor * delay; |
122 | 138 |
123 const int64 kMaxInt64 = std::numeric_limits<int64>::max(); | 139 const int64 kMaxInt64 = std::numeric_limits<int64>::max(); |
124 int64 delay_int = (delay > kMaxInt64) ? | 140 int64 delay_int = (delay > kMaxInt64) ? |
125 kMaxInt64 : static_cast<int64>(delay + 0.5); | 141 kMaxInt64 : static_cast<int64>(delay + 0.5); |
126 | 142 |
127 // Ensure that we do not exceed maximum delay. | 143 // Ensure that we do not exceed maximum delay. |
128 if (policy_->maximum_backoff_ms >= 0) | 144 if (policy_->maximum_backoff_ms >= 0) |
129 delay_int = std::min(delay_int, policy_->maximum_backoff_ms); | 145 delay_int = std::min(delay_int, policy_->maximum_backoff_ms); |
130 | 146 |
131 // 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 |
132 // header. | 148 // header. |
133 return std::max( | 149 return std::max( |
134 ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), | 150 ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), |
135 exponential_backoff_release_time_); | 151 exponential_backoff_release_time_); |
136 } | 152 } |
137 | 153 |
138 } // namespace net | 154 } // namespace net |
OLD | NEW |