Commit 9bfc98fe authored by tby's avatar tby Committed by Commit Bot

[Suggested files] Rate-limit calls to the backend

The cooldown between successive calls is set to 15 minutes by default,
but is controlled via an experiment parameter if we need to change it
later.

Bug: 1034842
Change-Id: Ief80a68db10bf67b67c55d679555e6d55b49fab1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2434072
Commit-Queue: Tony Yeoman <tby@chromium.org>
Reviewed-by: default avatarThanh Nguyen <thanhdng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811127}
parent 55238866
......@@ -64,7 +64,6 @@ void DriveZeroStateProvider::OnFileSystemMounted() {
void DriveZeroStateProvider::AppListShown() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
item_suggest_cache_.UpdateCache();
// TODO(crbug.com/1034842): Query ItemSuggest, consider rate-limiting.
}
ash::AppListSearchResultType DriveZeroStateProvider::ResultType() {
......
......@@ -178,6 +178,7 @@ const base::Feature ItemSuggestCache::kExperiment{
"LauncherItemSuggest", base::FEATURE_DISABLED_BY_DEFAULT};
constexpr base::FeatureParam<bool> ItemSuggestCache::kEnabled;
constexpr base::FeatureParam<std::string> ItemSuggestCache::kServerUrl;
constexpr base::FeatureParam<int> ItemSuggestCache::kMinMinutesBetweenUpdates;
ItemSuggestCache::Result::Result(const std::string& id,
const std::string& title)
......@@ -201,6 +202,8 @@ ItemSuggestCache::ItemSuggestCache(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: enabled_(kEnabled.Get()),
server_url_(kServerUrl.Get()),
min_time_between_updates_(
base::TimeDelta::FromMinutes(kMinMinutesBetweenUpdates.Get())),
profile_(profile),
url_loader_factory_(std::move(url_loader_factory)) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......@@ -216,7 +219,11 @@ base::Optional<ItemSuggestCache::Results> ItemSuggestCache::GetResults() {
void ItemSuggestCache::UpdateCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(crbug.com/1034842): Add rate-limiting for cache updates.
const auto& now = base::Time::Now();
if (now - time_of_last_update_ < min_time_between_updates_)
return;
time_of_last_update_ = now;
// Make no requests and exit in these cases:
// - another request is in-flight (url_loader_ is non-null)
......
......@@ -11,6 +11,7 @@
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
......@@ -80,6 +81,9 @@ class ItemSuggestCache {
&kExperiment, "server_url",
"https://appsitemsuggest-pa.googleapis.com/v1/items"};
static constexpr base::FeatureParam<int> kMinMinutesBetweenUpdates{
&kExperiment, "min_minutes_between_updates", 15};
void OnTokenReceived(GoogleServiceAuthError error,
signin::AccessTokenInfo token_info);
void OnSuggestionsReceived(const std::unique_ptr<std::string> json_response);
......@@ -89,8 +93,13 @@ class ItemSuggestCache {
base::Optional<Results> results_;
// Records the time of the last call to UpdateResults(), used to limit the
// number of queries to the ItemSuggest backend.
base::Time time_of_last_update_;
const bool enabled_;
const GURL server_url_;
const base::TimeDelta min_time_between_updates_;
Profile* profile_;
std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher> token_fetcher_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment