Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H | |
| 7 | |
| 8 #include "base/timer/timer.h" | |
| 9 #include "components/proximity_auth/cryptauth/sync_scheduler.h" | |
| 10 | |
| 11 namespace proximity_auth { | |
| 12 | |
| 13 // Implementation of SyncScheduler. | |
| 14 class SyncSchedulerImpl : public SyncScheduler { | |
| 15 public: | |
| 16 // Creates the scheduler: | |
| 17 // |delegate|: Handles sync requests and must outlive the scheduler. | |
| 18 // |refresh_period|: The time to wait for the PERIODIC_REFRESH strategy. | |
| 19 // |base_recovery_period|: The initial time to wait for the | |
| 20 // AGGRESSIVE_RECOVERY strategy. The time delta is increased for each | |
| 21 // subsequent failure. | |
| 22 // |scheduler_name|: The name of the scheduler for debugging purposes. | |
| 23 SyncSchedulerImpl(Delegate* delegate, | |
| 24 base::TimeDelta refresh_period, | |
| 25 base::TimeDelta base_recovery_period, | |
| 26 double max_jitter_percentage, | |
| 27 const std::string& scheduler_name); | |
|
Ilya Sherman
2015/05/18 23:25:39
nit: Please document the jitter percentage as well
Tim Song
2015/05/19 22:13:19
Done.
| |
| 28 | |
| 29 ~SyncSchedulerImpl() override; | |
| 30 | |
| 31 // SyncScheduler: | |
| 32 void Start(const base::TimeDelta& elapsed_time_since_last_sync, | |
| 33 bool is_aggressive_recovery) override; | |
|
Ilya Sherman
2015/05/18 23:25:39
Let's use an enum rather than a boolean for is_agg
Tim Song
2015/05/19 22:13:19
Done.
| |
| 34 void ForceSync() override; | |
| 35 base::TimeDelta GetTimeToNextSync() const override; | |
| 36 State GetState() const override; | |
| 37 | |
| 38 protected: | |
| 39 // Creates and returns a base::Timer object. Exposed for testing. | |
| 40 virtual scoped_ptr<base::Timer> CreateTimer(); | |
| 41 | |
| 42 private: | |
| 43 // SyncScheduler: | |
| 44 void OnSyncCompleted(bool success) override; | |
| 45 | |
| 46 // Called when |timer_| is fired. | |
| 47 void OnTimerFired(); | |
| 48 | |
| 49 // Schedules |timer_| for the next sync request. | |
| 50 void ScheduleNextSync(const base::TimeDelta& sync_delta); | |
| 51 | |
| 52 // Randomly adds a random jitter to the value of GetPeriod(). The returned | |
|
Ilya Sherman
2015/05/18 23:25:39
nit: "Randomly adds a random" -> "Adds a random"
Tim Song
2015/05/19 22:13:18
Done.
| |
| 53 // TimeDelta will be clamped to be non-negative. | |
| 54 base::TimeDelta GetJitteredPeriod(); | |
| 55 | |
| 56 // Returns the time to wait for the current strategy. | |
| 57 base::TimeDelta GetPeriod(); | |
| 58 | |
| 59 // The delegate handling sync requests when they are fired. | |
| 60 Delegate* const delegate_; | |
| 61 | |
| 62 // The time to wait until the next refresh when the last sync attempt was | |
| 63 // successful. | |
| 64 const base::TimeDelta refresh_period_; | |
| 65 | |
| 66 // The base recovery period for the AGGRESSIVE_RECOVERY strategy before | |
| 67 // backoffs are applied. | |
| 68 const base::TimeDelta base_recovery_period_; | |
| 69 | |
| 70 // The maximum percentage (both positively and negatively) that the time to | |
| 71 // wait | |
| 72 // between each sync request is jittered. The jitter is randomly applied to | |
| 73 // each | |
| 74 // period so we can avoid deterministic behaviour. | |
|
Ilya Sherman
2015/05/18 23:25:39
nit: Please re-wrap
Ilya Sherman
2015/05/18 23:25:39
nit: We're not really trying to avoid deterministi
Tim Song
2015/05/19 22:13:19
Done.
Tim Song
2015/05/19 22:13:19
Done.
| |
| 75 const double max_jitter_percentage_; | |
| 76 | |
| 77 // The name of the scheduler used for debugging purposes. | |
|
Ilya Sherman
2015/05/18 23:25:39
nit: "scheduler used" -> "scheduler, used"
Tim Song
2015/05/19 22:13:19
Done.
| |
| 78 const std::string scheduler_name_; | |
| 79 | |
| 80 // The current state of the scheduler. | |
| 81 State state_; | |
| 82 | |
| 83 // The number of failed syncs made in a row. Once a sync request succeeds, | |
| 84 // this counter is reset. | |
| 85 int failure_count_; | |
|
Ilya Sherman
2015/05/18 23:25:39
nit: size_t?
Tim Song
2015/05/19 22:13:19
Done.
| |
| 86 | |
| 87 // Timer firing for the next sync request. | |
| 88 scoped_ptr<base::Timer> timer_; | |
| 89 | |
| 90 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); | |
| 93 }; | |
| 94 | |
| 95 } // namespace proximity_auth | |
| 96 | |
| 97 #endif // COMPONENTS_PROXIMITY_CRYPTAUTH_CRYPTAUTH_SYNC_SCHEDULER_IMPL_H | |
| OLD | NEW |