OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sync/engine/backoff_delay_provider.h" |
| 6 |
| 7 #include "base/rand_util.h" |
| 8 #include "sync/internal_api/public/engine/polling_constants.h" |
| 9 #include "sync/internal_api/public/sessions/model_neutral_state.h" |
| 10 #include "sync/internal_api/public/util/syncer_error.h" |
| 11 |
| 12 using base::TimeDelta; |
| 13 |
| 14 namespace syncer { |
| 15 |
| 16 // static |
| 17 BackoffDelayProvider* BackoffDelayProvider::FromDefaults() { |
| 18 return new BackoffDelayProvider( |
| 19 TimeDelta::FromSeconds(kInitialBackoffRetrySeconds), |
| 20 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds)); |
| 21 } |
| 22 |
| 23 // static |
| 24 BackoffDelayProvider* BackoffDelayProvider::WithShortInitialRetryOverride() { |
| 25 return new BackoffDelayProvider( |
| 26 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds), |
| 27 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds)); |
| 28 } |
| 29 |
| 30 BackoffDelayProvider::BackoffDelayProvider( |
| 31 const base::TimeDelta& default_initial_backoff, |
| 32 const base::TimeDelta& short_initial_backoff) |
| 33 : default_initial_backoff_(default_initial_backoff), |
| 34 short_initial_backoff_(short_initial_backoff) { |
| 35 } |
| 36 |
| 37 BackoffDelayProvider::~BackoffDelayProvider() {} |
| 38 |
| 39 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { |
| 40 if (last_delay.InSeconds() >= kMaxBackoffSeconds) |
| 41 return TimeDelta::FromSeconds(kMaxBackoffSeconds); |
| 42 |
| 43 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 |
| 44 int64 backoff_s = |
| 45 std::max(static_cast<int64>(1), |
| 46 last_delay.InSeconds() * kBackoffRandomizationFactor); |
| 47 |
| 48 // Flip a coin to randomize backoff interval by +/- 50%. |
| 49 int rand_sign = base::RandInt(0, 1) * 2 - 1; |
| 50 |
| 51 // Truncation is adequate for rounding here. |
| 52 backoff_s = backoff_s + |
| 53 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); |
| 54 |
| 55 // Cap the backoff interval. |
| 56 backoff_s = std::max(static_cast<int64>(1), |
| 57 std::min(backoff_s, kMaxBackoffSeconds)); |
| 58 |
| 59 return TimeDelta::FromSeconds(backoff_s); |
| 60 } |
| 61 |
| 62 TimeDelta BackoffDelayProvider::GetInitialDelay( |
| 63 const sessions::ModelNeutralState& state) const { |
| 64 if (SyncerErrorIsError(state.last_get_key_result)) |
| 65 return default_initial_backoff_; |
| 66 // Note: If we received a MIGRATION_DONE on download updates, then commit |
| 67 // should not have taken place. Moreover, if we receive a MIGRATION_DONE |
| 68 // on commit, it means that download updates succeeded. Therefore, we only |
| 69 // need to check if either code is equal to SERVER_RETURN_MIGRATION_DONE, |
| 70 // and not if there were any more serious errors requiring the long retry. |
| 71 if (state.last_download_updates_result == SERVER_RETURN_MIGRATION_DONE || |
| 72 state.commit_result == SERVER_RETURN_MIGRATION_DONE) { |
| 73 return short_initial_backoff_; |
| 74 } |
| 75 |
| 76 return default_initial_backoff_; |
| 77 } |
| 78 |
| 79 } // namespace syncer |
OLD | NEW |