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

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

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tim's comments (renaming) Created 4 years, 5 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/request_throttler.h"
6
7 #include <vector>
8
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h"
14 #include "components/ntp_snippets/ntp_snippets_constants.h"
15 #include "components/ntp_snippets/pref_names.h"
16 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_associated_data.h"
19
20 namespace ntp_snippets {
21
22 // Used internally for working with a RequestType.
23 struct RequestTypeInfo {
24 const char* name;
25 const char* count_pref;
26 const char* day_pref;
27 };
28
29 namespace {
30
31 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA
32 // histogram, so do not change existing values. Insert new values at the end,
33 // and update the histogram definition.
34 enum class RequestStatus {
35 FORCED,
36 QUOTA_GRANTED,
37 QUOTA_EXCEEDED,
38 REQUEST_STATUS_COUNT
39 };
40
41 // When adding a new type here, extend also the "RequestCounterTypes"
42 // <histogram_suffixes> in histograms.xml with the |name| string.
43 const RequestTypeInfo kRequestTypeInfo[] = {
44 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
45 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount,
46 prefs::kSnippetFetcherQuotaDay}};
47
48 } // namespace
49
50 RequestThrottler::RequestThrottler(PrefService* pref_service,
51 RequestType type,
52 int default_quota)
53 : pref_service_(pref_service),
54 type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
55 DCHECK(pref_service);
56
57 std::string quota = variations::GetVariationParamValue(
58 ntp_snippets::kStudyName,
59 base::StringPrintf("quota_%s", GetRequestTypeAsString()));
60 if (!base::StringToInt(quota, &quota_)) {
61 LOG_IF(WARNING, !quota.empty())
62 << "Invalid variation parameter for quota for "
63 << GetRequestTypeAsString();
64 quota_ = default_quota;
65 }
66
67 // Since the histogram names are dynamic, we cannot use the standard macros
68 // and we need to lookup the histograms, instead.
69 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
70 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|).
71 histogram_request_status_ = base::LinearHistogram::FactoryGet(
72 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s",
73 GetRequestTypeAsString()),
74 1, status_count, status_count + 1,
75 base::HistogramBase::kUmaTargetedHistogramFlag);
76 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample).
77 histogram_per_day_ = base::Histogram::FactoryGet(
78 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s",
79 GetRequestTypeAsString()),
80 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
81 }
82
83 // static
84 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
85 for (const RequestTypeInfo& info : kRequestTypeInfo) {
86 registry->RegisterIntegerPref(info.count_pref, 0);
87 registry->RegisterIntegerPref(info.day_pref, 0);
88 }
89 }
90
91 bool RequestThrottler::DemandQuotaForRequest(bool forced_request) {
92 ResetCounterIfDayChanged();
93
94 if (forced_request) {
95 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED));
96 return true;
97 }
98
99 int new_count = GetCount() + 1;
100 SetCount(new_count);
101 bool available = (new_count <= quota_);
102
103 histogram_request_status_->Add(
104 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
105 : RequestStatus::QUOTA_EXCEEDED));
106 return available;
107 }
108
109 void RequestThrottler::ResetCounterIfDayChanged() {
110 // The count of days since a fixed reference (the Unix epoch).
111 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
112
113 if (!HasDay()) {
114 // The counter is used for the first time in this profile.
115 SetDay(now_day);
116 } else if (now_day != GetDay()) {
117 // Day has changed - report the number of requests from the previous day.
118 histogram_per_day_->Add(GetCount());
119 // Reset the counter.
120 SetCount(0);
121 SetDay(now_day);
122 }
123 }
124
125 const char* RequestThrottler::GetRequestTypeAsString() const {
126 return type_info_.name;
127 }
128
129 int RequestThrottler::GetCount() const {
130 return pref_service_->GetInteger(type_info_.count_pref);
131 }
132
133 void RequestThrottler::SetCount(int count) {
134 pref_service_->SetInteger(type_info_.count_pref, count);
135 }
136
137 int RequestThrottler::GetDay() const {
138 return pref_service_->GetInteger(type_info_.day_pref);
139 }
140
141 void RequestThrottler::SetDay(int day) {
142 pref_service_->SetInteger(type_info_.day_pref, day);
143 }
144
145 bool RequestThrottler::HasDay() const {
146 return pref_service_->HasPrefPath(type_info_.day_pref);
147 }
148
149 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698