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

Side by Side Diff: components/rappor/log_uploader.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Seperated log generation and uploading Created 7 years 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 (c) 2013 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/rappor/log_uploader.h"
6
7 //#include "chrome/browser/metrics/compression_utils.h"
8 #include "net/base/load_flags.h"
9 #include "url/gurl.h"
10
11 using base::TimeDelta;
12
13 namespace {
14
15 // The delay, in seconds, between uploading when there are queued logs from
16 // previous sessions to send.
17 const int kUnsentLogsIntervalSeconds = 3;
18
19 // When uploading metrics to the server fails, we progressively wait longer and
20 // longer before sending the next log. This backoff process helps reduce load
21 // on a server that is having issues.
22 // The following is the multiplier we use to expand that inter-log duration.
23 const double kBackoffMultiplier = 1.1;
24
25 // The maximum backoff multiplier.
26 const int kMaxBackoffMultiplier = 10;
27
28 // If an upload fails, and the transmission was over this byte count, then we
29 // will discard the log, and not try to retransmit it. We also don't persist
30 // the log to the prefs for transmission during the next chrome session if this
31 // limit is exceeded.
32 const size_t kUploadLogAvoidRetransmitSize = 50000;
33
34 // The maximum number of unsent logs we will keep.
35 const size_t kMaxQueuedLogs = 10;
36
37 } // anonymous namespace
38
39
40 namespace rappor {
41
42 LogUploader::LogUploader(const char* server_url, const char* mime_type)
43 : server_url_(server_url), mime_type_(mime_type), callback_pending_(false) {
44 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds);
45 }
46
47 void LogUploader::SetRequestContext(
48 net::URLRequestContextGetter* request_context) {
49 request_context_ = request_context;
50 }
51
52 void LogUploader::QueueLog(const std::string& log) {
53 queued_logs_.push(log);
54 if (!upload_timer_.IsRunning() && !callback_pending_) {
55 StartScheduledUpload();
56 }
57 }
58
59 void LogUploader::ScheduleNextUpload() {
60 if (upload_timer_.IsRunning() || callback_pending_)
61 return;
62
63 upload_timer_.Start(FROM_HERE, upload_interval_, this,
64 &LogUploader::StartScheduledUpload);
65 }
66
67 void LogUploader::StartScheduledUpload() {
68 callback_pending_ = true;
69 current_fetch_.reset(
70 net::URLFetcher::Create(GURL(server_url_), net::URLFetcher::POST, this));
71 current_fetch_->SetRequestContext(request_context_);
72 /*
73 std::string compressed_log_text;
74 bool compression_successful =
75 chrome::GzipCompress(log_text, &compressed_log_text);
76 DCHECK(compression_successful);
77 if (compression_successful) {
78 current_fetch_->SetUploadData(kMimeType, compressed_log_text);
79 // Tell the server that we're uploading gzipped protobufs.
80 current_fetch_->SetExtraRequestHeaders("content-encoding: gzip");
81 }*/
82 current_fetch_->SetUploadData(mime_type_, queued_logs_.front());
83
84 // We already drop cookies server-side, but we might as well strip them out
85 // client-side as well.
86 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
87 net::LOAD_DO_NOT_SEND_COOKIES);
88 current_fetch_->Start();
89 }
90
91 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) {
92 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
93 // Note however that |source| is aliased to the fetcher, so we should be
94 // careful not to delete it too early.
95 DCHECK_EQ(current_fetch_.get(), source);
96 scoped_ptr<net::URLFetcher> s(current_fetch_.Pass());
97
98 int response_code = source->GetResponseCode();
99
100 bool upload_succeeded = response_code == 200;
101
102 // Provide boolean for error recovery (allow us to ignore response_code).
103 bool discard_log = false;
104 const size_t log_size = queued_logs_.front().length();
105 if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) {
106 discard_log = true;
107 } else if (queued_logs_.size() > kMaxQueuedLogs) {
108 discard_log = true;
109 } else if (response_code == 400) {
110 // Bad syntax. Retransmission won't work.
111 discard_log = true;
112 }
113
114 if (upload_succeeded || discard_log)
115 queued_logs_.pop();
116
117 // Error 400 indicates a problem with the log, not with the server, so
118 // don't consider that a sign that the server is in trouble.
119 bool server_is_healthy = upload_succeeded || response_code == 400;
120 UploadFinished(server_is_healthy, !queued_logs_.empty());
121 }
122
123 void LogUploader::UploadFinished(bool server_is_healthy,
124 bool more_logs_remaining) {
125 DCHECK(callback_pending_);
126 callback_pending_ = false;
127 // If the server is having issues, back off. Otherwise, reset to default.
128 if (!server_is_healthy) {
129 BackOffUploadInterval();
130 } else {
131 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds);
132 }
133
134 if (more_logs_remaining) {
135 ScheduleNextUpload();
136 }
137 }
138
139 void LogUploader::BackOffUploadInterval() {
140 DCHECK_GT(kBackoffMultiplier, 1.0);
141 upload_interval_ = TimeDelta::FromMicroseconds(
142 static_cast<int64>(kBackoffMultiplier *
143 upload_interval_.InMicroseconds()));
144
145 TimeDelta max_interval = kMaxBackoffMultiplier *
146 TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds);
147 if (upload_interval_ > max_interval || upload_interval_.InSeconds() < 0) {
148 upload_interval_ = max_interval;
149 }
150 }
151
152 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698