| 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 "components/sync/engine_impl/backoff_delay_provider.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "sync/internal_api/public/engine/polling_constants.h" | 12 #include "components/sync/base/syncer_error.h" |
| 13 #include "sync/internal_api/public/sessions/model_neutral_state.h" | 13 #include "components/sync/engine/polling_constants.h" |
| 14 #include "sync/internal_api/public/util/syncer_error.h" | 14 #include "components/sync/sessions/model_neutral_state.h" |
| 15 | 15 |
| 16 using base::TimeDelta; | 16 using base::TimeDelta; |
| 17 | 17 |
| 18 namespace syncer { | 18 namespace syncer { |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 BackoffDelayProvider* BackoffDelayProvider::FromDefaults() { | 21 BackoffDelayProvider* BackoffDelayProvider::FromDefaults() { |
| 22 return new BackoffDelayProvider( | 22 return new BackoffDelayProvider( |
| 23 TimeDelta::FromSeconds(kInitialBackoffRetrySeconds), | 23 TimeDelta::FromSeconds(kInitialBackoffRetrySeconds), |
| 24 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); | 24 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 // static | 27 // static |
| 28 BackoffDelayProvider* BackoffDelayProvider::WithShortInitialRetryOverride() { | 28 BackoffDelayProvider* BackoffDelayProvider::WithShortInitialRetryOverride() { |
| 29 return new BackoffDelayProvider( | 29 return new BackoffDelayProvider( |
| 30 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds), | 30 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds), |
| 31 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); | 31 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 BackoffDelayProvider::BackoffDelayProvider( | 34 BackoffDelayProvider::BackoffDelayProvider( |
| 35 const base::TimeDelta& default_initial_backoff, | 35 const base::TimeDelta& default_initial_backoff, |
| 36 const base::TimeDelta& short_initial_backoff) | 36 const base::TimeDelta& short_initial_backoff) |
| 37 : default_initial_backoff_(default_initial_backoff), | 37 : default_initial_backoff_(default_initial_backoff), |
| 38 short_initial_backoff_(short_initial_backoff) { | 38 short_initial_backoff_(short_initial_backoff) {} |
| 39 } | |
| 40 | 39 |
| 41 BackoffDelayProvider::~BackoffDelayProvider() {} | 40 BackoffDelayProvider::~BackoffDelayProvider() {} |
| 42 | 41 |
| 43 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { | 42 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { |
| 44 if (last_delay.InSeconds() >= kMaxBackoffSeconds) | 43 if (last_delay.InSeconds() >= kMaxBackoffSeconds) |
| 45 return TimeDelta::FromSeconds(kMaxBackoffSeconds); | 44 return TimeDelta::FromSeconds(kMaxBackoffSeconds); |
| 46 | 45 |
| 47 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 | 46 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 |
| 48 int64_t backoff_s = | 47 int64_t backoff_s = |
| 49 std::max(static_cast<int64_t>(1), | 48 std::max(static_cast<int64_t>(1), |
| 50 last_delay.InSeconds() * kBackoffRandomizationFactor); | 49 last_delay.InSeconds() * kBackoffRandomizationFactor); |
| 51 | 50 |
| 52 // Flip a coin to randomize backoff interval by +/- 50%. | 51 // Flip a coin to randomize backoff interval by +/- 50%. |
| 53 int rand_sign = base::RandInt(0, 1) * 2 - 1; | 52 int rand_sign = base::RandInt(0, 1) * 2 - 1; |
| 54 | 53 |
| 55 // Truncation is adequate for rounding here. | 54 // Truncation is adequate for rounding here. |
| 56 backoff_s = backoff_s + | 55 backoff_s = |
| 56 backoff_s + |
| 57 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); | 57 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); |
| 58 | 58 |
| 59 // Cap the backoff interval. | 59 // Cap the backoff interval. |
| 60 backoff_s = std::max(static_cast<int64_t>(1), | 60 backoff_s = std::max(static_cast<int64_t>(1), |
| 61 std::min(backoff_s, kMaxBackoffSeconds)); | 61 std::min(backoff_s, kMaxBackoffSeconds)); |
| 62 | 62 |
| 63 return TimeDelta::FromSeconds(backoff_s); | 63 return TimeDelta::FromSeconds(backoff_s); |
| 64 } | 64 } |
| 65 | 65 |
| 66 TimeDelta BackoffDelayProvider::GetInitialDelay( | 66 TimeDelta BackoffDelayProvider::GetInitialDelay( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider. | 100 // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider. |
| 101 // There should be a way to deal with protocol errors before we get to this | 101 // There should be a way to deal with protocol errors before we get to this |
| 102 // point. | 102 // point. |
| 103 if (state.commit_result == SERVER_RETURN_CONFLICT) | 103 if (state.commit_result == SERVER_RETURN_CONFLICT) |
| 104 return short_initial_backoff_; | 104 return short_initial_backoff_; |
| 105 | 105 |
| 106 return default_initial_backoff_; | 106 return default_initial_backoff_; |
| 107 } | 107 } |
| 108 | 108 |
| 109 } // namespace syncer | 109 } // namespace syncer |
| OLD | NEW |