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 #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 weak_ptr_factory_(this) { | |
51 } | |
52 | |
53 SyncSchedulerImpl::~SyncSchedulerImpl() { | |
54 } | |
55 | |
56 void SyncSchedulerImpl::Start( | |
57 const base::TimeDelta& elapsed_time_since_last_sync, | |
58 Strategy strategy) { | |
59 strategy_ = strategy; | |
60 sync_state_ = SyncState::WAITING_FOR_REFRESH; | |
61 // We reset the failure backoff when the scheduler is started again, as the | |
62 // configuration that caused the previous attempts to fail most likely won't | |
63 // be present after a restart. | |
64 if (strategy_ == Strategy::AGGRESSIVE_RECOVERY) | |
65 failure_count_ = 1; | |
66 | |
67 // To take into account the time waited when the system is powered off, we | |
68 // subtract the time elapsed with a normal sync period to the initial time | |
69 // to wait. | |
70 base::TimeDelta sync_delta = | |
71 GetJitteredPeriod() - elapsed_time_since_last_sync; | |
72 if (sync_delta < base::TimeDelta::FromSeconds(0)) | |
73 sync_delta = base::TimeDelta::FromSeconds(0); | |
74 | |
75 ScheduleNextSync(sync_delta); | |
76 } | |
77 | |
78 void SyncSchedulerImpl::ForceSync() { | |
79 OnTimerFired(); | |
80 } | |
81 | |
82 base::TimeDelta SyncSchedulerImpl::GetTimeToNextSync() const { | |
83 if (!timer_) | |
84 return base::TimeDelta::FromSeconds(0); | |
85 return timer_->GetCurrentDelay(); | |
86 } | |
87 | |
88 SyncScheduler::Strategy SyncSchedulerImpl::GetStrategy() const { | |
89 return strategy_; | |
90 } | |
91 | |
92 SyncScheduler::SyncState SyncSchedulerImpl::GetSyncState() const { | |
93 return sync_state_; | |
94 } | |
95 | |
96 void SyncSchedulerImpl::OnTimerFired() { | |
97 timer_.reset(); | |
98 if (strategy_ == Strategy::PERIODIC_REFRESH) { | |
99 PA_LOG(INFO) << "Timer fired for periodic refresh, making request..."; | |
100 sync_state_ = SyncState::SYNC_IN_PROGRESS; | |
101 } else if (strategy_ == Strategy::AGGRESSIVE_RECOVERY) { | |
102 PA_LOG(INFO) << "Timer fired for aggressive recovery, making request..."; | |
103 sync_state_ = SyncState::SYNC_IN_PROGRESS; | |
104 } else { | |
105 NOTREACHED(); | |
106 return; | |
107 } | |
108 | |
109 delegate_->OnSyncRequested( | |
110 make_scoped_ptr(new SyncRequest(weak_ptr_factory_.GetWeakPtr()))); | |
111 } | |
112 | |
113 scoped_ptr<base::Timer> SyncSchedulerImpl::CreateTimer() { | |
114 bool retain_user_task = false; | |
115 bool is_repeating = false; | |
116 return make_scoped_ptr(new base::Timer(retain_user_task, is_repeating)); | |
117 } | |
118 | |
119 void SyncSchedulerImpl::ScheduleNextSync(const base::TimeDelta& sync_delta) { | |
120 if (sync_state_ != SyncState::WAITING_FOR_REFRESH) { | |
121 PA_LOG(ERROR) << "Unexpected state when scheduling next sync: sync_state=" | |
122 << static_cast<int>(sync_state_); | |
123 return; | |
124 } | |
125 | |
126 bool is_aggressive_recovery = (strategy_ == Strategy::AGGRESSIVE_RECOVERY); | |
127 PA_LOG(INFO) << "Scheduling next sync for " << scheduler_name_ << ":\n" | |
128 << " Strategy: " << (is_aggressive_recovery | |
129 ? "Aggressive Recovery" | |
130 : "Periodic Refresh") << "\n" | |
131 << " Time Delta: " << TimeDeltaToString(sync_delta) | |
132 << (is_aggressive_recovery | |
133 ? base::StringPrintf( | |
134 "\n Previous Failures: %d", | |
135 base::saturated_cast<int>(failure_count_)) | |
136 : ""); | |
137 | |
138 timer_ = CreateTimer(); | |
139 timer_->Start(FROM_HERE, sync_delta, | |
140 base::Bind(&SyncSchedulerImpl::OnTimerFired, | |
141 weak_ptr_factory_.GetWeakPtr())); | |
142 } | |
143 | |
144 void SyncSchedulerImpl::OnSyncCompleted(bool success) { | |
145 if (sync_state_ != SyncState::SYNC_IN_PROGRESS) { | |
146 PA_LOG(ERROR) << "Unexpected state when sync completed: sync_state=" | |
147 << static_cast<int>(sync_state_) | |
148 << ", strategy_=" << static_cast<int>(strategy_); | |
149 return; | |
150 } | |
151 sync_state_ = SyncState::WAITING_FOR_REFRESH; | |
152 | |
153 if (success) { | |
154 strategy_ = Strategy::PERIODIC_REFRESH; | |
155 failure_count_ = 0; | |
156 } else { | |
157 strategy_ = Strategy::AGGRESSIVE_RECOVERY; | |
158 ++failure_count_; | |
159 } | |
160 | |
161 ScheduleNextSync(GetJitteredPeriod()); | |
162 } | |
163 | |
164 base::TimeDelta SyncSchedulerImpl::GetJitteredPeriod() { | |
165 double jitter = 2 * max_jitter_ratio_ * (base::RandDouble() - 0.5); | |
166 base::TimeDelta period = GetPeriod(); | |
167 base::TimeDelta jittered_time_delta = period + (period * jitter); | |
168 if (jittered_time_delta.InMilliseconds() < 0) | |
169 jittered_time_delta = base::TimeDelta::FromMilliseconds(0); | |
170 return jittered_time_delta; | |
171 } | |
172 | |
173 base::TimeDelta SyncSchedulerImpl::GetPeriod() { | |
174 if (strategy_ == Strategy::PERIODIC_REFRESH) { | |
175 return refresh_period_; | |
176 } else if (strategy_ == Strategy::AGGRESSIVE_RECOVERY && failure_count_ > 0) { | |
177 // The backoff for each consecutive failure is exponentially doubled until | |
178 // it is equal to the normal refresh period. | |
179 // Note: |backoff_factor| may evaulate to INF if |failure_count_| is large, | |
180 // but multiplication operations for TimeDelta objects are saturated. | |
181 double backoff_factor = pow(2, failure_count_ - 1); | |
182 base::TimeDelta backoff_period = base_recovery_period_ * backoff_factor; | |
183 return backoff_period < refresh_period_ ? backoff_period : refresh_period_; | |
184 } else { | |
185 PA_LOG(ERROR) << "Error getting period for strategy: " | |
186 << static_cast<int>(strategy_); | |
187 return base::TimeDelta(); | |
188 } | |
189 } | |
190 | |
191 } // namespace proximity_auth | |
OLD | NEW |