1 // Copyright 2012 The Chromium Authors 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 NET_BASE_BACKOFF_ENTRY_H_ 6 #define NET_BASE_BACKOFF_ENTRY_H_ 7 8 #include <stdint.h> 9 10 #include "base/memory/raw_ptr.h" 11 #include "base/threading/thread_checker.h" 12 #include "base/time/time.h" 13 #include "net/base/net_export.h" 14 15 namespace base { 16 class TickClock; 17 } 18 19 namespace net { 20 21 // Provides the core logic needed for randomized exponential back-off 22 // on requests to a given resource, given a back-off policy. 23 // 24 // This utility class knows nothing about network specifics; it is 25 // intended for reuse in various networking scenarios. 26 class NET_EXPORT BackoffEntry { 27 public: 28 // The set of parameters that define a back-off policy. When modifying this, 29 // increment SERIALIZATION_VERSION_NUMBER in backoff_entry_serializer.cc. 30 struct Policy { 31 // Number of initial errors (in sequence) to ignore before applying 32 // exponential back-off rules. 33 int num_errors_to_ignore; 34 35 // Initial delay. The interpretation of this value depends on 36 // always_use_initial_delay. It's either how long we wait between 37 // requests before backoff starts, or how much we delay the first request 38 // after backoff starts. 39 int initial_delay_ms; 40 41 // Factor by which the waiting time will be multiplied. 42 double multiply_factor; 43 44 // Fuzzing percentage. ex: 10% will spread requests randomly 45 // between 90%-100% of the calculated time. 46 double jitter_factor; 47 48 // Maximum amount of time we are willing to delay our request, -1 49 // for no maximum. 50 int64_t maximum_backoff_ms; 51 52 // Time to keep an entry from being discarded even when it 53 // has no significant state, -1 to never discard. 54 int64_t entry_lifetime_ms; 55 56 // If true, we always use a delay of initial_delay_ms, even before 57 // we've seen num_errors_to_ignore errors. Otherwise, initial_delay_ms 58 // is the first delay once we start exponential backoff. 59 // 60 // So if we're ignoring 1 error, we'll see (N, N, Nm, Nm^2, ...) if true, 61 // and (0, 0, N, Nm, ...) when false, where N is initial_backoff_ms and 62 // m is multiply_factor, assuming we've already seen one success. 63 bool always_use_initial_delay; 64 }; 65 66 // Lifetime of policy must enclose lifetime of BackoffEntry. The 67 // pointer must be valid but is not dereferenced during construction. 68 explicit BackoffEntry(const Policy* policy); 69 // Lifetime of policy and clock must enclose lifetime of BackoffEntry. 70 // |policy| pointer must be valid but isn't dereferenced during construction. 71 // |clock| pointer may be null. 72 BackoffEntry(const Policy* policy, const base::TickClock* clock); 73 BackoffEntry(const BackoffEntry&) = delete; 74 BackoffEntry& operator=(const BackoffEntry&) = delete; 75 virtual ~BackoffEntry(); 76 77 // Inform this item that a request for the network resource it is 78 // tracking was made, and whether it failed or succeeded. 79 void InformOfRequest(bool succeeded); 80 81 // Returns true if a request for the resource this item tracks should 82 // be rejected at the present time due to exponential back-off policy. 83 bool ShouldRejectRequest() const; 84 85 // Returns the absolute time after which this entry (given its present 86 // state) will no longer reject requests. 87 base::TimeTicks GetReleaseTime() const; 88 89 // Returns the time until a request can be sent (will be zero if the release 90 // time is in the past). 91 base::TimeDelta GetTimeUntilRelease() const; 92 93 // Converts |backoff_duration| to a release time, by adding it to 94 // GetTimeTicksNow(), limited by maximum_backoff_ms. 95 base::TimeTicks BackoffDurationToReleaseTime( 96 base::TimeDelta backoff_duration) const; 97 98 // Causes this object reject requests until the specified absolute time. 99 // This can be used to e.g. implement support for a Retry-After header. 100 void SetCustomReleaseTime(const base::TimeTicks& release_time); 101 102 // Returns true if this object has no significant state (i.e. you could 103 // just as well start with a fresh BackoffEntry object), and hasn't 104 // had for Policy::entry_lifetime_ms. 105 bool CanDiscard() const; 106 107 // Resets this entry to a fresh (as if just constructed) state. 108 void Reset(); 109 110 // Returns the failure count for this entry. failure_count()111 int failure_count() const { return failure_count_; } 112 113 // Equivalent to TimeTicks::Now(), using clock_ if provided. 114 base::TimeTicks GetTimeTicksNow() const; 115 116 private: 117 // Calculates when requests should again be allowed through. 118 base::TimeTicks CalculateReleaseTime() const; 119 120 // Timestamp calculated by the exponential back-off algorithm at which we are 121 // allowed to start sending requests again. 122 base::TimeTicks exponential_backoff_release_time_; 123 124 // Counts request errors; decremented on success. 125 int failure_count_; 126 127 const raw_ptr<const Policy> policy_; // Not owned. 128 129 const raw_ptr<const base::TickClock> clock_; // Not owned. 130 131 THREAD_CHECKER(thread_checker_); 132 }; 133 134 } // namespace net 135 136 #endif // NET_BASE_BACKOFF_ENTRY_H_ 137