OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/backoff_entry.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <cmath> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/rand_util.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 BackoffEntry::BackoffEntry(const BackoffEntry::Policy* policy) |
| 16 : failure_count_(0), |
| 17 policy_(policy) { |
| 18 DCHECK(policy_); |
| 19 |
| 20 // Can't use GetTimeNow() as it's virtual. |
| 21 exponential_backoff_release_time_ = base::TimeTicks::Now(); |
| 22 } |
| 23 |
| 24 BackoffEntry::~BackoffEntry() { |
| 25 // TODO(joi): Remove this once our clients (e.g. URLRequestThrottlerManager) |
| 26 // are no longer singletons, destroyed from main thread AtExit. |
| 27 DetachFromThread(); |
| 28 } |
| 29 |
| 30 void BackoffEntry::InformOfRequest(bool succeeded) { |
| 31 if (!succeeded) { |
| 32 failure_count_++; |
| 33 exponential_backoff_release_time_ = CalculateReleaseTime(); |
| 34 } else { |
| 35 failure_count_ = 0; |
| 36 |
| 37 // The reason why we are not just cutting the release time to GetTimeNow() |
| 38 // is on the one hand, it would unset delay put by our custom retry-after |
| 39 // header and on the other we would like to push every request up to our |
| 40 // "horizon" when dealing with multiple in-flight requests. Ex: If we send |
| 41 // three requests and we receive 2 failures and 1 success. The success that |
| 42 // follows those failures will not reset the release time, further requests |
| 43 // will then need to wait the delay caused by the 2 failures. |
| 44 exponential_backoff_release_time_ = std::max( |
| 45 GetTimeNow(), exponential_backoff_release_time_); |
| 46 } |
| 47 } |
| 48 |
| 49 bool BackoffEntry::ShouldRejectRequest() const { |
| 50 return exponential_backoff_release_time_ > GetTimeNow(); |
| 51 } |
| 52 |
| 53 base::TimeTicks BackoffEntry::GetReleaseTime() const { |
| 54 return exponential_backoff_release_time_; |
| 55 } |
| 56 |
| 57 void BackoffEntry::SetCustomReleaseTime(const base::TimeTicks release_time) { |
| 58 exponential_backoff_release_time_ = release_time; |
| 59 } |
| 60 |
| 61 bool BackoffEntry::CanDiscard() const { |
| 62 if (policy_->entry_lifetime_ms_ == -1) |
| 63 return false; |
| 64 |
| 65 base::TimeTicks now = GetTimeNow(); |
| 66 |
| 67 int64 unused_since_ms = |
| 68 (now - exponential_backoff_release_time_).InMilliseconds(); |
| 69 |
| 70 // Release time is further than now, we are managing it. |
| 71 if (unused_since_ms < 0) |
| 72 return false; |
| 73 |
| 74 if (failure_count_ > 0) { |
| 75 // Need to keep track of failures until maximum back-off period |
| 76 // has passed (since further failures can add to back-off). |
| 77 return unused_since_ms >= std::max(policy_->maximum_backoff_ms_, |
| 78 policy_->entry_lifetime_ms_); |
| 79 } |
| 80 |
| 81 // Otherwise, consider the entry is outdated if it hasn't been used for the |
| 82 // specified lifetime period. |
| 83 return unused_since_ms >= policy_->entry_lifetime_ms_; |
| 84 } |
| 85 |
| 86 base::TimeTicks BackoffEntry::GetTimeNow() const { |
| 87 return base::TimeTicks::Now(); |
| 88 } |
| 89 |
| 90 base::TimeTicks BackoffEntry::CalculateReleaseTime() const { |
| 91 int effective_failure_count = |
| 92 std::max(0, failure_count_ - policy_->num_errors_to_ignore_); |
| 93 if (effective_failure_count == 0) { |
| 94 return GetTimeNow(); |
| 95 } |
| 96 |
| 97 double delay = policy_->initial_backoff_ms_; |
| 98 delay *= pow(policy_->multiply_factor_, effective_failure_count - 1); |
| 99 delay -= base::RandDouble() * policy_->jitter_factor_ * delay; |
| 100 |
| 101 // Ensure that we do not exceed maximum delay. |
| 102 int64 delay_int = static_cast<int64>(delay + 0.5); |
| 103 delay_int = std::min(delay_int, |
| 104 static_cast<int64>(policy_->maximum_backoff_ms_)); |
| 105 |
| 106 // Never reduce previously set release horizon, e.g. due to Retry-After |
| 107 // header. |
| 108 return std::max(GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int), |
| 109 exponential_backoff_release_time_); |
| 110 } |
| 111 |
| 112 } // namespace net |
OLD | NEW |