Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / components / metrics / metrics_upload_scheduler.cc
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.
4
5 #include "components/metrics/metrics_upload_scheduler.h"
6
7 #include <stdint.h>
8
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"
15
16 namespace metrics {
17
18 namespace {
19
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 = 1.1;
25
26 // The maximum backoff interval in minutes.
27 const int kMaxBackoffIntervalMinutes = 10;
28
29 // Minutes to wait if we are unable to upload due to data usage cap.
30 const 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.
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()));
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
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);
52 }
53
54 // Inital time delay after a log uploaded fails before retrying it.
55 base::TimeDelta GetInitialBackoffInterval() {
56   return base::TimeDelta::FromSeconds(15);
57 }
58
59 }  // namespace
60
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_) {}
67
68 MetricsUploadScheduler::~MetricsUploadScheduler() {}
69
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
73   // happen sooner).
74   if (!server_is_healthy) {
75     TaskDone(backoff_interval_);
76     backoff_interval_ = BackOffUploadInterval(backoff_interval_);
77   } else {
78     backoff_interval_ = initial_backoff_interval_;
79     TaskDone(unsent_logs_interval_);
80   }
81 }
82
83 void MetricsUploadScheduler::StopAndUploadCancelled() {
84   Stop();
85   TaskDone(unsent_logs_interval_);
86 }
87
88 void MetricsUploadScheduler::UploadOverDataUsageCap() {
89   TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes));
90 }
91
92 }  // namespace metrics