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

Side by Side Diff: components/ntp_snippets/request_throttler.cc

Issue 2227973002: Add request throttler to thumbnail fetching for articles on mobile NTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/request_throttler.h" 5 #include "components/ntp_snippets/request_throttler.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "components/ntp_snippets/ntp_snippets_constants.h" 14 #include "components/ntp_snippets/ntp_snippets_constants.h"
15 #include "components/ntp_snippets/pref_names.h" 15 #include "components/ntp_snippets/pref_names.h"
16 #include "components/prefs/pref_registry_simple.h" 16 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_associated_data.h" 18 #include "components/variations/variations_associated_data.h"
19 19
20 namespace ntp_snippets { 20 namespace ntp_snippets {
21 21
22 namespace { 22 namespace {
23 23
24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA 24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA
25 // histogram, so do not change existing values. Insert new values at the end, 25 // histogram, so do not change existing values. Insert new values at the end,
26 // and update the histogram definition. 26 // and update the histogram definition.
27 enum class RequestStatus { 27 enum class RequestStatus {
28 FORCED, 28 FORCED,
Marc Treib 2016/08/09 14:04:53 Note that you can rename the enum entry, just not
jkrcal 2016/08/10 10:27:26 Done.
29 QUOTA_GRANTED, 29 QUOTA_GRANTED,
30 QUOTA_EXCEEDED, 30 QUOTA_EXCEEDED,
31 REQUEST_STATUS_COUNT 31 REQUEST_STATUS_COUNT
32 }; 32 };
33 33
34 // Quota value to use if no quota should be applied (by default).
35 const int kUnlimitedQuota = INT_MAX;
36
34 } // namespace 37 } // namespace
35 38
36 struct RequestThrottler::RequestTypeInfo { 39 struct RequestThrottler::RequestTypeInfo {
37 const char* name; 40 const char* name;
38 const char* count_pref; 41 const char* count_pref;
42 const char* interactive_count_pref;
39 const char* day_pref; 43 const char* day_pref;
40 const int default_quota; 44 const int default_quota;
45 const int default_interactive_quota;
41 }; 46 };
42 47
43 // When adding a new type here, extend also the "RequestCounterTypes" 48 // When adding a new type here, extend also the "RequestCounterTypes"
44 // <histogram_suffixes> in histograms.xml with the |name| string. 49 // <histogram_suffixes> in histograms.xml with the |name| string.
45 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { 50 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = {
46 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, 51 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
47 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, 52 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount,
48 prefs::kSnippetFetcherQuotaDay, 50}}; 53 prefs::kSnippetFetcherInteractiveQuotaCount,
54 prefs::kSnippetFetcherQuotaDay, 50, kUnlimitedQuota},
55 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsQuotaCount,
56 prefs::kSnippetThumbnailsInteractiveQuotaCount,
57 prefs::kSnippetThumbnailsQuotaDay, kUnlimitedQuota, kUnlimitedQuota}};
49 58
50 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) 59 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
51 : pref_service_(pref_service), 60 : pref_service_(pref_service),
52 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { 61 type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
53 DCHECK(pref_service); 62 DCHECK(pref_service);
54 63
55 std::string quota = variations::GetVariationParamValue( 64 std::string quota = variations::GetVariationParamValue(
56 ntp_snippets::kStudyName, 65 ntp_snippets::kStudyName,
57 base::StringPrintf("quota_%s", GetRequestTypeAsString())); 66 base::StringPrintf("quota_%s", GetRequestTypeAsString()));
58 if (!base::StringToInt(quota, &quota_)) { 67 if (!base::StringToInt(quota, &quota_)) {
59 LOG_IF(WARNING, !quota.empty()) 68 LOG_IF(WARNING, !quota.empty())
60 << "Invalid variation parameter for quota for " 69 << "Invalid variation parameter for quota for "
61 << GetRequestTypeAsString(); 70 << GetRequestTypeAsString();
62 quota_ = type_info_.default_quota; 71 quota_ = type_info_.default_quota;
63 } 72 }
64 73
74 std::string interactive_quota = variations::GetVariationParamValue(
75 ntp_snippets::kStudyName,
76 base::StringPrintf("interactive_quota_%s", GetRequestTypeAsString()));
77 if (!base::StringToInt(interactive_quota, &interactive_quota_)) {
78 LOG_IF(WARNING, !interactive_quota.empty())
79 << "Invalid variation parameter for interactive quota for "
80 << GetRequestTypeAsString();
81 interactive_quota_ = type_info_.default_interactive_quota;
82 }
83
65 // Since the histogram names are dynamic, we cannot use the standard macros 84 // Since the histogram names are dynamic, we cannot use the standard macros
66 // and we need to lookup the histograms, instead. 85 // and we need to lookup the histograms, instead.
67 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); 86 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
68 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). 87 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|).
69 histogram_request_status_ = base::LinearHistogram::FactoryGet( 88 histogram_request_status_ = base::LinearHistogram::FactoryGet(
70 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", 89 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s",
71 GetRequestTypeAsString()), 90 GetRequestTypeAsString()),
72 1, status_count, status_count + 1, 91 1, status_count, status_count + 1,
73 base::HistogramBase::kUmaTargetedHistogramFlag); 92 base::HistogramBase::kUmaTargetedHistogramFlag);
74 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). 93 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample).
75 histogram_per_day_ = base::Histogram::FactoryGet( 94 histogram_per_day_ = base::Histogram::FactoryGet(
76 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", 95 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s",
77 GetRequestTypeAsString()), 96 GetRequestTypeAsString()),
78 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); 97 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
79 } 98 }
80 99
81 // static 100 // static
82 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { 101 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
83 for (const RequestTypeInfo& info : kRequestTypeInfo) { 102 for (const RequestTypeInfo& info : kRequestTypeInfo) {
84 registry->RegisterIntegerPref(info.count_pref, 0); 103 registry->RegisterIntegerPref(info.count_pref, 0);
104 registry->RegisterIntegerPref(info.interactive_count_pref, 0);
85 registry->RegisterIntegerPref(info.day_pref, 0); 105 registry->RegisterIntegerPref(info.day_pref, 0);
86 } 106 }
87 } 107 }
88 108
89 bool RequestThrottler::DemandQuotaForRequest(bool forced_request) { 109 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) {
90 ResetCounterIfDayChanged(); 110 ResetCounterIfDayChanged();
91 111
92 if (forced_request) { 112 int count = GetCount(interactive_request);
113 if (count < INT_MAX)
Marc Treib 2016/08/09 14:04:53 Do we ever expect to actually hit INT_MAX?! That w
jkrcal 2016/08/10 10:27:26 Okay, good point.
114 count++;
115 SetCount(interactive_request, count);
116 bool available = (count <= GetQuota(interactive_request));
117
118 if (interactive_request) {
Marc Treib 2016/08/09 14:04:53 if (interactive_request && available) ? Or maybe w
jkrcal 2016/08/10 10:27:26 Done. I tried to avoid touching the histograms but
93 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); 119 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED));
94 return true; 120 } else {
121 histogram_request_status_->Add(
122 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
123 : RequestStatus::QUOTA_EXCEEDED));
95 } 124 }
96
97 int new_count = GetCount() + 1;
98 SetCount(new_count);
99 bool available = (new_count <= quota_);
100
101 histogram_request_status_->Add(
102 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
103 : RequestStatus::QUOTA_EXCEEDED));
104 return available; 125 return available;
105 } 126 }
106 127
107 void RequestThrottler::ResetCounterIfDayChanged() { 128 void RequestThrottler::ResetCounterIfDayChanged() {
108 // The count of days since a fixed reference (the Unix epoch). 129 // The count of days since a fixed reference (the Unix epoch).
109 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); 130 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
110 131
111 if (!HasDay()) { 132 if (!HasDay()) {
112 // The counter is used for the first time in this profile. 133 // The counter is used for the first time in this profile.
113 SetDay(now_day); 134 SetDay(now_day);
114 } else if (now_day != GetDay()) { 135 } else if (now_day != GetDay()) {
115 // Day has changed - report the number of requests from the previous day. 136 // Day has changed - report the number of background requests from the
116 histogram_per_day_->Add(GetCount()); 137 // previous day.
Marc Treib 2016/08/09 14:04:53 Hm. Should we also report the number of interactiv
jkrcal 2016/08/10 10:27:26 Done.
138 histogram_per_day_->Add(GetCount(/*interactive_request=*/false));
117 // Reset the counter. 139 // Reset the counter.
118 SetCount(0); 140 SetCount(/*interactive_request=*/false, 0);
141 SetCount(/*interactive_request=*/true, 0);
119 SetDay(now_day); 142 SetDay(now_day);
120 } 143 }
121 } 144 }
122 145
123 const char* RequestThrottler::GetRequestTypeAsString() const { 146 const char* RequestThrottler::GetRequestTypeAsString() const {
124 return type_info_.name; 147 return type_info_.name;
125 } 148 }
126 149
127 int RequestThrottler::GetCount() const { 150 int RequestThrottler::GetQuota(bool interactive_request) const {
128 return pref_service_->GetInteger(type_info_.count_pref); 151 return interactive_request ? interactive_quota_ : quota_;
129 } 152 }
130 153
131 void RequestThrottler::SetCount(int count) { 154 int RequestThrottler::GetCount(bool interactive_request) const {
132 pref_service_->SetInteger(type_info_.count_pref, count); 155 return interactive_request
156 ? pref_service_->GetInteger(type_info_.interactive_count_pref)
157 : pref_service_->GetInteger(type_info_.count_pref);
Marc Treib 2016/08/09 14:04:53 You could move the ? into the GetInteger()
jkrcal 2016/08/10 10:27:26 Done.
158 }
159
160 void RequestThrottler::SetCount(bool interactive_request, int count) {
161 pref_service_->SetInteger(interactive_request
162 ? type_info_.interactive_count_pref
163 : type_info_.count_pref,
164 count);
133 } 165 }
134 166
135 int RequestThrottler::GetDay() const { 167 int RequestThrottler::GetDay() const {
136 return pref_service_->GetInteger(type_info_.day_pref); 168 return pref_service_->GetInteger(type_info_.day_pref);
137 } 169 }
138 170
139 void RequestThrottler::SetDay(int day) { 171 void RequestThrottler::SetDay(int day) {
140 pref_service_->SetInteger(type_info_.day_pref, day); 172 pref_service_->SetInteger(type_info_.day_pref, day);
141 } 173 }
142 174
143 bool RequestThrottler::HasDay() const { 175 bool RequestThrottler::HasDay() const {
144 return pref_service_->HasPrefPath(type_info_.day_pref); 176 return pref_service_->HasPrefPath(type_info_.day_pref);
145 } 177 }
146 178
147 } // namespace ntp_snippets 179 } // namespace ntp_snippets
OLDNEW
« components/ntp_snippets/request_throttler.h ('K') | « components/ntp_snippets/request_throttler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698