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