• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Weave 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 #ifndef LIBWEAVE_SRC_BACKOFF_ENTRY_H_
6 #define LIBWEAVE_SRC_BACKOFF_ENTRY_H_
7 
8 #include <base/time/time.h>
9 
10 namespace weave {
11 
12 // Provides the core logic needed for randomized exponential back-off
13 // on requests to a given resource, given a back-off policy.
14 //
15 // This class is largely taken from net/base/backoff_entry.h from Chromium.
16 // TODO(avakulenko): Consider packaging portions of Chrome's //net functionality
17 // into the current libchrome library.
18 class BackoffEntry {
19  public:
20   // The set of parameters that define a back-off policy.
21   struct Policy {
22     // Number of initial errors (in sequence) to ignore before applying
23     // exponential back-off rules.
24     int num_errors_to_ignore;
25 
26     // Initial delay.  The interpretation of this value depends on
27     // always_use_initial_delay.  It's either how long we wait between
28     // requests before backoff starts, or how much we delay the first request
29     // after backoff starts.
30     int initial_delay_ms;
31 
32     // Factor by which the waiting time will be multiplied.
33     double multiply_factor;
34 
35     // Fuzzing percentage. ex: 10% will spread requests randomly
36     // between 90%-100% of the calculated time.
37     double jitter_factor;
38 
39     // Maximum amount of time we are willing to delay our request, -1
40     // for no maximum.
41     int64_t maximum_backoff_ms;
42 
43     // Time to keep an entry from being discarded even when it
44     // has no significant state, -1 to never discard.
45     int64_t entry_lifetime_ms;
46 
47     // If true, we always use a delay of initial_delay_ms, even before
48     // we've seen num_errors_to_ignore errors.  Otherwise, initial_delay_ms
49     // is the first delay once we start exponential backoff.
50     //
51     // So if we're ignoring 1 error, we'll see (N, N, Nm, Nm^2, ...) if true,
52     // and (0, 0, N, Nm, ...) when false, where N is initial_backoff_ms and
53     // m is multiply_factor, assuming we've already seen one success.
54     bool always_use_initial_delay;
55   };
56 
57   // Lifetime of policy must enclose lifetime of BackoffEntry. The
58   // pointer must be valid but is not dereferenced during construction.
59   explicit BackoffEntry(const Policy* const policy);
~BackoffEntry()60   virtual ~BackoffEntry() {}
61 
62   // Inform this item that a request for the network resource it is
63   // tracking was made, and whether it failed or succeeded.
64   void InformOfRequest(bool succeeded);
65 
66   // Returns true if a request for the resource this item tracks should
67   // be rejected at the present time due to exponential back-off policy.
68   bool ShouldRejectRequest() const;
69 
70   // Returns the absolute time after which this entry (given its present
71   // state) will no longer reject requests.
72   base::TimeTicks GetReleaseTime() const;
73 
74   // Returns the time until a request can be sent.
75   base::TimeDelta GetTimeUntilRelease() const;
76 
77   // Causes this object reject requests until the specified absolute time.
78   // This can be used to e.g. implement support for a Retry-After header.
79   void SetCustomReleaseTime(const base::TimeTicks& release_time);
80 
81   // Returns true if this object has no significant state (i.e. you could
82   // just as well start with a fresh BackoffEntry object), and hasn't
83   // had for Policy::entry_lifetime_ms.
84   bool CanDiscard() const;
85 
86   // Resets this entry to a fresh (as if just constructed) state.
87   void Reset();
88 
89   // Returns the failure count for this entry.
failure_count()90   int failure_count() const { return failure_count_; }
91 
92  protected:
93   // Equivalent to TimeTicks::Now(), virtual so unit tests can override.
94   virtual base::TimeTicks ImplGetTimeNow() const;
95 
96  private:
97   // Calculates when requests should again be allowed through.
98   base::TimeTicks CalculateReleaseTime() const;
99 
100   // Timestamp calculated by the exponential back-off algorithm at which we are
101   // allowed to start sending requests again.
102   base::TimeTicks exponential_backoff_release_time_;
103 
104   // Counts request errors; decremented on success.
105   int failure_count_;
106 
107   const Policy* const policy_;
108 
109   DISALLOW_COPY_AND_ASSIGN(BackoffEntry);
110 };
111 
112 }  // namespace weave
113 
114 #endif  // LIBWEAVE_SRC_BACKOFF_ENTRY_H_
115