Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "base/metrics/histogram.h" | |
| 8 #include "net/base/load_flags.h" | |
| 9 | |
| 10 using base::TimeDelta; | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // The delay, in seconds, between uploading when there are queued logs to send. | |
| 15 const int kUnsentLogsIntervalSeconds = 3; | |
| 16 | |
| 17 // When uploading metrics to the server fails, we progressively wait longer and | |
| 18 // longer before sending the next log. This backoff process helps reduce load | |
| 19 // on a server that is having issues. | |
| 20 // The following is the multiplier we use to expand that inter-log duration. | |
| 21 const double kBackoffMultiplier = 1.1; | |
| 22 | |
| 23 // The maximum backoff multiplier. | |
| 24 const int kMaxBackoffIntervalSeconds = 60 * 60; | |
| 25 | |
| 26 // The maximum number of unsent logs we will keep. | |
| 27 // TODO(holte): Limit based on log size instead. | |
| 28 const size_t kMaxQueuedLogs = 10; | |
| 29 | |
| 30 enum ResponseStatus { | |
| 31 UNKNOWN_FAILURE, | |
| 32 SUCCESS, | |
| 33 BAD_REQUEST, // Invalid syntax or log too large. | |
| 34 NO_RESPONSE, | |
| 35 NUM_RESPONSE_STATUSES | |
| 36 }; | |
| 37 | |
| 38 ResponseStatus ResponseCodeToStatus(int response_code) { | |
| 39 switch (response_code) { | |
| 40 case 200: | |
| 41 return SUCCESS; | |
| 42 case 400: | |
| 43 return BAD_REQUEST; | |
| 44 case net::URLFetcher::RESPONSE_CODE_INVALID: | |
| 45 return NO_RESPONSE; | |
| 46 default: | |
| 47 return UNKNOWN_FAILURE; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 enum DiscardReason { | |
| 52 UPLOAD_SUCCESS, | |
| 53 UPLOAD_REJECTED, | |
| 54 QUEUE_OVERFLOW, | |
| 55 NUM_DISCARD_REASONS | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace rappor { | |
| 61 | |
| 62 LogUploader::LogUploader(const GURL& server_url, | |
| 63 const std::string& mime_type, | |
| 64 net::URLRequestContextGetter* request_context) | |
| 65 : server_url_(server_url), | |
| 66 mime_type_(mime_type), | |
| 67 request_context_(request_context), | |
| 68 has_callback_pending_(false), | |
| 69 upload_interval_(TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds)) { | |
| 70 } | |
| 71 | |
| 72 LogUploader::~LogUploader() {} | |
| 73 | |
| 74 void LogUploader::QueueLog(const std::string& log) { | |
| 75 queued_logs_.push(log); | |
| 76 if (!upload_timer_.IsRunning() && !has_callback_pending_) | |
| 77 StartScheduledUpload(); | |
| 78 } | |
| 79 | |
| 80 void LogUploader::ScheduleNextUpload() { | |
| 81 if (upload_timer_.IsRunning() || has_callback_pending_) | |
| 82 return; | |
| 83 | |
| 84 upload_timer_.Start( | |
| 85 FROM_HERE, upload_interval_, this, &LogUploader::StartScheduledUpload); | |
| 86 } | |
| 87 | |
| 88 void LogUploader::StartScheduledUpload() { | |
| 89 DCHECK(!has_callback_pending_); | |
| 90 has_callback_pending_ = true; | |
| 91 current_fetch_.reset( | |
| 92 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this)); | |
| 93 current_fetch_->SetRequestContext(request_context_.get()); | |
| 94 current_fetch_->SetUploadData(mime_type_, queued_logs_.front()); | |
| 95 | |
| 96 // We already drop cookies server-side, but we might as well strip them out | |
| 97 // client-side as well. | |
| 98 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 99 net::LOAD_DO_NOT_SEND_COOKIES); | |
| 100 current_fetch_->Start(); | |
| 101 } | |
| 102 | |
| 103 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 104 // We're not allowed to re-use the existing |URLFetcher|s, so free them here. | |
| 105 // Note however that |source| is aliased to the fetcher, so we should be | |
| 106 // careful not to delete it too early. | |
| 107 DCHECK_EQ(current_fetch_.get(), source); | |
| 108 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass()); | |
| 109 | |
| 110 int response_code = source->GetResponseCode(); | |
| 111 | |
| 112 // Log a histogram to track response success vs. failure rates. | |
| 113 UMA_HISTOGRAM_ENUMERATION("Rappor.UploadResponseStatus", | |
| 114 ResponseCodeToStatus(response_code), | |
|
Alexei Svitkine (slow)
2014/02/06 17:47:20
I know this how the MetricsService does it, but I'
Steven Holte
2014/02/06 23:07:08
Done.
| |
| 115 NUM_RESPONSE_STATUSES); | |
| 116 | |
| 117 bool upload_succeeded = response_code == 200; | |
| 118 | |
| 119 // Determine whether this log should be retransmitted. | |
| 120 bool discard_log = false; | |
| 121 if (upload_succeeded) { | |
| 122 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", | |
|
Alexei Svitkine (slow)
2014/02/06 17:47:20
Make this a separate function in the anon namespac
Steven Holte
2014/02/06 23:07:08
Rewrote so the macros is only used once.
| |
| 123 UPLOAD_SUCCESS, | |
| 124 NUM_DISCARD_REASONS); | |
| 125 discard_log = true; | |
| 126 } else if (response_code == 400) { | |
| 127 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", | |
| 128 UPLOAD_REJECTED, | |
| 129 NUM_DISCARD_REASONS); | |
| 130 discard_log = true; | |
| 131 } else if (queued_logs_.size() > kMaxQueuedLogs) { | |
| 132 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", | |
| 133 QUEUE_OVERFLOW, | |
| 134 NUM_DISCARD_REASONS); | |
| 135 discard_log = true; | |
| 136 } | |
| 137 | |
| 138 if (discard_log) | |
| 139 queued_logs_.pop(); | |
| 140 | |
| 141 // Error 400 indicates a problem with the log, not with the server, so | |
| 142 // don't consider that a sign that the server is in trouble. | |
| 143 bool server_is_healthy = upload_succeeded || response_code == 400; | |
| 144 UploadFinished(server_is_healthy, !queued_logs_.empty()); | |
| 145 } | |
| 146 | |
| 147 void LogUploader::UploadFinished(bool server_is_healthy, | |
| 148 bool more_logs_remaining) { | |
| 149 DCHECK(has_callback_pending_); | |
| 150 has_callback_pending_ = false; | |
| 151 // If the server is having issues, back off. Otherwise, reset to default. | |
| 152 if (!server_is_healthy) | |
| 153 BackOffUploadInterval(); | |
| 154 else | |
| 155 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); | |
| 156 | |
| 157 if (more_logs_remaining) | |
| 158 ScheduleNextUpload(); | |
| 159 } | |
| 160 | |
| 161 void LogUploader::BackOffUploadInterval() { | |
| 162 DCHECK_GT(kBackoffMultiplier, 1.0); | |
| 163 upload_interval_ = TimeDelta::FromMicroseconds(static_cast<int64>( | |
| 164 kBackoffMultiplier * upload_interval_.InMicroseconds())); | |
| 165 | |
| 166 TimeDelta max_interval = TimeDelta::FromSeconds(kMaxBackoffIntervalSeconds); | |
| 167 // Clamp large/overflowed times to the maximum interval. | |
| 168 if (upload_interval_ > max_interval || upload_interval_.InSeconds() < 0) | |
| 169 upload_interval_ = max_interval; | |
| 170 } | |
| 171 | |
| 172 } // namespace rappor | |
| OLD | NEW |