#include "extensions/renderer/extension_throttle_entry.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
namespace extensions {
const int ExtensionThrottleEntry::kDefaultSlidingWindowPeriodMs = 2000;
const int ExtensionThrottleEntry::kDefaultMaxSendThreshold = 20;
const int ExtensionThrottleEntry::kDefaultNumErrorsToIgnore = 2;
const int ExtensionThrottleEntry::kDefaultInitialDelayMs = 700;
const double ExtensionThrottleEntry::kDefaultMultiplyFactor = 1.4;
const double ExtensionThrottleEntry::kDefaultJitterFactor = 0.4;
const int ExtensionThrottleEntry::kDefaultMaximumBackoffMs = 15 * 60 * 1000;
const int ExtensionThrottleEntry::kDefaultEntryLifetimeMs = 2 * 60 * 1000;
ExtensionThrottleEntry::ExtensionThrottleEntry(const std::string& url_id)
: sliding_window_period_(base::Milliseconds(kDefaultSlidingWindowPeriodMs)),
max_send_threshold_(kDefaultMaxSendThreshold),
is_backoff_disabled_(false),
backoff_entry_(&backoff_policy_),
url_id_(url_id) {
Initialize();
}
ExtensionThrottleEntry::ExtensionThrottleEntry(
const std::string& url_id,
const net::BackoffEntry::Policy* backoff_policy)
: sliding_window_period_(base::Milliseconds(kDefaultSlidingWindowPeriodMs)),
max_send_threshold_(kDefaultMaxSendThreshold),
is_backoff_disabled_(false),
backoff_entry_(&backoff_policy_),
url_id_(url_id) {
DCHECK_GE(backoff_policy->initial_delay_ms, 0);
DCHECK_GT(backoff_policy->multiply_factor, 0);
DCHECK_GE(backoff_policy->jitter_factor, 0.0);
DCHECK_LT(backoff_policy->jitter_factor, 1.0);
DCHECK_GE(backoff_policy->maximum_backoff_ms, 0);
Initialize();
backoff_policy_ = *backoff_policy;
}
bool ExtensionThrottleEntry::IsEntryOutdated() const {
if (!send_log_.empty() &&
send_log_.back() + sliding_window_period_ > ImplGetTimeNow()) {
return false;
}
return GetBackoffEntry()->CanDiscard();
}
void ExtensionThrottleEntry::DisableBackoffThrottling() {
is_backoff_disabled_ = true;
}
bool ExtensionThrottleEntry::ShouldRejectRequest() const {
if (is_backoff_disabled_)
return false;
return GetBackoffEntry()->ShouldRejectRequest();
}
int64_t ExtensionThrottleEntry::ReserveSendingTimeForNextRequest(
const base::TimeTicks& earliest_time) {
base::TimeTicks now = ImplGetTimeNow();
base::TimeTicks recommended_sending_time =
std::max(std::max(now, earliest_time),
std::max(GetBackoffEntry()->GetReleaseTime(),
sliding_window_release_time_));
DCHECK(send_log_.empty() || recommended_sending_time >= send_log_.back());
send_log_.push(recommended_sending_time);
sliding_window_release_time_ = recommended_sending_time;
while ((send_log_.front() + sliding_window_period_ <=
sliding_window_release_time_) ||
send_log_.size() > static_cast<unsigned>(max_send_threshold_)) {
send_log_.pop();
}
if (send_log_.size() == static_cast<unsigned>(max_send_threshold_))
sliding_window_release_time_ = send_log_.front() + sliding_window_period_;
return (recommended_sending_time - now).InMillisecondsRoundedUp();
}
base::TimeTicks ExtensionThrottleEntry::GetExponentialBackoffReleaseTime()
const {
if (is_backoff_disabled_)
return ImplGetTimeNow();
return GetBackoffEntry()->GetReleaseTime();
}
void ExtensionThrottleEntry::UpdateWithResponse(int status_code) {
GetBackoffEntry()->InformOfRequest(IsConsideredSuccess(status_code));
}
void ExtensionThrottleEntry::ReceivedContentWasMalformed(int response_code) {
if (IsConsideredSuccess(response_code)) {
GetBackoffEntry()->InformOfRequest(false);
GetBackoffEntry()->InformOfRequest(false);
}
}
const std::string& ExtensionThrottleEntry::GetURLIdForDebugging() const {
return url_id_;
}
ExtensionThrottleEntry::~ExtensionThrottleEntry() = default;
void ExtensionThrottleEntry::Initialize() {
sliding_window_release_time_ = base::TimeTicks::Now();
backoff_policy_.num_errors_to_ignore = kDefaultNumErrorsToIgnore;
backoff_policy_.initial_delay_ms = kDefaultInitialDelayMs;
backoff_policy_.multiply_factor = kDefaultMultiplyFactor;
backoff_policy_.jitter_factor = kDefaultJitterFactor;
backoff_policy_.maximum_backoff_ms = kDefaultMaximumBackoffMs;
backoff_policy_.entry_lifetime_ms = kDefaultEntryLifetimeMs;
backoff_policy_.always_use_initial_delay = false;
}
bool ExtensionThrottleEntry::IsConsideredSuccess(int response_code) {
return !(response_code == 500 || response_code == 503 ||
response_code == 509);
}
base::TimeTicks ExtensionThrottleEntry::ImplGetTimeNow() const {
return base::TimeTicks::Now();
}
const net::BackoffEntry* ExtensionThrottleEntry::GetBackoffEntry() const {
return &backoff_entry_;
}
net::BackoffEntry* ExtensionThrottleEntry::GetBackoffEntry() {
return &backoff_entry_;
}
}