This source file includes following definitions.
- InformOfRequest
- ShouldRejectRequest
- GetTimeUntilRelease
- GetReleaseTime
- SetCustomReleaseTime
- CanDiscard
- Reset
- ImplGetTimeNow
- CalculateReleaseTime
#include "net/base/backoff_entry.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include "base/logging.h"
#include "base/rand_util.h"
namespace net {
BackoffEntry::BackoffEntry(const BackoffEntry::Policy* const policy)
: policy_(policy) {
DCHECK(policy_);
Reset();
}
BackoffEntry::~BackoffEntry() {
DetachFromThread();
}
void BackoffEntry::InformOfRequest(bool succeeded) {
if (!succeeded) {
++failure_count_;
exponential_backoff_release_time_ = CalculateReleaseTime();
} else {
if (failure_count_ > 0)
--failure_count_;
base::TimeDelta delay;
if (policy_->always_use_initial_delay)
delay = base::TimeDelta::FromMilliseconds(policy_->initial_delay_ms);
exponential_backoff_release_time_ = std::max(
ImplGetTimeNow() + delay, exponential_backoff_release_time_);
}
}
bool BackoffEntry::ShouldRejectRequest() const {
return exponential_backoff_release_time_ > ImplGetTimeNow();
}
base::TimeDelta BackoffEntry::GetTimeUntilRelease() const {
base::TimeTicks now = ImplGetTimeNow();
if (exponential_backoff_release_time_ <= now)
return base::TimeDelta();
return exponential_backoff_release_time_ - now;
}
base::TimeTicks BackoffEntry::GetReleaseTime() const {
return exponential_backoff_release_time_;
}
void BackoffEntry::SetCustomReleaseTime(const base::TimeTicks& release_time) {
exponential_backoff_release_time_ = release_time;
}
bool BackoffEntry::CanDiscard() const {
if (policy_->entry_lifetime_ms == -1)
return false;
base::TimeTicks now = ImplGetTimeNow();
int64 unused_since_ms =
(now - exponential_backoff_release_time_).InMilliseconds();
if (unused_since_ms < 0)
return false;
if (failure_count_ > 0) {
return unused_since_ms >= std::max(policy_->maximum_backoff_ms,
policy_->entry_lifetime_ms);
}
return unused_since_ms >= policy_->entry_lifetime_ms;
}
void BackoffEntry::Reset() {
failure_count_ = 0;
exponential_backoff_release_time_ = base::TimeTicks();
}
base::TimeTicks BackoffEntry::ImplGetTimeNow() const {
return base::TimeTicks::Now();
}
base::TimeTicks BackoffEntry::CalculateReleaseTime() const {
int effective_failure_count =
std::max(0, failure_count_ - policy_->num_errors_to_ignore);
if (policy_->always_use_initial_delay)
++effective_failure_count;
if (effective_failure_count == 0) {
return std::max(ImplGetTimeNow(), exponential_backoff_release_time_);
}
double delay = policy_->initial_delay_ms;
delay *= pow(policy_->multiply_factor, effective_failure_count - 1);
delay -= base::RandDouble() * policy_->jitter_factor * delay;
const int64 kMaxInt64 = std::numeric_limits<int64>::max();
int64 delay_int = (delay > kMaxInt64) ?
kMaxInt64 : static_cast<int64>(delay + 0.5);
if (policy_->maximum_backoff_ms >= 0)
delay_int = std::min(delay_int, policy_->maximum_backoff_ms);
return std::max(
ImplGetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int),
exponential_backoff_release_time_);
}
}