1 // Copyright 2017 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.
5 #include "components/metrics/metrics_upload_scheduler.h"
9 #include "base/feature_list.h"
10 #include "base/metrics/field_trial_params.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "build/build_config.h"
14 #include "components/metrics/metrics_scheduler.h"
20 // When uploading metrics to the server fails, we progressively wait longer and
21 // longer before sending the next log. This backoff process helps reduce load
22 // on a server that is having issues.
23 // The following is the multiplier we use to expand that inter-log duration.
24 const double kBackoffMultiplier = 2;
26 // The maximum backoff interval in hours.
27 const int kMaxBackoffIntervalHours = 24;
29 // Minutes to wait if we are unable to upload due to data usage cap.
30 const int kOverDataUsageIntervalMinutes = 5;
32 // Increases the upload interval each time it's called, to handle the case
33 // where the server is having issues.
34 base::TimeDelta BackOffUploadInterval(base::TimeDelta interval) {
35 DCHECK_GT(kBackoffMultiplier, 1.0);
36 interval = base::TimeDelta::FromMicroseconds(static_cast<int64_t>(
37 kBackoffMultiplier * interval.InMicroseconds()));
39 base::TimeDelta max_interval =
40 base::TimeDelta::FromHours(kMaxBackoffIntervalHours);
41 if (interval > max_interval || interval.InSeconds() < 0) {
42 interval = max_interval;
47 // Time delay after a log is uploaded successfully before attempting another.
48 // On mobile, keeping the radio on is very expensive, so prefer to keep this
49 // short and send in bursts.
50 base::TimeDelta GetUnsentLogsInterval() {
51 return base::TimeDelta::FromSeconds(3);
54 // Initial time delay after a log uploaded fails before retrying it.
55 base::TimeDelta GetInitialBackoffInterval() {
56 return base::TimeDelta::FromMinutes(5);
61 MetricsUploadScheduler::MetricsUploadScheduler(
62 const base::Closure& upload_callback)
63 : MetricsScheduler(upload_callback),
64 unsent_logs_interval_(GetUnsentLogsInterval()),
65 initial_backoff_interval_(GetInitialBackoffInterval()),
66 backoff_interval_(initial_backoff_interval_) {}
68 MetricsUploadScheduler::~MetricsUploadScheduler() {}
70 void MetricsUploadScheduler::UploadFinished(bool server_is_healthy) {
71 // If the server is having issues, back off. Otherwise, reset to default
72 // (unless there are more logs to send, in which case the next upload should
74 if (!server_is_healthy) {
75 TaskDone(backoff_interval_);
76 backoff_interval_ = BackOffUploadInterval(backoff_interval_);
78 backoff_interval_ = initial_backoff_interval_;
79 TaskDone(unsent_logs_interval_);
83 void MetricsUploadScheduler::StopAndUploadCancelled() {
85 TaskDone(unsent_logs_interval_);
88 void MetricsUploadScheduler::UploadOverDataUsageCap() {
89 TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes));
92 } // namespace metrics