| 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 "chrome/browser/sync/retry_verifier.h" | 5 #include "chrome/browser/sync/retry_verifier.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "sync/internal_api/public/engine/polling_constants.h" | 10 #include "sync/internal_api/public/engine/polling_constants.h" |
| 11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | 11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
| 12 | 12 |
| 13 namespace browser_sync { | 13 namespace browser_sync { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 // Given the current delay calculate the minimum and maximum wait times for | 16 // Given the current delay calculate the minimum and maximum wait times for |
| 17 // the next retry. | 17 // the next retry. |
| 18 DelayInfo CalculateDelay(int64 current_delay) { | 18 DelayInfo CalculateDelay(int64 current_delay) { |
| 19 int64 backoff_s = std::max(static_cast<int64>(1), current_delay * | 19 int64 backoff_s = std::max(static_cast<int64>(1), current_delay * |
| 20 csync::kBackoffRandomizationFactor); | 20 syncer::kBackoffRandomizationFactor); |
| 21 | 21 |
| 22 DelayInfo delay_info; | 22 DelayInfo delay_info; |
| 23 delay_info.min_delay = backoff_s + (-1 * current_delay/ | 23 delay_info.min_delay = backoff_s + (-1 * current_delay/ |
| 24 csync::kBackoffRandomizationFactor); | 24 syncer::kBackoffRandomizationFactor); |
| 25 delay_info.max_delay = backoff_s + current_delay/2; | 25 delay_info.max_delay = backoff_s + current_delay/2; |
| 26 | 26 |
| 27 delay_info.min_delay = std::max(static_cast<int64>(1), | 27 delay_info.min_delay = std::max(static_cast<int64>(1), |
| 28 std::min(delay_info.min_delay, csync::kMaxBackoffSeconds)); | 28 std::min(delay_info.min_delay, syncer::kMaxBackoffSeconds)); |
| 29 | 29 |
| 30 delay_info.max_delay = std::max(static_cast<int64>(1), | 30 delay_info.max_delay = std::max(static_cast<int64>(1), |
| 31 std::min(delay_info.max_delay, csync::kMaxBackoffSeconds)); | 31 std::min(delay_info.max_delay, syncer::kMaxBackoffSeconds)); |
| 32 | 32 |
| 33 return delay_info; | 33 return delay_info; |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Fills the table with the maximum and minimum values for each retry, upto | 36 // Fills the table with the maximum and minimum values for each retry, upto |
| 37 // |count| number of retries. | 37 // |count| number of retries. |
| 38 void FillDelayTable(DelayInfo* delay_table, int count) { | 38 void FillDelayTable(DelayInfo* delay_table, int count) { |
| 39 DCHECK(count > 1); | 39 DCHECK(count > 1); |
| 40 | 40 |
| 41 // We start off with the minimum value of 2 seconds. | 41 // We start off with the minimum value of 2 seconds. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 72 success_(false), | 72 success_(false), |
| 73 done_(false) { | 73 done_(false) { |
| 74 memset(&delay_table_, 0, sizeof(delay_table_)); | 74 memset(&delay_table_, 0, sizeof(delay_table_)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 RetryVerifier::~RetryVerifier() { | 77 RetryVerifier::~RetryVerifier() { |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Initializes the state for verification. | 80 // Initializes the state for verification. |
| 81 void RetryVerifier::Initialize( | 81 void RetryVerifier::Initialize( |
| 82 const csync::sessions::SyncSessionSnapshot& snap) { | 82 const syncer::sessions::SyncSessionSnapshot& snap) { |
| 83 retry_count_ = 0; | 83 retry_count_ = 0; |
| 84 last_sync_time_ = snap.sync_start_time(); | 84 last_sync_time_ = snap.sync_start_time(); |
| 85 FillDelayTable(delay_table_, kMaxRetry); | 85 FillDelayTable(delay_table_, kMaxRetry); |
| 86 done_ = false; | 86 done_ = false; |
| 87 success_ = false; | 87 success_ = false; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void RetryVerifier::VerifyRetryInterval( | 90 void RetryVerifier::VerifyRetryInterval( |
| 91 const csync::sessions::SyncSessionSnapshot& snap) { | 91 const syncer::sessions::SyncSessionSnapshot& snap) { |
| 92 DCHECK(retry_count_ < kMaxRetry); | 92 DCHECK(retry_count_ < kMaxRetry); |
| 93 if (retry_count_ == 0) { | 93 if (retry_count_ == 0) { |
| 94 if (snap.sync_start_time() != last_sync_time_) { | 94 if (snap.sync_start_time() != last_sync_time_) { |
| 95 retry_count_++; | 95 retry_count_++; |
| 96 last_sync_time_ = snap.sync_start_time(); | 96 last_sync_time_ = snap.sync_start_time(); |
| 97 } | 97 } |
| 98 success_ = true; | 98 success_ = true; |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Check if the sync start time has changed. If so indicates a new sync | 102 // Check if the sync start time has changed. If so indicates a new sync |
| 103 // has taken place. | 103 // has taken place. |
| 104 if (snap.sync_start_time() != last_sync_time_) { | 104 if (snap.sync_start_time() != last_sync_time_) { |
| 105 base::TimeDelta delta = snap.sync_start_time() - last_sync_time_; | 105 base::TimeDelta delta = snap.sync_start_time() - last_sync_time_; |
| 106 success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta); | 106 success_ = IsRetryOnTime(delay_table_,retry_count_ -1, delta); |
| 107 last_sync_time_ = snap.sync_start_time(); | 107 last_sync_time_ = snap.sync_start_time(); |
| 108 ++retry_count_; | 108 ++retry_count_; |
| 109 done_ = (retry_count_ >= kMaxRetry); | 109 done_ = (retry_count_ >= kMaxRetry); |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 } // namespace browser_sync | 113 } // namespace browser_sync |
| OLD | NEW |