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 { | |
13 class TickClock; | |
14 } | |
15 | |
12 namespace net { | 16 namespace net { |
13 | 17 |
14 // Provides the core logic needed for randomized exponential back-off | 18 // Provides the core logic needed for randomized exponential back-off |
15 // on requests to a given resource, given a back-off policy. | 19 // on requests to a given resource, given a back-off policy. |
16 // | 20 // |
17 // This utility class knows nothing about network specifics; it is | 21 // This utility class knows nothing about network specifics; it is |
18 // intended for reuse in various networking scenarios. | 22 // intended for reuse in various networking scenarios. |
19 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 23 class NET_EXPORT BackoffEntry : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
20 public: | 24 public: |
21 // The set of parameters that define a back-off policy. | 25 // The set of parameters that define a back-off policy. |
(...skipping 28 matching lines...) Expand all Loading... | |
50 // is the first delay once we start exponential backoff. | 54 // is the first delay once we start exponential backoff. |
51 // | 55 // |
52 // So if we're ignoring 1 error, we'll see (N, N, Nm, Nm^2, ...) if true, | 56 // So if we're ignoring 1 error, we'll see (N, N, Nm, Nm^2, ...) if true, |
53 // and (0, 0, N, Nm, ...) when false, where N is initial_backoff_ms and | 57 // and (0, 0, N, Nm, ...) when false, where N is initial_backoff_ms and |
54 // m is multiply_factor, assuming we've already seen one success. | 58 // m is multiply_factor, assuming we've already seen one success. |
55 bool always_use_initial_delay; | 59 bool always_use_initial_delay; |
56 }; | 60 }; |
57 | 61 |
58 // Lifetime of policy must enclose lifetime of BackoffEntry. The | 62 // Lifetime of policy must enclose lifetime of BackoffEntry. The |
59 // pointer must be valid but is not dereferenced during construction. | 63 // pointer must be valid but is not dereferenced during construction. |
60 explicit BackoffEntry(const Policy* const policy); | 64 explicit BackoffEntry(const Policy* const policy); |
pneubeck (no reviews)
2015/04/14 10:27:04
arguments are usually not declared const.
(same fo
johnme
2015/04/20 15:52:44
Done.
| |
65 BackoffEntry(const Policy* const policy, base::TickClock* clock); | |
pneubeck (no reviews)
2015/04/14 10:27:04
this needs a comment about the lifetime of |clock|
johnme
2015/04/20 15:52:44
Added a comment. Requiring non-null |clock| seems
pneubeck (no reviews)
2015/04/20 16:35:50
Doesn't sound like a convincing argument to me (to
| |
61 virtual ~BackoffEntry(); | 66 virtual ~BackoffEntry(); |
62 | 67 |
63 // Inform this item that a request for the network resource it is | 68 // Inform this item that a request for the network resource it is |
64 // tracking was made, and whether it failed or succeeded. | 69 // tracking was made, and whether it failed or succeeded. |
65 void InformOfRequest(bool succeeded); | 70 void InformOfRequest(bool succeeded); |
66 | 71 |
67 // Returns true if a request for the resource this item tracks should | 72 // Returns true if a request for the resource this item tracks should |
68 // be rejected at the present time due to exponential back-off policy. | 73 // be rejected at the present time due to exponential back-off policy. |
69 bool ShouldRejectRequest() const; | 74 bool ShouldRejectRequest() const; |
70 | 75 |
(...skipping 12 matching lines...) Expand all Loading... | |
83 // just as well start with a fresh BackoffEntry object), and hasn't | 88 // just as well start with a fresh BackoffEntry object), and hasn't |
84 // had for Policy::entry_lifetime_ms. | 89 // had for Policy::entry_lifetime_ms. |
85 bool CanDiscard() const; | 90 bool CanDiscard() const; |
86 | 91 |
87 // Resets this entry to a fresh (as if just constructed) state. | 92 // Resets this entry to a fresh (as if just constructed) state. |
88 void Reset(); | 93 void Reset(); |
89 | 94 |
90 // Returns the failure count for this entry. | 95 // Returns the failure count for this entry. |
91 int failure_count() const { return failure_count_; } | 96 int failure_count() const { return failure_count_; } |
92 | 97 |
93 protected: | |
94 // Equivalent to TimeTicks::Now(), virtual so unit tests can override. | |
95 virtual base::TimeTicks ImplGetTimeNow() const; | |
96 | |
97 private: | 98 private: |
98 // Calculates when requests should again be allowed through. | 99 // Calculates when requests should again be allowed through. |
99 base::TimeTicks CalculateReleaseTime() const; | 100 base::TimeTicks CalculateReleaseTime() const; |
100 | 101 |
102 // Equivalent to TimeTicks::Now(), using clock_ if provided. | |
103 base::TimeTicks GetTimeTicksNow() const; | |
104 | |
101 // Timestamp calculated by the exponential back-off algorithm at which we are | 105 // Timestamp calculated by the exponential back-off algorithm at which we are |
102 // allowed to start sending requests again. | 106 // allowed to start sending requests again. |
103 base::TimeTicks exponential_backoff_release_time_; | 107 base::TimeTicks exponential_backoff_release_time_; |
104 | 108 |
105 // Counts request errors; decremented on success. | 109 // Counts request errors; decremented on success. |
106 int failure_count_; | 110 int failure_count_; |
107 | 111 |
108 const Policy* const policy_; | 112 const Policy* const policy_; |
109 | 113 |
114 base::TickClock* clock_; // Not owned. | |
pneubeck (no reviews)
2015/04/14 10:27:04
should this ever change during the lifetime of the
johnme
2015/04/20 15:52:44
Done.
| |
115 | |
110 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); | 116 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); |
111 }; | 117 }; |
112 | 118 |
113 } // namespace net | 119 } // namespace net |
114 | 120 |
115 #endif // NET_BASE_BACKOFF_ENTRY_H_ | 121 #endif // NET_BASE_BACKOFF_ENTRY_H_ |
OLD | NEW |