OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_BACKOFF_ENTRY_H_ | 5 #ifndef NET_BASE_BACKOFF_ENTRY_H_ |
6 #define NET_BASE_BACKOFF_ENTRY_H_ | 6 #define NET_BASE_BACKOFF_ENTRY_H_ |
7 | 7 |
| 8 #include "base/macros.h" |
8 #include "base/threading/non_thread_safe.h" | 9 #include "base/threading/non_thread_safe.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 class TickClock; | 14 class TickClock; |
14 } | 15 } |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 // Provides the core logic needed for randomized exponential back-off | 19 // Provides the core logic needed for randomized exponential back-off |
19 // on requests to a given resource, given a back-off policy. | 20 // on requests to a given resource, given a back-off policy. |
20 // | 21 // |
21 // This utility class knows nothing about network specifics; it is | 22 // This utility class knows nothing about network specifics; it is |
22 // intended for reuse in various networking scenarios. | 23 // intended for reuse in various networking scenarios. |
23 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 24 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
24 public: | 25 public: |
25 // The set of parameters that define a back-off policy. | 26 // The set of parameters that define a back-off policy. When modifying this, |
| 27 // increment SERIALIZATION_VERSION_NUMBER in backoff_entry_serializer.cc. |
26 struct Policy { | 28 struct Policy { |
27 // Number of initial errors (in sequence) to ignore before applying | 29 // Number of initial errors (in sequence) to ignore before applying |
28 // exponential back-off rules. | 30 // exponential back-off rules. |
29 int num_errors_to_ignore; | 31 int num_errors_to_ignore; |
30 | 32 |
31 // Initial delay. The interpretation of this value depends on | 33 // Initial delay. The interpretation of this value depends on |
32 // always_use_initial_delay. It's either how long we wait between | 34 // 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 | 35 // requests before backoff starts, or how much we delay the first request |
34 // after backoff starts. | 36 // after backoff starts. |
35 int initial_delay_ms; | 37 int initial_delay_ms; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 void InformOfRequest(bool succeeded); | 75 void InformOfRequest(bool succeeded); |
74 | 76 |
75 // Returns true if a request for the resource this item tracks should | 77 // 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. | 78 // be rejected at the present time due to exponential back-off policy. |
77 bool ShouldRejectRequest() const; | 79 bool ShouldRejectRequest() const; |
78 | 80 |
79 // Returns the absolute time after which this entry (given its present | 81 // Returns the absolute time after which this entry (given its present |
80 // state) will no longer reject requests. | 82 // state) will no longer reject requests. |
81 base::TimeTicks GetReleaseTime() const; | 83 base::TimeTicks GetReleaseTime() const; |
82 | 84 |
83 // Returns the time until a request can be sent. | 85 // Returns the time until a request can be sent (will be zero if the release |
| 86 // time is in the past). |
84 base::TimeDelta GetTimeUntilRelease() const; | 87 base::TimeDelta GetTimeUntilRelease() const; |
85 | 88 |
| 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; |
| 93 |
86 // Causes this object reject requests until the specified absolute time. | 94 // 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. | 95 // This can be used to e.g. implement support for a Retry-After header. |
88 void SetCustomReleaseTime(const base::TimeTicks& release_time); | 96 void SetCustomReleaseTime(const base::TimeTicks& release_time); |
89 | 97 |
90 // Returns true if this object has no significant state (i.e. you could | 98 // 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 | 99 // just as well start with a fresh BackoffEntry object), and hasn't |
92 // had for Policy::entry_lifetime_ms. | 100 // had for Policy::entry_lifetime_ms. |
93 bool CanDiscard() const; | 101 bool CanDiscard() const; |
94 | 102 |
95 // Resets this entry to a fresh (as if just constructed) state. | 103 // Resets this entry to a fresh (as if just constructed) state. |
96 void Reset(); | 104 void Reset(); |
97 | 105 |
98 // Returns the failure count for this entry. | 106 // Returns the failure count for this entry. |
99 int failure_count() const { return failure_count_; } | 107 int failure_count() const { return failure_count_; } |
100 | 108 |
| 109 // Returns the TickClock passed in to the constructor. May be NULL. |
| 110 base::TickClock* tick_clock() const { return clock_; } |
| 111 |
101 private: | 112 private: |
102 // Calculates when requests should again be allowed through. | 113 // Calculates when requests should again be allowed through. |
103 base::TimeTicks CalculateReleaseTime() const; | 114 base::TimeTicks CalculateReleaseTime() const; |
104 | 115 |
105 // Equivalent to TimeTicks::Now(), using clock_ if provided. | 116 // Equivalent to TimeTicks::Now(), using clock_ if provided. |
106 base::TimeTicks GetTimeTicksNow() const; | 117 base::TimeTicks GetTimeTicksNow() const; |
107 | 118 |
108 // Timestamp calculated by the exponential back-off algorithm at which we are | 119 // Timestamp calculated by the exponential back-off algorithm at which we are |
109 // allowed to start sending requests again. | 120 // allowed to start sending requests again. |
110 base::TimeTicks exponential_backoff_release_time_; | 121 base::TimeTicks exponential_backoff_release_time_; |
111 | 122 |
112 // Counts request errors; decremented on success. | 123 // Counts request errors; decremented on success. |
113 int failure_count_; | 124 int failure_count_; |
114 | 125 |
115 const Policy* const policy_; // Not owned. | 126 const Policy* const policy_; // Not owned. |
116 | 127 |
117 base::TickClock* const clock_; // Not owned. | 128 base::TickClock* const clock_; // Not owned. |
118 | 129 |
119 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); | 130 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); |
120 }; | 131 }; |
121 | 132 |
122 } // namespace net | 133 } // namespace net |
123 | 134 |
124 #endif // NET_BASE_BACKOFF_ENTRY_H_ | 135 #endif // NET_BASE_BACKOFF_ENTRY_H_ |
OLD | NEW |