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/threading/non_thread_safe.h" | 8 #include "base/threading/non_thread_safe.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 class TickClock; | 13 class TickClock; |
14 } | 14 } |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 // Provides the core logic needed for randomized exponential back-off | 18 // Provides the core logic needed for randomized exponential back-off |
19 // on requests to a given resource, given a back-off policy. | 19 // on requests to a given resource, given a back-off policy. |
20 // | 20 // |
21 // This utility class knows nothing about network specifics; it is | 21 // This utility class knows nothing about network specifics; it is |
22 // intended for reuse in various networking scenarios. | 22 // intended for reuse in various networking scenarios. |
23 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 23 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
24 public: | 24 public: |
25 // The set of parameters that define a back-off policy. | 25 // The set of parameters that define a back-off policy. If you change this, |
26 // increment SERIALIZATION_VERSION_NUMBER in backoff_entry_serializer.cc. | |
26 struct Policy { | 27 struct Policy { |
27 // Number of initial errors (in sequence) to ignore before applying | 28 // Number of initial errors (in sequence) to ignore before applying |
28 // exponential back-off rules. | 29 // exponential back-off rules. |
29 int num_errors_to_ignore; | 30 int num_errors_to_ignore; |
30 | 31 |
31 // Initial delay. The interpretation of this value depends on | 32 // Initial delay. The interpretation of this value depends on |
32 // always_use_initial_delay. It's either how long we wait between | 33 // 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 // requests before backoff starts, or how much we delay the first request |
34 // after backoff starts. | 35 // after backoff starts. |
35 int initial_delay_ms; | 36 int initial_delay_ms; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 // had for Policy::entry_lifetime_ms. | 93 // had for Policy::entry_lifetime_ms. |
93 bool CanDiscard() const; | 94 bool CanDiscard() const; |
94 | 95 |
95 // Resets this entry to a fresh (as if just constructed) state. | 96 // Resets this entry to a fresh (as if just constructed) state. |
96 void Reset(); | 97 void Reset(); |
97 | 98 |
98 // Returns the failure count for this entry. | 99 // Returns the failure count for this entry. |
99 int failure_count() const { return failure_count_; } | 100 int failure_count() const { return failure_count_; } |
100 | 101 |
101 private: | 102 private: |
103 friend class BackoffEntrySerializer; | |
104 | |
102 // Calculates when requests should again be allowed through. | 105 // Calculates when requests should again be allowed through. |
103 base::TimeTicks CalculateReleaseTime() const; | 106 base::TimeTicks CalculateReleaseTime() const; |
104 | 107 |
108 // Converts time_until_release to a release time, obeying maximum_backoff_ms. | |
109 base::TimeTicks TimeUntilReleaseToReleaseTime( | |
110 base::TimeDelta time_until_release) const; | |
mmenke
2015/05/05 15:47:51
nit: Maybe time_until_release -> backoff (or back
johnme
2015/05/06 12:46:46
Ok, renamed time_until_release to backoff_duration
| |
111 | |
105 // Equivalent to TimeTicks::Now(), using clock_ if provided. | 112 // Equivalent to TimeTicks::Now(), using clock_ if provided. |
106 base::TimeTicks GetTimeTicksNow() const; | 113 base::TimeTicks GetTimeTicksNow() const; |
107 | 114 |
108 // Timestamp calculated by the exponential back-off algorithm at which we are | 115 // Timestamp calculated by the exponential back-off algorithm at which we are |
109 // allowed to start sending requests again. | 116 // allowed to start sending requests again. |
110 base::TimeTicks exponential_backoff_release_time_; | 117 base::TimeTicks exponential_backoff_release_time_; |
111 | 118 |
112 // Counts request errors; decremented on success. | 119 // Counts request errors; decremented on success. |
113 int failure_count_; | 120 int failure_count_; |
114 | 121 |
115 const Policy* const policy_; // Not owned. | 122 const Policy* const policy_; // Not owned. |
116 | 123 |
117 base::TickClock* const clock_; // Not owned. | 124 base::TickClock* const clock_; // Not owned. |
118 | 125 |
119 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); | 126 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); |
120 }; | 127 }; |
121 | 128 |
122 } // namespace net | 129 } // namespace net |
123 | 130 |
124 #endif // NET_BASE_BACKOFF_ENTRY_H_ | 131 #endif // NET_BASE_BACKOFF_ENTRY_H_ |
OLD | NEW |