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