Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: chrome/browser/metrics/metrics_reporting_scheduler.cc

Issue 6869034: Factor a scheduler object out of MetricsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fix Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 MetricsService* metrics_service)
35 : metrics_service_(metrics_service),
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() {}
jar (doing other things) 2011/04/16 02:03:13 Why bother with a destructor? ...or at best, list
stuartmorgan 2011/04/18 17:03:00 My understanding of http://www.chromium.org/develo
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 metrics_service_->StartScheduledUpload();
84 }
85
86 void MetricsReportingScheduler::ScheduleNextCallback() {
87 DCHECK(running_);
88 if (timer_pending_ || callback_pending_)
89 return;
90
91 // If the metrics service isn't recording and reporting, there's no point in
92 // starting an upload timer.
93 if (!metrics_service_->reporting_active() ||
94 !metrics_service_->recording_active())
jar (doing other things) 2011/04/16 02:03:13 I thought you reduced these two items into a singl
stuartmorgan 2011/04/18 17:03:00 The prior CL eliminate a variable that was essenti
95 return;
96
97 timer_pending_ = true;
98
99 MessageLoop::current()->PostDelayedTask(
100 FROM_HERE,
101 upload_timer_factory_.NewRunnableMethod(
102 &MetricsReportingScheduler::TriggerUpload),
103 upload_interval_.InMilliseconds());
104 }
105
106 void MetricsReportingScheduler::BackOffUploadInterval() {
107 DCHECK(kBackoffMultiplier > 1.0);
108 upload_interval_ = TimeDelta::FromMicroseconds(
109 static_cast<int64>(kBackoffMultiplier *
110 upload_interval_.InMicroseconds()));
111
112 TimeDelta max_interval = kMaxBackoffMultiplier *
113 TimeDelta::FromSeconds(kStandardUploadIntervalSeconds);
114 if (upload_interval_ > max_interval) {
jar (doing other things) 2011/04/16 02:03:13 You may as well be defensive and also check: || up
stuartmorgan 2011/04/18 17:03:00 Done.
115 upload_interval_ = max_interval;
116 }
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698