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 #include "net/url_request/url_fetcher.h" |
| 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 DiscardReason { |
| 31 UPLOAD_SUCCESS, |
| 32 UPLOAD_REJECTED, |
| 33 QUEUE_OVERFLOW, |
| 34 NUM_DISCARD_REASONS |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 namespace rappor { |
| 40 |
| 41 LogUploader::LogUploader(const GURL& server_url, |
| 42 const std::string& mime_type, |
| 43 net::URLRequestContextGetter* request_context) |
| 44 : server_url_(server_url), |
| 45 mime_type_(mime_type), |
| 46 request_context_(request_context), |
| 47 has_callback_pending_(false), |
| 48 upload_interval_(base::TimeDelta::FromSeconds( |
| 49 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 // static |
| 88 base::TimeDelta LogUploader::BackOffUploadInterval(base::TimeDelta interval) { |
| 89 DCHECK_GT(kBackoffMultiplier, 1.0); |
| 90 interval = base::TimeDelta::FromMicroseconds(static_cast<int64>( |
| 91 kBackoffMultiplier * interval.InMicroseconds())); |
| 92 |
| 93 base::TimeDelta max_interval = |
| 94 base::TimeDelta::FromSeconds(kMaxBackoffIntervalSeconds); |
| 95 return interval > max_interval ? max_interval : interval; |
| 96 } |
| 97 |
| 98 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) { |
| 99 // We're not allowed to re-use the existing |URLFetcher|s, so free them here. |
| 100 // Note however that |source| is aliased to the fetcher, so we should be |
| 101 // careful not to delete it too early. |
| 102 DCHECK_EQ(current_fetch_.get(), source); |
| 103 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass()); |
| 104 |
| 105 int response_code = source->GetResponseCode(); |
| 106 |
| 107 // Log a histogram to track response success vs. failure rates. |
| 108 UMA_HISTOGRAM_SPARSE_SLOWLY("Rappor.UploadResponseCode", response_code); |
| 109 |
| 110 bool upload_succeeded = response_code == 200; |
| 111 |
| 112 // Determine whether this log should be retransmitted. |
| 113 DiscardReason reason = NUM_DISCARD_REASONS; |
| 114 if (upload_succeeded) { |
| 115 reason = UPLOAD_SUCCESS; |
| 116 } else if (response_code == 400) { |
| 117 reason = UPLOAD_REJECTED; |
| 118 } else if (queued_logs_.size() > kMaxQueuedLogs) { |
| 119 reason = QUEUE_OVERFLOW; |
| 120 } |
| 121 |
| 122 if (reason != NUM_DISCARD_REASONS) { |
| 123 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", |
| 124 reason, |
| 125 NUM_DISCARD_REASONS); |
| 126 queued_logs_.pop(); |
| 127 } |
| 128 |
| 129 // Error 400 indicates a problem with the log, not with the server, so |
| 130 // don't consider that a sign that the server is in trouble. |
| 131 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 132 OnUploadFinished(server_is_healthy, !queued_logs_.empty()); |
| 133 } |
| 134 |
| 135 void LogUploader::OnUploadFinished(bool server_is_healthy, |
| 136 bool more_logs_remaining) { |
| 137 DCHECK(has_callback_pending_); |
| 138 has_callback_pending_ = false; |
| 139 // If the server is having issues, back off. Otherwise, reset to default. |
| 140 if (!server_is_healthy) |
| 141 upload_interval_ = BackOffUploadInterval(upload_interval_); |
| 142 else |
| 143 upload_interval_ = base::TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); |
| 144 |
| 145 if (more_logs_remaining) |
| 146 ScheduleNextUpload(upload_interval_); |
| 147 } |
| 148 |
| 149 } // namespace rappor |
OLD | NEW |