File: backoff_delay_provider.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (109 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/engine_impl/backoff_delay_provider.h"

#include <stdint.h>

#include <algorithm>

#include "base/rand_util.h"
#include "components/sync/base/syncer_error.h"
#include "components/sync/engine/cycle/model_neutral_state.h"
#include "components/sync/engine/polling_constants.h"

using base::TimeDelta;

namespace syncer {

// static
BackoffDelayProvider* BackoffDelayProvider::FromDefaults() {
  return new BackoffDelayProvider(
      TimeDelta::FromSeconds(kInitialBackoffRetrySeconds),
      TimeDelta::FromSeconds(kInitialBackoffImmediateRetrySeconds));
}

// static
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);

  // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2
  int64_t backoff_s =
      std::max(static_cast<int64_t>(1),
               last_delay.InSeconds() * kBackoffRandomizationFactor);

  // Flip a coin to randomize backoff interval by +/- 50%.
  int rand_sign = base::RandInt(0, 1) * 2 - 1;

  // Truncation is adequate for rounding here.
  backoff_s =
      backoff_s +
      (rand_sign * (last_delay.InSeconds() / kBackoffRandomizationFactor));

  // Cap the backoff interval.
  backoff_s = std::max(static_cast<int64_t>(1),
                       std::min(backoff_s, kMaxBackoffSeconds));

  return TimeDelta::FromSeconds(backoff_s);
}

TimeDelta BackoffDelayProvider::GetInitialDelay(
    const ModelNeutralState& state) const {
  // NETWORK_CONNECTION_UNAVAILABLE implies we did not receive HTTP response
  // from server because of some network error. If network is unavailable then
  // on next connection type or address change scheduler will run canary job.
  // Otherwise we'll retry in 30 seconds.
  if (state.commit_result == NETWORK_CONNECTION_UNAVAILABLE ||
      state.last_download_updates_result == NETWORK_CONNECTION_UNAVAILABLE) {
    return default_initial_backoff_;
  }

  if (SyncerErrorIsError(state.last_get_key_result))
    return default_initial_backoff_;

  // Note: If we received a MIGRATION_DONE on download updates, then commit
  // should not have taken place.  Moreover, if we receive a MIGRATION_DONE
  // on commit, it means that download updates succeeded.  Therefore, we only
  // need to check if either code is equal to SERVER_RETURN_MIGRATION_DONE,
  // and not if there were any more serious errors requiring the long retry.
  if (state.last_download_updates_result == SERVER_RETURN_MIGRATION_DONE ||
      state.commit_result == SERVER_RETURN_MIGRATION_DONE) {
    return short_initial_backoff_;
  }

  // If a datatype decides the GetUpdates must be retried (e.g. because the
  // context has been updated since the request), use the short delay.
  if (state.last_download_updates_result == DATATYPE_TRIGGERED_RETRY)
    return short_initial_backoff_;

  // When the server tells us we have a conflict, then we should download the
  // latest updates so we can see the conflict ourselves, resolve it locally,
  // then try again to commit.  Running another sync cycle will do all these
  // things.  There's no need to back off, we can do this immediately.
  //
  // TODO(sync): We shouldn't need to handle this in BackoffDelayProvider.
  // There should be a way to deal with protocol errors before we get to this
  // point.
  if (state.commit_result == SERVER_RETURN_CONFLICT)
    return short_initial_backoff_;

  return default_initial_backoff_;
}

}  // namespace syncer