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