Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: components/proximity_auth/cryptauth/sync_scheduler_impl.cc

Issue 1147563002: Add SyncScheduler for scheduling CryptAuth enrollments and syncing devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "components/proximity_auth/cryptauth/sync_scheduler_impl.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10
11 #include "base/bind.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/rand_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "components/proximity_auth/logging/logging.h"
16
17 namespace proximity_auth {
18
19 namespace {
20
21 // Returns a human readable string given a |time_delta|.
22 std::string TimeDeltaToString(const base::TimeDelta& time_delta) {
23 if (time_delta.InDays() > 0)
24 return base::StringPrintf("%d days", time_delta.InDays());
25
26 if (time_delta.InHours() > 0)
27 return base::StringPrintf("%d hours", time_delta.InHours());
28
29 if (time_delta.InMinutes() > 0)
30 return base::StringPrintf("%d minutes", time_delta.InMinutes());
31
32 return base::StringPrintf("%d seconds",
33 base::saturated_cast<int>(time_delta.InSeconds()));
34 }
35
36 } // namespace
37
38 SyncSchedulerImpl::SyncSchedulerImpl(Delegate* delegate,
39 base::TimeDelta refresh_period,
40 base::TimeDelta base_recovery_period,
41 double max_jitter_ratio,
42 const std::string& scheduler_name)
43 : delegate_(delegate),
44 refresh_period_(refresh_period),
45 base_recovery_period_(base_recovery_period),
46 max_jitter_ratio_(max_jitter_ratio),
47 scheduler_name_(scheduler_name),
48 strategy_(Strategy::PERIODIC_REFRESH),
49 sync_state_(SyncState::NOT_STARTED),
50 failure_count_(0),
51 weak_ptr_factory_(this) {
52 }
53
54 SyncSchedulerImpl::~SyncSchedulerImpl() {
55 }
56
57 void SyncSchedulerImpl::Start(
58 const base::TimeDelta& elapsed_time_since_last_sync,
59 Strategy strategy) {
60 strategy_ = strategy;
61 sync_state_ = SyncState::WAITING_FOR_REFRESH;
62 // We reset the failure backoff when the scheduler is started again, as the
63 // configuration that caused the previous attempts to fail most likely won't
64 // be present after a restart.
65 if (strategy_ == Strategy::AGGRESSIVE_RECOVERY)
66 failure_count_ = 1;
67
68 // To take into account the time waited when the system is powered off, we
69 // subtract the time elapsed with a normal sync period to the initial time
70 // to wait.
71 base::TimeDelta sync_delta =
72 GetJitteredPeriod() - elapsed_time_since_last_sync;
73 if (sync_delta < base::TimeDelta::FromSeconds(0))
74 sync_delta = base::TimeDelta::FromSeconds(0);
75
76 ScheduleNextSync(sync_delta);
77 }
78
79 void SyncSchedulerImpl::ForceSync() {
80 OnTimerFired();
81 }
82
83 base::TimeDelta SyncSchedulerImpl::GetTimeToNextSync() const {
84 if (!timer_)
85 return base::TimeDelta::FromSeconds(0);
86 return timer_->GetCurrentDelay();
87 }
88
89 SyncScheduler::Strategy SyncSchedulerImpl::GetStrategy() const {
90 return strategy_;
91 }
92
93 SyncScheduler::SyncState SyncSchedulerImpl::GetSyncState() const {
94 return sync_state_;
95 }
96
97 void SyncSchedulerImpl::OnTimerFired() {
98 timer_.reset();
99 if (strategy_ == Strategy::PERIODIC_REFRESH) {
100 PA_LOG(INFO) << "Timer fired for periodic refresh, making request...";
101 sync_state_ = SyncState::SYNC_IN_PROGRESS;
102 } else if (strategy_ == Strategy::AGGRESSIVE_RECOVERY) {
103 PA_LOG(INFO) << "Timer fired for aggressive recovery, making request...";
104 sync_state_ = SyncState::SYNC_IN_PROGRESS;
105 } else {
106 NOTREACHED();
107 return;
108 }
109
110 delegate_->OnSyncRequested(
111 make_scoped_ptr(new SyncRequest(weak_ptr_factory_.GetWeakPtr())));
112 }
113
114 scoped_ptr<base::Timer> SyncSchedulerImpl::CreateTimer() {
115 bool retain_user_task = false;
116 bool is_repeating = false;
117 return make_scoped_ptr(new base::Timer(retain_user_task, is_repeating));
118 }
119
120 void SyncSchedulerImpl::ScheduleNextSync(const base::TimeDelta& sync_delta) {
121 if (sync_state_ != SyncState::WAITING_FOR_REFRESH) {
122 PA_LOG(ERROR) << "Unexpected state when scheduling next sync: sync_state="
123 << static_cast<int>(sync_state_);
124 return;
125 }
126
127 bool is_aggressive_recovery = (strategy_ == Strategy::AGGRESSIVE_RECOVERY);
128 PA_LOG(INFO) << "Scheduling next sync for " << scheduler_name_ << ":\n"
129 << " Strategy: " << (is_aggressive_recovery
130 ? "Aggressive Recovery"
131 : "Periodic Refresh") << "\n"
132 << " Time Delta: " << TimeDeltaToString(sync_delta)
133 << (is_aggressive_recovery
134 ? base::StringPrintf(
135 "\n Previous Failures: %d",
136 base::saturated_cast<int>(failure_count_))
137 : "");
138
139 timer_ = CreateTimer();
140 timer_->Start(FROM_HERE, sync_delta,
141 base::Bind(&SyncSchedulerImpl::OnTimerFired,
142 weak_ptr_factory_.GetWeakPtr()));
143 }
144
145 void SyncSchedulerImpl::OnSyncCompleted(bool success) {
146 if (sync_state_ != SyncState::SYNC_IN_PROGRESS) {
147 PA_LOG(ERROR) << "Unexpected state when sync completed: sync_state="
148 << static_cast<int>(sync_state_)
149 << ", strategy_=" << static_cast<int>(strategy_);
150 return;
151 }
152 sync_state_ = SyncState::WAITING_FOR_REFRESH;
153
154 if (success) {
155 strategy_ = Strategy::PERIODIC_REFRESH;
156 failure_count_ = 0;
157 } else {
158 strategy_ = Strategy::AGGRESSIVE_RECOVERY;
159 ++failure_count_;
160 }
161
162 ScheduleNextSync(GetJitteredPeriod());
163 }
164
165 base::TimeDelta SyncSchedulerImpl::GetJitteredPeriod() {
166 double jitter = 2 * max_jitter_ratio_ * (base::RandDouble() - 0.5);
167 base::TimeDelta period = GetPeriod();
168 base::TimeDelta jittered_time_delta = period + (period * jitter);
169 if (jittered_time_delta.InMilliseconds() < 0)
170 jittered_time_delta = base::TimeDelta::FromMilliseconds(0);
171 return jittered_time_delta;
172 }
173
174 base::TimeDelta SyncSchedulerImpl::GetPeriod() {
175 if (strategy_ == Strategy::PERIODIC_REFRESH) {
176 return refresh_period_;
177 } else if (strategy_ == Strategy::AGGRESSIVE_RECOVERY && failure_count_ > 0) {
178 // The backoff for each consecutive failure is exponentially doubled until
179 // it is equal to the normal refresh period.
180 // Note: |backoff_factor| may evaulate to INF if |failure_count_| is large,
181 // but multiplication operations for TimeDelta objects are saturated.
182 double backoff_factor = pow(2, failure_count_ - 1);
183 base::TimeDelta backoff_period = base_recovery_period_ * backoff_factor;
184 return backoff_period < refresh_period_ ? backoff_period : refresh_period_;
185 } else {
186 PA_LOG(ERROR) << "Error getting period for strategy: "
187 << static_cast<int>(strategy_);
188 return base::TimeDelta();
189 }
190 }
191
192 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698