Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / components / metrics / metrics_upload_scheduler.cc
1 // Copyright 2017 The Chromium Authors
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 namespace {
18
19 // When uploading metrics to the server fails, we progressively wait longer and
20 // longer before sending the next log. This backoff process helps reduce load
21 // on a server that is having issues.
22 // The following is the multiplier we use to expand that inter-log duration.
23 const double kBackoffMultiplier = 2;
24
25 // The maximum backoff interval in hours.
26 const int kMaxBackoffIntervalHours = 24;
27
28 // Minutes to wait if we are unable to upload due to data usage cap.
29 const int kOverDataUsageIntervalMinutes = 5;
30
31 // Increases the upload interval each time it's called, to handle the case
32 // where the server is having issues.
33 base::TimeDelta BackOffUploadInterval(base::TimeDelta interval) {
34   DCHECK_GT(kBackoffMultiplier, 1.0);
35   interval = base::Microseconds(
36       static_cast<int64_t>(kBackoffMultiplier * interval.InMicroseconds()));
37
38   base::TimeDelta max_interval = base::Hours(kMaxBackoffIntervalHours);
39   if (interval > max_interval || interval.InSeconds() < 0) {
40     interval = max_interval;
41   }
42   return interval;
43 }
44
45 // Time delay after a log is uploaded successfully before attempting another.
46 // On mobile, keeping the radio on is very expensive, so prefer to keep this
47 // short and send in bursts.
48 base::TimeDelta GetUnsentLogsInterval() {
49   return base::Seconds(3);
50 }
51
52 // Initial time delay after a log uploaded fails before retrying it.
53 base::TimeDelta GetInitialBackoffInterval() {
54   return base::Minutes(5);
55 }
56
57 }  // namespace
58
59 MetricsUploadScheduler::MetricsUploadScheduler(
60     const base::RepeatingClosure& upload_callback,
61     bool fast_startup_for_testing)
62     : MetricsScheduler(upload_callback, fast_startup_for_testing),
63       unsent_logs_interval_(GetUnsentLogsInterval()),
64       initial_backoff_interval_(GetInitialBackoffInterval()),
65       backoff_interval_(initial_backoff_interval_) {}
66
67 MetricsUploadScheduler::~MetricsUploadScheduler() {}
68
69 void MetricsUploadScheduler::UploadFinished(bool server_is_healthy) {
70   // If the server is having issues, back off. Otherwise, reset to default
71   // (unless there are more logs to send, in which case the next upload should
72   // happen sooner).
73   if (!server_is_healthy) {
74     TaskDone(backoff_interval_);
75     backoff_interval_ = BackOffUploadInterval(backoff_interval_);
76   } else {
77     backoff_interval_ = initial_backoff_interval_;
78     TaskDone(unsent_logs_interval_);
79   }
80 }
81
82 void MetricsUploadScheduler::StopAndUploadCancelled() {
83   Stop();
84   TaskDone(unsent_logs_interval_);
85 }
86
87 void MetricsUploadScheduler::UploadOverDataUsageCap() {
88   TaskDone(base::Minutes(kOverDataUsageIntervalMinutes));
89 }
90
91 }  // namespace metrics