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

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: Fixing the compile error 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 <limits.h>
7 #include <vector> 8 #include <vector>
8 9
9 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
12 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h" 14 #include "base/time/time.h"
14 #include "components/ntp_snippets/ntp_snippets_constants.h" 15 #include "components/ntp_snippets/ntp_snippets_constants.h"
15 #include "components/ntp_snippets/pref_names.h" 16 #include "components/ntp_snippets/pref_names.h"
16 #include "components/prefs/pref_registry_simple.h" 17 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h" 18 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_associated_data.h" 19 #include "components/variations/variations_associated_data.h"
19 20
20 namespace ntp_snippets { 21 namespace ntp_snippets {
21 22
22 namespace { 23 namespace {
23 24
24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA 25 // 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, 26 // histogram, so do not change existing values. Insert new values at the end,
26 // and update the histogram definition. 27 // and update the histogram definition.
27 enum class RequestStatus { 28 enum class RequestStatus {
28 FORCED, 29 INTERACTIVE_QUOTA_GRANTED,
29 QUOTA_GRANTED, 30 BACKGROUND_QUOTA_GRANTED,
30 QUOTA_EXCEEDED, 31 BACKGROUND_QUOTA_EXCEEDED,
32 INTERACTIVE_QUOTA_EXCEEDED,
31 REQUEST_STATUS_COUNT 33 REQUEST_STATUS_COUNT
32 }; 34 };
33 35
36 // Quota value to use if no quota should be applied (by default).
37 const int kUnlimitedQuota = INT_MAX;
38
34 } // namespace 39 } // namespace
35 40
36 struct RequestThrottler::RequestTypeInfo { 41 struct RequestThrottler::RequestTypeInfo {
37 const char* name; 42 const char* name;
38 const char* count_pref; 43 const char* count_pref;
44 const char* interactive_count_pref;
39 const char* day_pref; 45 const char* day_pref;
40 const int default_quota; 46 const int default_quota;
47 const int default_interactive_quota;
41 }; 48 };
42 49
43 // When adding a new type here, extend also the "RequestCounterTypes" 50 // When adding a new type here, extend also the "RequestThrottlerTypes"
44 // <histogram_suffixes> in histograms.xml with the |name| string. 51 // <histogram_suffixes> in histograms.xml with the |name| string.
45 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { 52 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = {
46 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, 53 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
47 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, 54 {"SuggestionFetcher", prefs::kSnippetFetcherRequestCount,
48 prefs::kSnippetFetcherQuotaDay, 50}}; 55 prefs::kSnippetFetcherInteractiveRequestCount,
56 prefs::kSnippetFetcherRequestsDay, 50, kUnlimitedQuota},
57 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL,
58 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount,
59 prefs::kSnippetThumbnailsInteractiveRequestCount,
60 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}};
49 61
50 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) 62 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
51 : pref_service_(pref_service), 63 : pref_service_(pref_service),
52 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { 64 type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
53 DCHECK(pref_service); 65 DCHECK(pref_service);
54 66
55 std::string quota = variations::GetVariationParamValue( 67 std::string quota = variations::GetVariationParamValue(
56 ntp_snippets::kStudyName, 68 ntp_snippets::kStudyName,
57 base::StringPrintf("quota_%s", GetRequestTypeAsString())); 69 base::StringPrintf("quota_%s", GetRequestTypeAsString()));
58 if (!base::StringToInt(quota, &quota_)) { 70 if (!base::StringToInt(quota, &quota_)) {
59 LOG_IF(WARNING, !quota.empty()) 71 LOG_IF(WARNING, !quota.empty())
60 << "Invalid variation parameter for quota for " 72 << "Invalid variation parameter for quota for "
61 << GetRequestTypeAsString(); 73 << GetRequestTypeAsString();
62 quota_ = type_info_.default_quota; 74 quota_ = type_info_.default_quota;
63 } 75 }
64 76
77 std::string interactive_quota = variations::GetVariationParamValue(
78 ntp_snippets::kStudyName,
79 base::StringPrintf("interactive_quota_%s", GetRequestTypeAsString()));
80 if (!base::StringToInt(interactive_quota, &interactive_quota_)) {
81 LOG_IF(WARNING, !interactive_quota.empty())
82 << "Invalid variation parameter for interactive quota for "
83 << GetRequestTypeAsString();
84 interactive_quota_ = type_info_.default_interactive_quota;
85 }
86
65 // Since the histogram names are dynamic, we cannot use the standard macros 87 // Since the histogram names are dynamic, we cannot use the standard macros
66 // and we need to lookup the histograms, instead. 88 // and we need to lookup the histograms, instead.
67 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); 89 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
68 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). 90 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|).
69 histogram_request_status_ = base::LinearHistogram::FactoryGet( 91 histogram_request_status_ = base::LinearHistogram::FactoryGet(
70 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", 92 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s",
71 GetRequestTypeAsString()), 93 GetRequestTypeAsString()),
72 1, status_count, status_count + 1, 94 1, status_count, status_count + 1,
73 base::HistogramBase::kUmaTargetedHistogramFlag); 95 base::HistogramBase::kUmaTargetedHistogramFlag);
74 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). 96 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample).
75 histogram_per_day_ = base::Histogram::FactoryGet( 97 histogram_per_day_ = base::Histogram::FactoryGet(
76 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", 98 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s",
77 GetRequestTypeAsString()), 99 GetRequestTypeAsString()),
78 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); 100 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
79 } 101 }
80 102
81 // static 103 // static
82 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { 104 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
83 for (const RequestTypeInfo& info : kRequestTypeInfo) { 105 for (const RequestTypeInfo& info : kRequestTypeInfo) {
84 registry->RegisterIntegerPref(info.count_pref, 0); 106 registry->RegisterIntegerPref(info.count_pref, 0);
107 registry->RegisterIntegerPref(info.interactive_count_pref, 0);
85 registry->RegisterIntegerPref(info.day_pref, 0); 108 registry->RegisterIntegerPref(info.day_pref, 0);
86 } 109 }
87 } 110 }
88 111
89 bool RequestThrottler::DemandQuotaForRequest(bool forced_request) { 112 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) {
90 ResetCounterIfDayChanged(); 113 ResetCounterIfDayChanged();
91 114
92 if (forced_request) { 115 int new_count = GetCount(interactive_request) + 1;
93 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); 116 SetCount(interactive_request, new_count);
94 return true; 117 bool available = (new_count <= GetQuota(interactive_request));
118
119 if (interactive_request) {
120 histogram_request_status_->Add(static_cast<int>(
121 available ? RequestStatus::INTERACTIVE_QUOTA_GRANTED
122 : RequestStatus::INTERACTIVE_QUOTA_EXCEEDED));
123 } else {
124 histogram_request_status_->Add(
125 static_cast<int>(available ? RequestStatus::BACKGROUND_QUOTA_GRANTED
126 : RequestStatus::BACKGROUND_QUOTA_EXCEEDED));
95 } 127 }
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; 128 return available;
105 } 129 }
106 130
107 void RequestThrottler::ResetCounterIfDayChanged() { 131 void RequestThrottler::ResetCounterIfDayChanged() {
108 // The count of days since a fixed reference (the Unix epoch). 132 // The count of days since a fixed reference (the Unix epoch).
109 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); 133 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
110 134
111 if (!HasDay()) { 135 if (!HasDay()) {
112 // The counter is used for the first time in this profile. 136 // The counter is used for the first time in this profile.
113 SetDay(now_day); 137 SetDay(now_day);
114 } else if (now_day != GetDay()) { 138 } else if (now_day != GetDay()) {
115 // Day has changed - report the number of requests from the previous day. 139 // Day has changed - report the number of background requests from the
116 histogram_per_day_->Add(GetCount()); 140 // previous day.
141 histogram_per_day_->Add(GetCount(/*interactive_request=*/false));
117 // Reset the counter. 142 // Reset the counter.
118 SetCount(0); 143 SetCount(/*interactive_request=*/false, 0);
144 SetCount(/*interactive_request=*/true, 0);
119 SetDay(now_day); 145 SetDay(now_day);
120 } 146 }
121 } 147 }
122 148
123 const char* RequestThrottler::GetRequestTypeAsString() const { 149 const char* RequestThrottler::GetRequestTypeAsString() const {
124 return type_info_.name; 150 return type_info_.name;
125 } 151 }
126 152
127 int RequestThrottler::GetCount() const { 153 int RequestThrottler::GetQuota(bool interactive_request) const {
128 return pref_service_->GetInteger(type_info_.count_pref); 154 return interactive_request ? interactive_quota_ : quota_;
129 } 155 }
130 156
131 void RequestThrottler::SetCount(int count) { 157 int RequestThrottler::GetCount(bool interactive_request) const {
132 pref_service_->SetInteger(type_info_.count_pref, count); 158 return pref_service_->GetInteger(interactive_request
159 ? type_info_.interactive_count_pref
160 : type_info_.count_pref);
161 }
162
163 void RequestThrottler::SetCount(bool interactive_request, int count) {
164 pref_service_->SetInteger(interactive_request
165 ? type_info_.interactive_count_pref
166 : type_info_.count_pref,
167 count);
133 } 168 }
134 169
135 int RequestThrottler::GetDay() const { 170 int RequestThrottler::GetDay() const {
136 return pref_service_->GetInteger(type_info_.day_pref); 171 return pref_service_->GetInteger(type_info_.day_pref);
137 } 172 }
138 173
139 void RequestThrottler::SetDay(int day) { 174 void RequestThrottler::SetDay(int day) {
140 pref_service_->SetInteger(type_info_.day_pref, day); 175 pref_service_->SetInteger(type_info_.day_pref, day);
141 } 176 }
142 177
143 bool RequestThrottler::HasDay() const { 178 bool RequestThrottler::HasDay() const {
144 return pref_service_->HasPrefPath(type_info_.day_pref); 179 return pref_service_->HasPrefPath(type_info_.day_pref);
145 } 180 }
146 181
147 } // namespace ntp_snippets 182 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698