1 // Copyright 2014 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 #ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
10 #include "base/callback.h"
11 #include "base/time/time.h"
12 #include "components/domain_reliability/domain_reliability_export.h"
18 namespace domain_reliability
{
20 class DomainReliabilityConfig
;
23 // Determines when an upload should be scheduled. A domain's config will
24 // specify minimum and maximum upload delays; the minimum upload delay ensures
25 // that Chrome will not send too many upload requests to a site by waiting at
26 // least that long after the first beacon, while the maximum upload delay makes
27 // sure the server receives the reports while they are still fresh.
29 // When everything is working fine, the scheduler will return precisely that
30 // interval. If all uploaders have failed, then the beginning or ending points
31 // of the interval may be pushed later to accomodate the retry with exponential
34 // See dispatcher.h for an explanation of what happens with the scheduled
36 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler
{
38 typedef base::Callback
<void(base::TimeDelta
, base::TimeDelta
)>
39 ScheduleUploadCallback
;
43 base::TimeDelta minimum_upload_delay
;
44 base::TimeDelta maximum_upload_delay
;
45 base::TimeDelta upload_retry_interval
;
47 static Params
GetFromFieldTrialsOrDefaults();
50 DomainReliabilityScheduler(MockableTime
* time
,
51 size_t num_collectors
,
53 const ScheduleUploadCallback
& callback
);
54 ~DomainReliabilityScheduler();
56 // If there is no upload pending, schedules an upload based on the provided
57 // parameters (some time between the minimum and maximum delay from now).
58 // May call the ScheduleUploadCallback.
61 // Returns which collector to use for an upload that is about to start. Must
62 // be called exactly once during or after the ScheduleUploadCallback but
63 // before OnUploadComplete is called. (Also records the upload start time for
64 // future retries, if the upload ends up failing.)
65 size_t OnUploadStart();
67 // Updates the scheduler state based on the result of an upload. Must be
68 // called exactly once after |OnUploadStart|. |success| should be true if the
69 // upload was successful, and false otherwise.
70 void OnUploadComplete(bool success
);
72 base::Value
* GetWebUIData() const;
75 struct CollectorState
{
78 // The number of consecutive failures to upload to this collector, or 0 if
79 // the most recent upload succeeded.
81 base::TimeTicks next_upload
;
84 void MaybeScheduleUpload();
86 void GetNextUploadTimeAndCollector(base::TimeTicks now
,
87 base::TimeTicks
* upload_time_out
,
88 size_t* collector_index_out
);
90 base::TimeDelta
GetUploadRetryInterval(unsigned failures
);
93 std::vector
<CollectorState
> collectors_
;
95 ScheduleUploadCallback callback_
;
97 // Whether there are beacons that have not yet been uploaded. Set when a
98 // beacon arrives or an upload fails, and cleared when an upload starts.
101 // Whether the scheduler has called the ScheduleUploadCallback to schedule
102 // the next upload. Set when an upload is scheduled and cleared when the
104 bool upload_scheduled_
;
106 // Whether the last scheduled upload is in progress. Set when the upload
107 // starts and cleared when the upload completes (successfully or not).
108 bool upload_running_
;
110 // Index of the collector selected for the next upload. (Set in
111 // |OnUploadStart| and cleared in |OnUploadComplete|.)
112 size_t collector_index_
;
114 // Time of the first beacon that was not included in the last successful
116 base::TimeTicks first_beacon_time_
;
118 // first_beacon_time_ saved during uploads. Restored if upload fails.
119 base::TimeTicks old_first_beacon_time_
;
121 // Extra bits to return in GetWebUIData.
122 base::TimeTicks scheduled_min_time_
;
123 base::TimeTicks scheduled_max_time_
;
124 // Whether the other last_upload_* fields are populated.
125 bool last_upload_finished_
;
126 base::TimeTicks last_upload_start_time_
;
127 base::TimeTicks last_upload_end_time_
;
128 size_t last_upload_collector_index_
;
129 bool last_upload_success_
;
132 } // namespace domain_reliability
134 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_