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