scheduler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
  5. #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "components/domain_reliability/domain_reliability_export.h"
  13. #include "components/domain_reliability/uploader.h"
  14. #include "net/base/backoff_entry.h"
  15. namespace domain_reliability {
  16. class MockableTime;
  17. // Determines when an upload should be scheduled. A domain's config will
  18. // specify minimum and maximum upload delays; the minimum upload delay ensures
  19. // that Chrome will not send too many upload requests to a site by waiting at
  20. // least that long after the first beacon, while the maximum upload delay makes
  21. // sure the server receives the reports while they are still fresh.
  22. //
  23. // When everything is working fine, the scheduler will return precisely that
  24. // interval. If all uploaders have failed, then the beginning or ending points
  25. // of the interval may be pushed later to accomodate the retry with exponential
  26. // backoff.
  27. //
  28. // See dispatcher.h for an explanation of what happens with the scheduled
  29. // interval.
  30. class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
  31. public:
  32. typedef base::RepeatingCallback<void(base::TimeDelta, base::TimeDelta)>
  33. ScheduleUploadCallback;
  34. struct Params {
  35. public:
  36. base::TimeDelta minimum_upload_delay;
  37. base::TimeDelta maximum_upload_delay;
  38. base::TimeDelta upload_retry_interval;
  39. static Params GetFromFieldTrialsOrDefaults();
  40. };
  41. DomainReliabilityScheduler(const MockableTime* time,
  42. size_t num_collectors,
  43. const Params& params,
  44. const ScheduleUploadCallback& callback);
  45. DomainReliabilityScheduler(const DomainReliabilityScheduler&) = delete;
  46. DomainReliabilityScheduler& operator=(const DomainReliabilityScheduler&) =
  47. delete;
  48. ~DomainReliabilityScheduler();
  49. // If there is no upload pending, schedules an upload based on the provided
  50. // parameters (some time between the minimum and maximum delay from now).
  51. // May call the ScheduleUploadCallback.
  52. void OnBeaconAdded();
  53. // Returns which collector to use for an upload that is about to start. Must
  54. // be called exactly once during or after the ScheduleUploadCallback but
  55. // before OnUploadComplete is called. (Also records the upload start time for
  56. // future retries, if the upload ends up failing.)
  57. size_t OnUploadStart();
  58. // Updates the scheduler state based on the result of an upload. Must be
  59. // called exactly once after |OnUploadStart|. |result| should be the result
  60. // passed to the upload callback by the Uploader.
  61. void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
  62. // Disables jitter in BackoffEntries to make scheduling deterministic for
  63. // unit tests.
  64. void MakeDeterministicForTesting();
  65. private:
  66. void MaybeScheduleUpload();
  67. void GetNextUploadTimeAndCollector(base::TimeTicks now,
  68. base::TimeTicks* upload_time_out,
  69. size_t* collector_index_out);
  70. raw_ptr<const MockableTime> time_;
  71. Params params_;
  72. ScheduleUploadCallback callback_;
  73. net::BackoffEntry::Policy backoff_policy_;
  74. std::vector<std::unique_ptr<net::BackoffEntry>> collectors_;
  75. // Whether there are beacons that have not yet been uploaded. Set when a
  76. // beacon arrives or an upload fails, and cleared when an upload starts.
  77. bool upload_pending_;
  78. // Whether the scheduler has called the ScheduleUploadCallback to schedule
  79. // the next upload. Set when an upload is scheduled and cleared when the
  80. // upload starts.
  81. bool upload_scheduled_;
  82. // Whether the last scheduled upload is in progress. Set when the upload
  83. // starts and cleared when the upload completes (successfully or not).
  84. bool upload_running_;
  85. // Index of the collector selected for the next upload. (Set in
  86. // |OnUploadStart| and cleared in |OnUploadComplete|.)
  87. size_t collector_index_;
  88. // Time of the first beacon that was not included in the last successful
  89. // upload.
  90. base::TimeTicks first_beacon_time_;
  91. // first_beacon_time_ saved during uploads. Restored if upload fails.
  92. base::TimeTicks old_first_beacon_time_;
  93. };
  94. } // namespace domain_reliability
  95. #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_