| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/metrics/metrics_reporting_scheduler.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "chrome/browser/metrics/metrics_service.h" |
| 9 |
| 10 using base::Time; |
| 11 using base::TimeDelta; |
| 12 |
| 13 // The delay, in seconds, after startup before sending the first log message. |
| 14 static const int kInitialUploadIntervalSeconds = 60; |
| 15 |
| 16 // The delay, in seconds, between uploading when there are queued logs from |
| 17 // previous sessions to send. |
| 18 static const int kUnsentLogsIntervalSeconds = 15; |
| 19 |
| 20 // Standard interval between log uploads, in seconds. |
| 21 static const int kStandardUploadIntervalSeconds = 30 * 60; // Thirty minutes. |
| 22 |
| 23 // When uploading metrics to the server fails, we progressively wait longer and |
| 24 // longer before sending the next log. This backoff process helps reduce load |
| 25 // on a server that is having issues. |
| 26 // The following is the multiplier we use to expand that inter-log duration. |
| 27 static const double kBackoffMultiplier = 1.1; |
| 28 |
| 29 // The maximum backoff multiplier. |
| 30 static const int kMaxBackoffMultiplier = 10; |
| 31 |
| 32 |
| 33 MetricsReportingScheduler::MetricsReportingScheduler( |
| 34 const base::Closure& upload_callback) |
| 35 : upload_callback_(upload_callback), |
| 36 ALLOW_THIS_IN_INITIALIZER_LIST(upload_timer_factory_(this)), |
| 37 upload_interval_(TimeDelta::FromSeconds(kInitialUploadIntervalSeconds)), |
| 38 running_(false), |
| 39 timer_pending_(false), |
| 40 callback_pending_(false) { |
| 41 } |
| 42 |
| 43 MetricsReportingScheduler::~MetricsReportingScheduler() {} |
| 44 |
| 45 void MetricsReportingScheduler::Start() { |
| 46 running_ = true; |
| 47 ScheduleNextCallback(); |
| 48 } |
| 49 |
| 50 void MetricsReportingScheduler::Stop() { |
| 51 running_ = false; |
| 52 } |
| 53 |
| 54 void MetricsReportingScheduler::UploadFinished(bool server_is_healthy, |
| 55 bool more_logs_remaining) { |
| 56 DCHECK(callback_pending_); |
| 57 callback_pending_ = false; |
| 58 // If the server is having issues, back off. Otherwise, reset to default |
| 59 // (unless there are more logs to send, in which case the next upload should |
| 60 // happen sooner). |
| 61 if (!server_is_healthy) { |
| 62 BackOffUploadInterval(); |
| 63 } else if (more_logs_remaining) { |
| 64 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); |
| 65 } else { |
| 66 upload_interval_ = TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); |
| 67 } |
| 68 |
| 69 if (running_) |
| 70 ScheduleNextCallback(); |
| 71 } |
| 72 |
| 73 void MetricsReportingScheduler::UploadCancelled() { |
| 74 DCHECK(callback_pending_); |
| 75 callback_pending_ = false; |
| 76 if (running_) |
| 77 ScheduleNextCallback(); |
| 78 } |
| 79 |
| 80 void MetricsReportingScheduler::TriggerUpload() { |
| 81 timer_pending_ = false; |
| 82 callback_pending_ = true; |
| 83 upload_callback_.Run(); |
| 84 } |
| 85 |
| 86 void MetricsReportingScheduler::ScheduleNextCallback() { |
| 87 DCHECK(running_); |
| 88 if (timer_pending_ || callback_pending_) |
| 89 return; |
| 90 |
| 91 timer_pending_ = true; |
| 92 |
| 93 MessageLoop::current()->PostDelayedTask( |
| 94 FROM_HERE, |
| 95 upload_timer_factory_.NewRunnableMethod( |
| 96 &MetricsReportingScheduler::TriggerUpload), |
| 97 upload_interval_.InMilliseconds()); |
| 98 } |
| 99 |
| 100 void MetricsReportingScheduler::BackOffUploadInterval() { |
| 101 DCHECK(kBackoffMultiplier > 1.0); |
| 102 upload_interval_ = TimeDelta::FromMicroseconds( |
| 103 static_cast<int64>(kBackoffMultiplier * |
| 104 upload_interval_.InMicroseconds())); |
| 105 |
| 106 TimeDelta max_interval = kMaxBackoffMultiplier * |
| 107 TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); |
| 108 if (upload_interval_ > max_interval || upload_interval_.InSeconds() < 0) { |
| 109 upload_interval_ = max_interval; |
| 110 } |
| 111 } |
| OLD | NEW |