This source file includes following definitions.
- FromDefaults
- WithShortInitialRetryOverride
- short_initial_backoff_
- GetDelay
- GetInitialDelay
#include "sync/engine/backoff_delay_provider.h"
#include "base/rand_util.h"
#include "sync/internal_api/public/engine/polling_constants.h"
#include "sync/internal_api/public/sessions/model_neutral_state.h"
#include "sync/internal_api/public/util/syncer_error.h"
using base::TimeDelta;
namespace syncer {
BackoffDelayProvider* BackoffDelayProvider::FromDefaults() {
return new BackoffDelayProvider(
TimeDelta::FromSeconds(kInitialBackoffRetrySeconds),
TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds));
}
BackoffDelayProvider* BackoffDelayProvider::WithShortInitialRetryOverride() {
return new BackoffDelayProvider(
TimeDelta::FromSeconds(kInitialBackoffShortRetrySeconds),
TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds));
}
BackoffDelayProvider::BackoffDelayProvider(
const base::TimeDelta& default_initial_backoff,
const base::TimeDelta& short_initial_backoff)
: default_initial_backoff_(default_initial_backoff),
short_initial_backoff_(short_initial_backoff) {
}
BackoffDelayProvider::~BackoffDelayProvider() {}
TimeDelta BackoffDelayProvider::GetDelay(const base::TimeDelta& last_delay) {
if (last_delay.InSeconds() >= kMaxBackoffSeconds)
return TimeDelta::FromSeconds(kMaxBackoffSeconds);
int64 backoff_s =
std::max(static_cast<int64>(1),
last_delay.InSeconds() * kBackoffRandomizationFactor);
int rand_sign = base::RandInt(0, 1) * 2 - 1;
backoff_s = backoff_s +
(rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor));
backoff_s = std::max(static_cast<int64>(1),
std::min(backoff_s, kMaxBackoffSeconds));
return TimeDelta::FromSeconds(backoff_s);
}
TimeDelta BackoffDelayProvider::GetInitialDelay(
const sessions::ModelNeutralState& state) const {
if (state.commit_result == NETWORK_CONNECTION_UNAVAILABLE ||
state.last_download_updates_result == NETWORK_CONNECTION_UNAVAILABLE) {
return short_initial_backoff_;
}
if (SyncerErrorIsError(state.last_get_key_result))
return default_initial_backoff_;
if (state.last_download_updates_result == SERVER_RETURN_MIGRATION_DONE ||
state.commit_result == SERVER_RETURN_MIGRATION_DONE) {
return short_initial_backoff_;
}
if (state.commit_result == SERVER_RETURN_CONFLICT) {
return short_initial_backoff_;
}
return default_initial_backoff_;
}
}