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