OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/engine/backoff_delay_provider.h" | 5 #include "sync/engine/backoff_delay_provider.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <algorithm> | 9 #include <algorithm> |
8 | 10 |
9 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
10 #include "sync/internal_api/public/engine/polling_constants.h" | 12 #include "sync/internal_api/public/engine/polling_constants.h" |
11 #include "sync/internal_api/public/sessions/model_neutral_state.h" | 13 #include "sync/internal_api/public/sessions/model_neutral_state.h" |
12 #include "sync/internal_api/public/util/syncer_error.h" | 14 #include "sync/internal_api/public/util/syncer_error.h" |
13 | 15 |
14 using base::TimeDelta; | 16 using base::TimeDelta; |
15 | 17 |
16 namespace syncer { | 18 namespace syncer { |
(...skipping 19 matching lines...) Expand all Loading... |
36 short_initial_backoff_(short_initial_backoff) { | 38 short_initial_backoff_(short_initial_backoff) { |
37 } | 39 } |
38 | 40 |
39 BackoffDelayProvider::~BackoffDelayProvider() {} | 41 BackoffDelayProvider::~BackoffDelayProvider() {} |
40 | 42 |
41 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { | 43 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { |
42 if (last_delay.InSeconds() >= kMaxBackoffSeconds) | 44 if (last_delay.InSeconds() >= kMaxBackoffSeconds) |
43 return TimeDelta::FromSeconds(kMaxBackoffSeconds); | 45 return TimeDelta::FromSeconds(kMaxBackoffSeconds); |
44 | 46 |
45 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 | 47 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 |
46 int64 backoff_s = | 48 int64_t backoff_s = |
47 std::max(static_cast<int64>(1), | 49 std::max(static_cast<int64_t>(1), |
48 last_delay.InSeconds() * kBackoffRandomizationFactor); | 50 last_delay.InSeconds() * kBackoffRandomizationFactor); |
49 | 51 |
50 // Flip a coin to randomize backoff interval by +/- 50%. | 52 // Flip a coin to randomize backoff interval by +/- 50%. |
51 int rand_sign = base::RandInt(0, 1) * 2 - 1; | 53 int rand_sign = base::RandInt(0, 1) * 2 - 1; |
52 | 54 |
53 // Truncation is adequate for rounding here. | 55 // Truncation is adequate for rounding here. |
54 backoff_s = backoff_s + | 56 backoff_s = backoff_s + |
55 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); | 57 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); |
56 | 58 |
57 // Cap the backoff interval. | 59 // Cap the backoff interval. |
58 backoff_s = std::max(static_cast<int64>(1), | 60 backoff_s = std::max(static_cast<int64_t>(1), |
59 std::min(backoff_s, kMaxBackoffSeconds)); | 61 std::min(backoff_s, kMaxBackoffSeconds)); |
60 | 62 |
61 return TimeDelta::FromSeconds(backoff_s); | 63 return TimeDelta::FromSeconds(backoff_s); |
62 } | 64 } |
63 | 65 |
64 TimeDelta BackoffDelayProvider::GetInitialDelay( | 66 TimeDelta BackoffDelayProvider::GetInitialDelay( |
65 const sessions::ModelNeutralState& state) const { | 67 const sessions::ModelNeutralState& state) const { |
66 // NETWORK_CONNECTION_UNAVAILABLE implies we did not even manage to hit the | 68 // NETWORK_CONNECTION_UNAVAILABLE implies we did not even manage to hit the |
67 // wire; the failure occurred locally. Note that if commit_result is *not* | 69 // wire; the failure occurred locally. Note that if commit_result is *not* |
68 // UNSET, this implies download_updates_result succeeded. Also note that | 70 // UNSET, this implies download_updates_result succeeded. Also note that |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider. | 114 // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider. |
113 // There should be a way to deal with protocol errors before we get to this | 115 // There should be a way to deal with protocol errors before we get to this |
114 // point. | 116 // point. |
115 if (state.commit_result == SERVER_RETURN_CONFLICT) | 117 if (state.commit_result == SERVER_RETURN_CONFLICT) |
116 return short_initial_backoff_; | 118 return short_initial_backoff_; |
117 | 119 |
118 return default_initial_backoff_; | 120 return default_initial_backoff_; |
119 } | 121 } |
120 | 122 |
121 } // namespace syncer | 123 } // namespace syncer |
OLD | NEW |