backoff_delay_provider.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "components/sync/engine/backoff_delay_provider.h"
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include "base/memory/ptr_util.h"
  8. #include "base/rand_util.h"
  9. #include "components/sync/base/syncer_error.h"
  10. #include "components/sync/engine/cycle/model_neutral_state.h"
  11. #include "components/sync/engine/polling_constants.h"
  12. namespace syncer {
  13. namespace {
  14. // This calculates approx. last_delay * kBackoffMultiplyFactor +/- last_delay
  15. // * kBackoffJitterFactor. |jitter_sign| must be -1 or 1 and determines whether
  16. // the jitter in the delay will be positive or negative.
  17. base::TimeDelta GetDelayImpl(base::TimeDelta last_delay, int jitter_sign) {
  18. DCHECK(jitter_sign == -1 || jitter_sign == 1);
  19. if (last_delay >= kMaxBackoffTime)
  20. return kMaxBackoffTime;
  21. const base::TimeDelta backoff =
  22. std::max(kMinBackoffTime, last_delay * kBackoffMultiplyFactor) +
  23. jitter_sign * kBackoffJitterFactor * last_delay;
  24. // Clamp backoff between 1 second and |kMaxBackoffTime|.
  25. return std::max(kMinBackoffTime, std::min(backoff, kMaxBackoffTime));
  26. }
  27. } // namespace
  28. // static
  29. std::unique_ptr<BackoffDelayProvider> BackoffDelayProvider::FromDefaults() {
  30. // base::WrapUnique() used because the constructor is private.
  31. return base::WrapUnique(new BackoffDelayProvider(
  32. kInitialBackoffRetryTime, kInitialBackoffImmediateRetryTime));
  33. }
  34. // static
  35. std::unique_ptr<BackoffDelayProvider>
  36. BackoffDelayProvider::WithShortInitialRetryOverride() {
  37. // base::WrapUnique() used because the constructor is private.
  38. return base::WrapUnique(new BackoffDelayProvider(
  39. kInitialBackoffShortRetryTime, kInitialBackoffImmediateRetryTime));
  40. }
  41. BackoffDelayProvider::BackoffDelayProvider(
  42. const base::TimeDelta& default_initial_backoff,
  43. const base::TimeDelta& short_initial_backoff)
  44. : default_initial_backoff_(default_initial_backoff),
  45. short_initial_backoff_(short_initial_backoff) {}
  46. BackoffDelayProvider::~BackoffDelayProvider() = default;
  47. base::TimeDelta BackoffDelayProvider::GetDelay(
  48. const base::TimeDelta& last_delay) {
  49. // Flip a coin to randomize backoff interval by +/- kBackoffJitterFactor.
  50. const int jitter_sign = base::RandInt(0, 1) * 2 - 1;
  51. return GetDelayImpl(last_delay, jitter_sign);
  52. }
  53. base::TimeDelta BackoffDelayProvider::GetInitialDelay(
  54. const ModelNeutralState& state) const {
  55. // NETWORK_CONNECTION_UNAVAILABLE implies we did not receive HTTP response
  56. // from server because of some network error. If network is unavailable then
  57. // on next connection type or address change scheduler will run canary job.
  58. // Otherwise we'll retry in 30 seconds.
  59. if (state.commit_result.value() ==
  60. SyncerError::NETWORK_CONNECTION_UNAVAILABLE ||
  61. state.last_download_updates_result.value() ==
  62. SyncerError::NETWORK_CONNECTION_UNAVAILABLE) {
  63. return default_initial_backoff_;
  64. }
  65. if (state.last_get_key_result.IsActualError())
  66. return default_initial_backoff_;
  67. // Note: If we received a MIGRATION_DONE on download updates, then commit
  68. // should not have taken place. Moreover, if we receive a MIGRATION_DONE
  69. // on commit, it means that download updates succeeded. Therefore, we only
  70. // need to check if either code is equal to SERVER_RETURN_MIGRATION_DONE,
  71. // and not if there were any more serious errors requiring the long retry.
  72. if (state.last_download_updates_result.value() ==
  73. SyncerError::SERVER_RETURN_MIGRATION_DONE ||
  74. state.commit_result.value() ==
  75. SyncerError::SERVER_RETURN_MIGRATION_DONE) {
  76. return short_initial_backoff_;
  77. }
  78. // If a datatype decides the GetUpdates must be retried (e.g. because the
  79. // context has been updated since the request), use the short delay.
  80. if (state.last_download_updates_result.value() ==
  81. SyncerError::DATATYPE_TRIGGERED_RETRY)
  82. return short_initial_backoff_;
  83. // When the server tells us we have a conflict, then we should download the
  84. // latest updates so we can see the conflict ourselves, resolve it locally,
  85. // then try again to commit. Running another sync cycle will do all these
  86. // things. There's no need to back off, we can do this immediately.
  87. //
  88. // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider.
  89. // There should be a way to deal with protocol errors before we get to this
  90. // point.
  91. if (state.commit_result.value() == SyncerError::SERVER_RETURN_CONFLICT)
  92. return short_initial_backoff_;
  93. return default_initial_backoff_;
  94. }
  95. base::TimeDelta BackoffDelayProvider::GetDelayForTesting(
  96. base::TimeDelta last_delay,
  97. int jitter_sign) {
  98. return GetDelayImpl(last_delay, jitter_sign);
  99. }
  100. } // namespace syncer