Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 } | 22 } |
| 23 | 23 |
| 24 BackoffEntry::~BackoffEntry() { | 24 BackoffEntry::~BackoffEntry() { |
| 25 // TODO(joi): Remove this once our clients (e.g. URLRequestThrottlerManager) | 25 // TODO(joi): Remove this once our clients (e.g. URLRequestThrottlerManager) |
| 26 // always destroy from the I/O thread. | 26 // always destroy from the I/O thread. |
| 27 DetachFromThread(); | 27 DetachFromThread(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void BackoffEntry::InformOfRequest(bool succeeded) { | 30 void BackoffEntry::InformOfRequest(bool succeeded) { |
| 31 if (!succeeded) { | 31 if (!succeeded) { |
| 32 failure_count_++; | 32 ++failure_count_; |
| 33 exponential_backoff_release_time_ = CalculateReleaseTime(); | |
| 34 } else { | 33 } else { |
| 35 failure_count_ = 0; | 34 failure_count_ = std::max(0, --failure_count_); |
| 35 } | |
| 36 | 36 |
| 37 // The reason why we are not just cutting the release time to GetTimeNow() | 37 exponential_backoff_release_time_ = CalculateReleaseTime(); |
|
sanjeevr
2011/05/04 18:51:50
I don't know the code well enough to tell if calli
Jói
2011/05/04 20:04:29
Nope, it's a small bug to call it in the success c
| |
| 38 // is on the one hand, it would unset a release time set by | |
| 39 // SetCustomReleaseTime and on the other we would like to push every | |
| 40 // request up to our "horizon" when dealing with multiple in-flight | |
| 41 // requests. Ex: If we send three requests and we receive 2 failures and | |
| 42 // 1 success. The success that follows those failures will not reset the | |
| 43 // release time, further requests will then need to wait the delay caused | |
| 44 // by the 2 failures. | |
| 45 exponential_backoff_release_time_ = std::max( | |
| 46 GetTimeNow(), exponential_backoff_release_time_); | |
| 47 } | |
| 48 } | 38 } |
| 49 | 39 |
| 50 bool BackoffEntry::ShouldRejectRequest() const { | 40 bool BackoffEntry::ShouldRejectRequest() const { |
| 51 return exponential_backoff_release_time_ > GetTimeNow(); | 41 return exponential_backoff_release_time_ > GetTimeNow(); |
| 52 } | 42 } |
| 53 | 43 |
| 54 base::TimeTicks BackoffEntry::GetReleaseTime() const { | 44 base::TimeTicks BackoffEntry::GetReleaseTime() const { |
| 55 return exponential_backoff_release_time_; | 45 return exponential_backoff_release_time_; |
| 56 } | 46 } |
| 57 | 47 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 double delay = policy_->initial_backoff_ms; | 93 double delay = policy_->initial_backoff_ms; |
| 104 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); | 94 delay *= pow(policy_->multiply_factor, effective_failure_count - 1); |
| 105 delay -= base::RandDouble() * policy_->jitter_factor * delay; | 95 delay -= base::RandDouble() * policy_->jitter_factor * delay; |
| 106 | 96 |
| 107 // Ensure that we do not exceed maximum delay. | 97 // Ensure that we do not exceed maximum delay. |
| 108 int64 delay_int = static_cast<int64>(delay + 0.5); | 98 int64 delay_int = static_cast<int64>(delay + 0.5); |
| 109 delay_int = std::min(delay_int, | 99 delay_int = std::min(delay_int, |
| 110 static_cast<int64>(policy_->maximum_backoff_ms)); | 100 static_cast<int64>(policy_->maximum_backoff_ms)); |
| 111 | 101 |
| 112 // Never reduce previously set release horizon, e.g. due to Retry-After | 102 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 113 // header. | 103 // header. To clarify, the delay we are adding may have been reduced (if |
| 104 // the failure count was decreased from its previous value) but the absolute | |
| 105 // time of the release horizon should not be moved forward. | |
| 114 return std::max(GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), | 106 return std::max(GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), |
| 115 exponential_backoff_release_time_); | 107 exponential_backoff_release_time_); |
| 116 } | 108 } |
| 117 | 109 |
| 118 } // namespace net | 110 } // namespace net |
| OLD | NEW |