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 // Called when a beacon is added to the context. May call the provided
53 // ScheduleUploadCallback (if all previous beacons have been uploaded).
56 // Called when an upload is about to start.
57 void OnUploadStart(int* collector_index_out
);
59 // Called when an upload has finished. May call the provided
60 // ScheduleUploadCallback (if the upload failed, or if beacons arrived
61 // during the upload).
62 void OnUploadComplete(bool success
);
65 struct CollectorState
{
68 // The number of consecutive failures to upload to this collector, or 0 if
69 // the most recent upload succeeded.
71 base::TimeTicks next_upload
;
74 void MaybeScheduleUpload();
76 void GetNextUploadTimeAndCollector(base::TimeTicks now
,
77 base::TimeTicks
* upload_time_out
,
78 int* collector_index_out
);
79 base::TimeDelta
GetUploadRetryInterval(int failures
);
82 std::vector
<CollectorState
> collectors_
;
84 ScheduleUploadCallback callback_
;
86 // Whether there are beacons that have not yet been uploaded. Set when a
87 // beacon arrives or an upload fails, and cleared when an upload starts.
90 // Whether the scheduler has called the ScheduleUploadCallback to schedule
91 // the next upload. Set when an upload is scheduled and cleared when the
93 bool upload_scheduled_
;
95 // Whether the last scheduled upload is in progress. Set when the upload
96 // starts and cleared when the upload completes (successfully or not).
99 // Index of the collector selected for the next upload. (Set in
100 // |OnUploadStart| and cleared in |OnUploadComplete|.)
101 int collector_index_
;
103 // Time of the first beacon that was not included in the last successful
105 base::TimeTicks first_beacon_time_
;
107 // first_beacon_time_ saved during uploads. Restored if upload fails.
108 base::TimeTicks old_first_beacon_time_
;
111 } // namespace domain_reliability
113 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_