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