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. 4 5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_ 6 #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_ 7 8 #include <vector> 9 10 #include "base/callback.h" 11 #include "base/time/time.h" 12 #include "components/domain_reliability/domain_reliability_export.h" 13 14 namespace domain_reliability { 15 16 class DomainReliabilityConfig; 17 class MockableTime; 18 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. 24 // 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 28 // backoff. 29 // 30 // See dispatcher.h for an explanation of what happens with the scheduled 31 // interval. 32 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler { 33 public: 34 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> 35 ScheduleUploadCallback; 36 37 struct Params { 38 public: 39 base::TimeDelta minimum_upload_delay; 40 base::TimeDelta maximum_upload_delay; 41 base::TimeDelta upload_retry_interval; 42 43 static Params GetFromFieldTrialsOrDefaults(); 44 }; 45 46 DomainReliabilityScheduler(MockableTime* time, 47 size_t num_collectors, 48 const Params& params, 49 const ScheduleUploadCallback& callback); 50 ~DomainReliabilityScheduler(); 51 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. 55 void OnBeaconAdded(); 56 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(); 62 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); 67 68 private: 69 struct CollectorState { 70 CollectorState(); 71 72 // The number of consecutive failures to upload to this collector, or 0 if 73 // the most recent upload succeeded. 74 unsigned failures; 75 base::TimeTicks next_upload; 76 }; 77 78 void MaybeScheduleUpload(); 79 80 void GetNextUploadTimeAndCollector(base::TimeTicks now, 81 base::TimeTicks* upload_time_out, 82 size_t* collector_index_out); 83 84 base::TimeDelta GetUploadRetryInterval(unsigned failures); 85 86 MockableTime* time_; 87 std::vector<CollectorState> collectors_; 88 Params params_; 89 ScheduleUploadCallback callback_; 90 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. 93 bool upload_pending_; 94 95 // Whether the scheduler has called the ScheduleUploadCallback to schedule 96 // the next upload. Set when an upload is scheduled and cleared when the 97 // upload starts. 98 bool upload_scheduled_; 99 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_; 103 104 // Index of the collector selected for the next upload. (Set in 105 // |OnUploadStart| and cleared in |OnUploadComplete|.) 106 size_t collector_index_; 107 108 // Time of the first beacon that was not included in the last successful 109 // upload. 110 base::TimeTicks first_beacon_time_; 111 112 // first_beacon_time_ saved during uploads. Restored if upload fails. 113 base::TimeTicks old_first_beacon_time_; 114 }; 115 116 } // namespace domain_reliability 117 118 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_ 119