| 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 <stdint.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "base/rand_util.h" | |
| 12 #include "sync/internal_api/public/engine/polling_constants.h" | |
| 13 #include "sync/internal_api/public/sessions/model_neutral_state.h" | |
| 14 #include "sync/internal_api/public/util/syncer_error.h" | |
| 15 | |
| 16 using base::TimeDelta; | |
| 17 | |
| 18 namespace syncer { | |
| 19 | |
| 20 // static | |
| 21 BackoffDelayProvider* BackoffDelayProvider::FromDefaults() { | |
| 22 return new BackoffDelayProvider( | |
| 23 TimeDelta::FromSeconds(kInitialBackoffRetrySeconds), | |
| 24 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); | |
| 25 } | |
| 26 | |
| 27 // static | |
| 28 BackoffDelayProvider* BackoffDelayProvider::WithShortInitialRetryOverride() { | |
| 29 return new BackoffDelayProvider( | |
| 30 TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds), | |
| 31 TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds)); | |
| 32 } | |
| 33 | |
| 34 BackoffDelayProvider::BackoffDelayProvider( | |
| 35 const base::TimeDelta& default_initial_backoff, | |
| 36 const base::TimeDelta& short_initial_backoff) | |
| 37 : default_initial_backoff_(default_initial_backoff), | |
| 38 short_initial_backoff_(short_initial_backoff) { | |
| 39 } | |
| 40 | |
| 41 BackoffDelayProvider::~BackoffDelayProvider() {} | |
| 42 | |
| 43 TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) { | |
| 44 if (last_delay.InSeconds() >= kMaxBackoffSeconds) | |
| 45 return TimeDelta::FromSeconds(kMaxBackoffSeconds); | |
| 46 | |
| 47 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 | |
| 48 int64_t backoff_s = | |
| 49 std::max(static_cast<int64_t>(1), | |
| 50 last_delay.InSeconds() * kBackoffRandomizationFactor); | |
| 51 | |
| 52 // Flip a coin to randomize backoff interval by +/- 50%. | |
| 53 int rand_sign = base::RandInt(0, 1) * 2 - 1; | |
| 54 | |
| 55 // Truncation is adequate for rounding here. | |
| 56 backoff_s = backoff_s + | |
| 57 (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor)); | |
| 58 | |
| 59 // Cap the backoff interval. | |
| 60 backoff_s = std::max(static_cast<int64_t>(1), | |
| 61 std::min(backoff_s, kMaxBackoffSeconds)); | |
| 62 | |
| 63 return TimeDelta::FromSeconds(backoff_s); | |
| 64 } | |
| 65 | |
| 66 TimeDelta BackoffDelayProvider::GetInitialDelay( | |
| 67 const sessions::ModelNeutralState& state) const { | |
| 68 // NETWORK_CONNECTION_UNAVAILABLE implies we did not receive HTTP response | |
| 69 // from server because of some network error. If network is unavailable then | |
| 70 // on next connection type or address change scheduler will run canary job. | |
| 71 // Otherwise we'll retry in 30 seconds. | |
| 72 if (state.commit_result == NETWORK_CONNECTION_UNAVAILABLE || | |
| 73 state.last_download_updates_result == NETWORK_CONNECTION_UNAVAILABLE) { | |
| 74 return default_initial_backoff_; | |
| 75 } | |
| 76 | |
| 77 if (SyncerErrorIsError(state.last_get_key_result)) | |
| 78 return default_initial_backoff_; | |
| 79 | |
| 80 // Note: If we received a MIGRATION_DONE on download updates, then commit | |
| 81 // should not have taken place. Moreover, if we receive a MIGRATION_DONE | |
| 82 // on commit, it means that download updates succeeded. Therefore, we only | |
| 83 // need to check if either code is equal to SERVER_RETURN_MIGRATION_DONE, | |
| 84 // and not if there were any more serious errors requiring the long retry. | |
| 85 if (state.last_download_updates_result == SERVER_RETURN_MIGRATION_DONE || | |
| 86 state.commit_result == SERVER_RETURN_MIGRATION_DONE) { | |
| 87 return short_initial_backoff_; | |
| 88 } | |
| 89 | |
| 90 // If a datatype decides the GetUpdates must be retried (e.g. because the | |
| 91 // context has been updated since the request), use the short delay. | |
| 92 if (state.last_download_updates_result == DATATYPE_TRIGGERED_RETRY) | |
| 93 return short_initial_backoff_; | |
| 94 | |
| 95 // When the server tells us we have a conflict, then we should download the | |
| 96 // latest updates so we can see the conflict ourselves, resolve it locally, | |
| 97 // then try again to commit. Running another sync cycle will do all these | |
| 98 // things. There's no need to back off, we can do this immediately. | |
| 99 // | |
| 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 | |
| 102 // point. | |
| 103 if (state.commit_result == SERVER_RETURN_CONFLICT) | |
| 104 return short_initial_backoff_; | |
| 105 | |
| 106 return default_initial_backoff_; | |
| 107 } | |
| 108 | |
| 109 } // namespace syncer | |
| OLD | NEW |