blob: 457a725f412227013bc07f20804e92e88ca426b1 [file] [log] [blame]
holte68395852017-01-10 20:40:211// 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.
4
5#include "components/metrics/metrics_upload_scheduler.h"
6
7#include <stdint.h>
8
holte85bbd9d2017-02-14 20:55:589#include "base/feature_list.h"
10#include "base/metrics/field_trial_params.h"
holte68395852017-01-10 20:40:2111#include "base/metrics/histogram_macros.h"
holte85bbd9d2017-02-14 20:55:5812#include "base/strings/string_number_conversions.h"
holte68395852017-01-10 20:40:2113#include "build/build_config.h"
14#include "components/metrics/metrics_scheduler.h"
15
16namespace metrics {
17
holte85bbd9d2017-02-14 20:55:5818namespace {
holte68395852017-01-10 20:40:2119
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.
24const double kBackoffMultiplier = 1.1;
25
26// The maximum backoff interval in minutes.
27const int kMaxBackoffIntervalMinutes = 10;
28
29// Minutes to wait if we are unable to upload due to data usage cap.
30const int kOverDataUsageIntervalMinutes = 5;
31
32// Increases the upload interval each time it's called, to handle the case
33// where the server is having issues.
34base::TimeDelta BackOffUploadInterval(base::TimeDelta interval) {
35 DCHECK_GT(kBackoffMultiplier, 1.0);
36 interval = base::TimeDelta::FromMicroseconds(static_cast<int64_t>(
37 kBackoffMultiplier * interval.InMicroseconds()));
38
39 base::TimeDelta max_interval =
40 base::TimeDelta::FromMinutes(kMaxBackoffIntervalMinutes);
41 if (interval > max_interval || interval.InSeconds() < 0) {
42 interval = max_interval;
43 }
44 return interval;
45}
46
holte85bbd9d2017-02-14 20:55:5847// 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.
50base::TimeDelta GetUnsentLogsInterval() {
Steven Holtea2c02632018-01-30 21:24:0451 return base::TimeDelta::FromSeconds(3);
holte85bbd9d2017-02-14 20:55:5852}
53
54// Inital time delay after a log uploaded fails before retrying it.
55base::TimeDelta GetInitialBackoffInterval() {
Steven Holtea2c02632018-01-30 21:24:0456 return base::TimeDelta::FromSeconds(15);
holte85bbd9d2017-02-14 20:55:5857}
58
holte68395852017-01-10 20:40:2159} // namespace
60
61MetricsUploadScheduler::MetricsUploadScheduler(
62 const base::Closure& upload_callback)
63 : MetricsScheduler(upload_callback),
holte85bbd9d2017-02-14 20:55:5864 unsent_logs_interval_(GetUnsentLogsInterval()),
65 initial_backoff_interval_(GetInitialBackoffInterval()),
66 backoff_interval_(initial_backoff_interval_) {}
holte68395852017-01-10 20:40:2167
68MetricsUploadScheduler::~MetricsUploadScheduler() {}
69
70void 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
73 // happen sooner).
74 if (!server_is_healthy) {
holte85bbd9d2017-02-14 20:55:5875 TaskDone(backoff_interval_);
76 backoff_interval_ = BackOffUploadInterval(backoff_interval_);
holte68395852017-01-10 20:40:2177 } else {
holte85bbd9d2017-02-14 20:55:5878 backoff_interval_ = initial_backoff_interval_;
79 TaskDone(unsent_logs_interval_);
holte68395852017-01-10 20:40:2180 }
holte68395852017-01-10 20:40:2181}
82
holte85bbd9d2017-02-14 20:55:5883void MetricsUploadScheduler::StopAndUploadCancelled() {
84 Stop();
85 TaskDone(unsent_logs_interval_);
holte68395852017-01-10 20:40:2186}
87
88void MetricsUploadScheduler::UploadOverDataUsageCap() {
89 TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes));
90}
91
holte68395852017-01-10 20:40:2192} // namespace metrics