Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Unified Diff: components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc

Issue 2714863004: [Remote suggestions] Move the throttler into the Scheduler (Closed)
Patch Set: Ilya's comment Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
diff --git a/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc b/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
index 19b651c9e686b87e5a345e15189626fed344d033..ab9605f11efce3717aa1fa7696f084d3dd57cd6f 100644
--- a/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
+++ b/components/ntp_snippets/remote/scheduling_remote_suggestions_provider.cc
@@ -186,6 +186,18 @@ SchedulingRemoteSuggestionsProvider::SchedulingRemoteSuggestionsProvider(
persistent_scheduler_(persistent_scheduler),
background_fetch_in_progress_(false),
user_classifier_(user_classifier),
+ request_throttler_rare_ntp_user_(
+ pref_service,
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER),
+ request_throttler_active_ntp_user_(
+ pref_service,
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER),
+ request_throttler_active_suggestions_consumer_(
+ pref_service,
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER),
pref_service_(pref_service),
clock_(std::move(clock)),
enabled_triggers_(GetEnabledTriggerTypes()) {
@@ -283,6 +295,14 @@ void SchedulingRemoteSuggestionsProvider::RefetchInTheBackground(
return;
}
+ if (!AcquireQuota(/*interactive_request=*/false)) {
+ if (callback) {
+ callback->Run(Status(StatusCode::TEMPORARY_ERROR,
+ "Non-interactive quota exceeded"));
+ }
+ return;
+ }
+
background_fetch_in_progress_ = true;
RemoteSuggestionsProvider::FetchStatusCallback wrapper_callback = base::Bind(
&SchedulingRemoteSuggestionsProvider::RefetchInTheBackgroundFinished,
@@ -322,6 +342,15 @@ void SchedulingRemoteSuggestionsProvider::Fetch(
const Category& category,
const std::set<std::string>& known_suggestion_ids,
const FetchDoneCallback& callback) {
+ if (!AcquireQuota(/*interactive_request=*/true)) {
+ if (callback) {
+ callback.Run(
+ Status(StatusCode::TEMPORARY_ERROR, "Interactive quota exceeded"),
+ std::vector<ContentSuggestion>());
+ }
+ return;
+ }
+
provider_->Fetch(
category, known_suggestion_ids,
base::Bind(&SchedulingRemoteSuggestionsProvider::FetchFinished,
@@ -329,6 +358,10 @@ void SchedulingRemoteSuggestionsProvider::Fetch(
}
void SchedulingRemoteSuggestionsProvider::ReloadSuggestions() {
+ if (!AcquireQuota(/*interactive_request=*/true)) {
+ return;
+ }
+
provider_->ReloadSuggestions();
}
@@ -489,6 +522,23 @@ bool SchedulingRemoteSuggestionsProvider::BackgroundFetchesDisabled(
return false;
}
+bool SchedulingRemoteSuggestionsProvider::AcquireQuota(
+ bool interactive_request) {
+ switch (user_classifier_->GetUserClass()) {
+ case UserClassifier::UserClass::RARE_NTP_USER:
+ return request_throttler_rare_ntp_user_.DemandQuotaForRequest(
+ interactive_request);
+ case UserClassifier::UserClass::ACTIVE_NTP_USER:
+ return request_throttler_active_ntp_user_.DemandQuotaForRequest(
+ interactive_request);
+ case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
+ return request_throttler_active_suggestions_consumer_
+ .DemandQuotaForRequest(interactive_request);
+ }
+ NOTREACHED();
+ return false;
+}
+
void SchedulingRemoteSuggestionsProvider::FetchFinished(
const FetchDoneCallback& callback,
Status fetch_status,

Powered by Google App Engine
This is Rietveld 408576698