OLD | NEW |
---|---|
(Empty) | |
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 #include "components/password_manager/core/browser/affiliation_fetch_throttler.h " | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/rand_util.h" | |
9 #include "base/thread_task_runner_handle.h" | |
10 #include "base/time/tick_clock.h" | |
11 #include "base/time/time.h" | |
12 | |
13 namespace password_manager { | |
14 | |
15 namespace { | |
16 | |
17 net::BackoffEntry::Policy kAffiliationBackoffParameters = { | |
mmenke
2015/01/14 19:25:07
const
engedy
2015/01/15 19:16:42
Done.
| |
18 // Number of initial errors (in sequence) to ignore before going into | |
19 // exponential backoff. | |
20 0, | |
21 | |
22 // Initial delay (in ms) once backoff starts. | |
23 10 * 1000, // 10 seconds | |
24 | |
25 // Factor by which the delay will be multiplied on each subsequent failure. | |
26 4, | |
27 | |
28 // Fuzzing percentage: 50% will spread delays randomly between 50%--100% of | |
29 // the nominal time. | |
30 .5, // 50% | |
31 | |
32 // Maximum delay (in ms) during exponential backoff. | |
33 6 * 3600 * 1000, // 6 hours | |
34 | |
35 // Time to keep an entry from being discarded even when it has no | |
36 // significant state, -1 to never discard. (Not applicable.) | |
37 -1, | |
38 | |
39 // False means that initial_delay_ms is the first delay once we start | |
40 // exponential backoff, i.e., there is no delay after subsequent successful | |
41 // requests. | |
42 false, | |
43 }; | |
44 | |
45 // Grace period before the first request is sent after network connectivity is | |
46 // restored. The fuzzing factor from above applies. | |
47 const int64 kGracePeriodAfterNetworkRestoredInMs = 10 * 1000; // 10 seconds | |
mmenke
2015/01/14 19:25:07
Should include basictypes.h for int64
engedy
2015/01/15 19:16:42
Ahh, it looks like that is already deprecated. I h
| |
48 | |
49 // Implementation of net::BackoffEntry that allows mocking its tick source. | |
50 class BackoffEntryImpl : public net::BackoffEntry { | |
51 public: | |
52 BackoffEntryImpl(base::TickClock* tick_clock) | |
mmenke
2015/01/14 19:25:07
explicit
engedy
2015/01/15 19:16:42
Done.
| |
53 : BackoffEntry(&kAffiliationBackoffParameters), tick_clock_(tick_clock) {} | |
54 ~BackoffEntryImpl() override{}; | |
mmenke
2015/01/14 19:25:07
nit: space after "override"
engedy
2015/01/15 19:16:42
Ahh, actually, there is also a superfluous semi-co
| |
55 | |
56 protected: | |
57 // net::BackoffEntry: | |
58 base::TimeTicks ImplGetTimeNow() const override { | |
mmenke
2015/01/14 19:25:06
optional: This can be private (parent classes can
engedy
2015/01/15 19:16:42
Done.
| |
59 return tick_clock_->NowTicks(); | |
60 } | |
61 | |
62 private: | |
63 // Must outlive this instance. | |
64 base::TickClock* tick_clock_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(BackoffEntryImpl); | |
67 }; | |
68 | |
69 } // namespace | |
70 | |
71 AffiliationFetchThrottler::AffiliationFetchThrottler( | |
72 AffiliationFetchThrottlerDelegate* delegate, | |
73 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
74 scoped_ptr<base::TickClock> tick_clock) | |
75 : delegate_(delegate), | |
76 task_runner_(task_runner), | |
77 state_(IDLE), | |
78 has_network_connectivity_(false), | |
79 is_fetch_scheduled_(false), | |
80 tick_clock_(tick_clock.Pass()), | |
81 exponential_backoff_(new BackoffEntryImpl(tick_clock_.get())), | |
82 weak_ptr_factory_(this) { | |
83 DCHECK(delegate); | |
84 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
mmenke
2015/01/14 19:25:06
Putting this in a constructor seems like a bad ide
engedy
2015/01/15 19:16:42
I think that is a bit over-cautious. The contract
| |
85 has_network_connectivity_ = !net::NetworkChangeNotifier::IsOffline(); | |
86 } | |
87 | |
88 AffiliationFetchThrottler::~AffiliationFetchThrottler() { | |
89 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
90 } | |
91 | |
92 void AffiliationFetchThrottler::SignalNetworkRequestNeeded() { | |
93 if (state_ != IDLE) | |
94 return; | |
95 | |
96 state_ = FETCH_NEEDED; | |
97 if (has_network_connectivity_) | |
98 EnsureCallbackIsScheduled(); | |
99 } | |
100 | |
101 void AffiliationFetchThrottler::InformOfNetworkRequestComplete(bool success) { | |
102 DCHECK_EQ(state_, FETCH_IN_FLIGHT); | |
103 state_ = IDLE; | |
104 exponential_backoff_->InformOfRequest(success); | |
mmenke
2015/01/14 20:43:43
Oops...Got so sidetracked thinking about the NCN s
engedy
2015/01/15 19:16:42
Please see my other comment.
| |
105 } | |
106 | |
107 void AffiliationFetchThrottler::EnsureCallbackIsScheduled() { | |
108 DCHECK_EQ(state_, FETCH_NEEDED); | |
109 if (is_fetch_scheduled_) | |
110 return; | |
111 | |
112 is_fetch_scheduled_ = true; | |
113 task_runner_->PostDelayedTask( | |
114 FROM_HERE, | |
115 base::Bind(&AffiliationFetchThrottler::OnBackoffDelayExpiredCallback, | |
116 weak_ptr_factory_.GetWeakPtr()), | |
117 exponential_backoff_->GetTimeUntilRelease()); | |
118 } | |
119 | |
120 void AffiliationFetchThrottler::OnBackoffDelayExpiredCallback() { | |
121 DCHECK_EQ(state_, FETCH_NEEDED); | |
122 DCHECK(is_fetch_scheduled_); | |
123 is_fetch_scheduled_ = false; | |
124 | |
125 // Do nothing if network connectivity was lost while this callback was in the | |
126 // task queue. The callback will be posted in the OnConnectionTypeChanged | |
127 // handler once again. | |
128 if (!has_network_connectivity_) | |
129 return; | |
130 | |
131 // The release time might have been increased if network connectivity was lost | |
132 // and restored while this callback was in the task queue. If so, reschedule. | |
133 if (exponential_backoff_->ShouldRejectRequest()) | |
134 EnsureCallbackIsScheduled(); | |
135 else | |
136 state_ = delegate_->OnCanSendNetworkRequest() ? FETCH_IN_FLIGHT : IDLE; | |
137 } | |
138 | |
139 void AffiliationFetchThrottler::OnConnectionTypeChanged( | |
140 net::NetworkChangeNotifier::ConnectionType type) { | |
141 if (has_network_connectivity_ == !net::NetworkChangeNotifier::IsOffline()) | |
mmenke
2015/01/14 19:25:06
What if we're switching from WIFI to cellular?
mmenke
2015/01/14 19:25:07
Shouldn't call NetworkChangeNotifier::IsOffline tw
engedy
2015/01/15 19:16:42
Done.
engedy
2015/01/15 19:16:42
Hmm. So far I did not consider the kind of the con
mmenke
2015/01/16 21:28:34
Now that I realize how this class actually works,
| |
142 return; | |
143 | |
144 has_network_connectivity_ = !net::NetworkChangeNotifier::IsOffline(); | |
145 if (!has_network_connectivity_) | |
146 return; | |
mmenke
2015/01/14 19:25:06
While being offline is one of the most common caus
mmenke
2015/01/14 19:25:07
No provisions to retry here?
engedy
2015/01/15 19:16:42
I am not sure I understand. Are you implying that
engedy
2015/01/15 19:16:42
Hmm. I believe this is somewhat different than net
mmenke
2015/01/16 21:28:34
Sorry, got confused about how this would plug in (
| |
147 | |
148 double grace_ms = | |
149 kGracePeriodAfterNetworkRestoredInMs * | |
150 (1 - base::RandDouble() * kAffiliationBackoffParameters.jitter_factor); | |
151 base::TimeTicks release_time = std::max( | |
152 exponential_backoff_->GetReleaseTime(), | |
153 tick_clock_->NowTicks() + base::TimeDelta::FromMillisecondsD(grace_ms)); | |
154 // Note that SetCustomReleaseTime() takes the maximum of the current release | |
155 // time and the supplied |release_time|. | |
156 exponential_backoff_->SetCustomReleaseTime(release_time); | |
157 | |
158 if (state_ == FETCH_NEEDED) | |
159 EnsureCallbackIsScheduled(); | |
160 } | |
161 | |
162 } // namespace password_manager | |
OLD | NEW |