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"
14 namespace domain_reliability
{
16 class DomainReliabilityConfig
;
19 // Determines when an upload should be scheduled. A domain's config will
20 // specify minimum and maximum upload delays; the minimum upload delay ensures
21 // that Chrome will not send too many upload requests to a site by waiting at
22 // least that long after the first beacon, while the maximum upload delay makes
23 // sure the server receives the reports while they are still fresh.
25 // When everything is working fine, the scheduler will return precisely that
26 // interval. If all uploaders have failed, then the beginning or ending points
27 // of the interval may be pushed later to accomodate the retry with exponential
30 // See dispatcher.h for an explanation of what happens with the scheduled
32 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler
{
34 typedef base::Callback
<void(base::TimeDelta
, base::TimeDelta
)>
35 ScheduleUploadCallback
;
39 base::TimeDelta minimum_upload_delay
;
40 base::TimeDelta maximum_upload_delay
;
41 base::TimeDelta upload_retry_interval
;
43 static Params
GetFromFieldTrialsOrDefaults();
46 DomainReliabilityScheduler(MockableTime
* time
,
47 size_t num_collectors
,
49 const ScheduleUploadCallback
& callback
);
50 ~DomainReliabilityScheduler();
52 // If there is no upload pending, schedules an upload based on the provided
53 // parameters (some time between the minimum and maximum delay from now).
54 // May call the ScheduleUploadCallback.
57 // Returns which collector to use for an upload that is about to start. Must
58 // be called exactly once during or after the ScheduleUploadCallback but
59 // before OnUploadComplete is called. (Also records the upload start time for
60 // future retries, if the upload ends up failing.)
61 size_t OnUploadStart();
63 // Updates the scheduler state based on the result of an upload. Must be
64 // called exactly once after |OnUploadStart|. |success| should be true if the
65 // upload was successful, and false otherwise.
66 void OnUploadComplete(bool success
);
69 struct CollectorState
{
72 // The number of consecutive failures to upload to this collector, or 0 if
73 // the most recent upload succeeded.
75 base::TimeTicks next_upload
;
78 void MaybeScheduleUpload();
80 void GetNextUploadTimeAndCollector(base::TimeTicks now
,
81 base::TimeTicks
* upload_time_out
,
82 size_t* collector_index_out
);
84 base::TimeDelta
GetUploadRetryInterval(unsigned failures
);
87 std::vector
<CollectorState
> collectors_
;
89 ScheduleUploadCallback callback_
;
91 // Whether there are beacons that have not yet been uploaded. Set when a
92 // beacon arrives or an upload fails, and cleared when an upload starts.
95 // Whether the scheduler has called the ScheduleUploadCallback to schedule
96 // the next upload. Set when an upload is scheduled and cleared when the
98 bool upload_scheduled_
;
100 // Whether the last scheduled upload is in progress. Set when the upload
101 // starts and cleared when the upload completes (successfully or not).
102 bool upload_running_
;
104 // Index of the collector selected for the next upload. (Set in
105 // |OnUploadStart| and cleared in |OnUploadComplete|.)
106 size_t collector_index_
;
108 // Time of the first beacon that was not included in the last successful
110 base::TimeTicks first_beacon_time_
;
112 // first_beacon_time_ saved during uploads. Restored if upload fails.
113 base::TimeTicks old_first_beacon_time_
;
116 } // namespace domain_reliability
118 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_