| 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* 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); |
| 61 virtual ~BackoffEntry(); | 69 virtual ~BackoffEntry(); |
| 62 | 70 |
| 63 // Inform this item that a request for the network resource it is | 71 // Inform this item that a request for the network resource it is |
| 64 // tracking was made, and whether it failed or succeeded. | 72 // tracking was made, and whether it failed or succeeded. |
| 65 void InformOfRequest(bool succeeded); | 73 void InformOfRequest(bool succeeded); |
| 66 | 74 |
| 67 // Returns true if a request for the resource this item tracks should | 75 // 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. | 76 // be rejected at the present time due to exponential back-off policy. |
| 69 bool ShouldRejectRequest() const; | 77 bool ShouldRejectRequest() const; |
| 70 | 78 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 83 // just as well start with a fresh BackoffEntry object), and hasn't | 91 // just as well start with a fresh BackoffEntry object), and hasn't |
| 84 // had for Policy::entry_lifetime_ms. | 92 // had for Policy::entry_lifetime_ms. |
| 85 bool CanDiscard() const; | 93 bool CanDiscard() const; |
| 86 | 94 |
| 87 // Resets this entry to a fresh (as if just constructed) state. | 95 // Resets this entry to a fresh (as if just constructed) state. |
| 88 void Reset(); | 96 void Reset(); |
| 89 | 97 |
| 90 // Returns the failure count for this entry. | 98 // Returns the failure count for this entry. |
| 91 int failure_count() const { return failure_count_; } | 99 int failure_count() const { return failure_count_; } |
| 92 | 100 |
| 93 protected: | |
| 94 // Equivalent to TimeTicks::Now(), virtual so unit tests can override. | |
| 95 virtual base::TimeTicks ImplGetTimeNow() const; | |
| 96 | |
| 97 private: | 101 private: |
| 98 // Calculates when requests should again be allowed through. | 102 // Calculates when requests should again be allowed through. |
| 99 base::TimeTicks CalculateReleaseTime() const; | 103 base::TimeTicks CalculateReleaseTime() const; |
| 100 | 104 |
| 105 // Equivalent to TimeTicks::Now(), using clock_ if provided. |
| 106 base::TimeTicks GetTimeTicksNow() const; |
| 107 |
| 101 // Timestamp calculated by the exponential back-off algorithm at which we are | 108 // Timestamp calculated by the exponential back-off algorithm at which we are |
| 102 // allowed to start sending requests again. | 109 // allowed to start sending requests again. |
| 103 base::TimeTicks exponential_backoff_release_time_; | 110 base::TimeTicks exponential_backoff_release_time_; |
| 104 | 111 |
| 105 // Counts request errors; decremented on success. | 112 // Counts request errors; decremented on success. |
| 106 int failure_count_; | 113 int failure_count_; |
| 107 | 114 |
| 108 const Policy* const policy_; | 115 const Policy* const policy_; // Not owned. |
| 116 |
| 117 base::TickClock* const clock_; // Not owned. |
| 109 | 118 |
| 110 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); | 119 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); |
| 111 }; | 120 }; |
| 112 | 121 |
| 113 } // namespace net | 122 } // namespace net |
| 114 | 123 |
| 115 #endif // NET_BASE_BACKOFF_ENTRY_H_ | 124 #endif // NET_BASE_BACKOFF_ENTRY_H_ |
| OLD | NEW |