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
3 // found in the LICENSE file.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "net/base/backoff_entry.h"
14 #include "net/log/net_log.h"
15 #include "net/url_request/url_request_throttler_entry_interface.h"
19 class NetworkDelegate
;
20 class URLRequestThrottlerManager
;
22 // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager.
23 // It analyzes requests of a specific URL over some period of time, in order to
24 // deduce the back-off time for every request.
25 // The back-off algorithm consists of two parts. Firstly, exponential back-off
26 // is used when receiving 5XX server errors or malformed response bodies.
27 // The exponential back-off rule is enforced by URLRequestHttpJob. Any
28 // request sent during the back-off period will be cancelled.
29 // Secondly, a sliding window is used to count recent requests to a given
30 // destination and provide guidance (to the application level only) on whether
31 // too many requests have been sent and when a good time to send the next one
32 // would be. This is never used to deny requests at the network level.
33 class NET_EXPORT URLRequestThrottlerEntry
34 : public URLRequestThrottlerEntryInterface
{
36 // Sliding window period.
37 static const int kDefaultSlidingWindowPeriodMs
;
39 // Maximum number of requests allowed in sliding window period.
40 static const int kDefaultMaxSendThreshold
;
42 // Number of initial errors to ignore before starting exponential back-off.
43 static const int kDefaultNumErrorsToIgnore
;
45 // Initial delay for exponential back-off.
46 static const int kDefaultInitialDelayMs
;
48 // Factor by which the waiting time will be multiplied.
49 static const double kDefaultMultiplyFactor
;
51 // Fuzzing percentage. ex: 10% will spread requests randomly
52 // between 90%-100% of the calculated time.
53 static const double kDefaultJitterFactor
;
55 // Maximum amount of time we are willing to delay our request.
56 static const int kDefaultMaximumBackoffMs
;
58 // Time after which the entry is considered outdated.
59 static const int kDefaultEntryLifetimeMs
;
61 // The manager object's lifetime must enclose the lifetime of this object.
62 URLRequestThrottlerEntry(URLRequestThrottlerManager
* manager
,
63 const std::string
& url_id
);
65 // The life span of instances created with this constructor is set to
66 // infinite, and the number of initial errors to ignore is set to 0.
67 // It is only used by unit tests.
68 URLRequestThrottlerEntry(URLRequestThrottlerManager
* manager
,
69 const std::string
& url_id
,
70 int sliding_window_period_ms
,
71 int max_send_threshold
,
72 int initial_backoff_ms
,
73 double multiply_factor
,
75 int maximum_backoff_ms
);
77 // Used by the manager, returns true if the entry needs to be garbage
79 bool IsEntryOutdated() const;
81 // Causes this entry to never reject requests due to back-off.
82 void DisableBackoffThrottling();
84 // Causes this entry to NULL its manager pointer.
87 // Implementation of URLRequestThrottlerEntryInterface.
88 bool ShouldRejectRequest(const URLRequest
& request
,
89 NetworkDelegate
* network_delegate
) const override
;
90 int64
ReserveSendingTimeForNextRequest(
91 const base::TimeTicks
& earliest_time
) override
;
92 base::TimeTicks
GetExponentialBackoffReleaseTime() const override
;
93 void UpdateWithResponse(int status_code
) override
;
94 void ReceivedContentWasMalformed(int response_code
) override
;
97 ~URLRequestThrottlerEntry() override
;
101 // Returns true if the given response code is considered a success for
102 // throttling purposes.
103 bool IsConsideredSuccess(int response_code
);
105 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
106 virtual base::TimeTicks
ImplGetTimeNow() const;
108 // Retrieves the back-off entry object we're using. Used to enable a
109 // unit testing seam for dependency injection in tests.
110 virtual const BackoffEntry
* GetBackoffEntry() const;
111 virtual BackoffEntry
* GetBackoffEntry();
113 // Returns true if |load_flags| contains a flag that indicates an
114 // explicit request by the user to load the resource. We never
115 // throttle requests with such load flags.
116 static bool ExplicitUserRequest(const int load_flags
);
119 base::TimeTicks
sliding_window_release_time() const {
120 return sliding_window_release_time_
;
124 void set_sliding_window_release_time(const base::TimeTicks
& release_time
) {
125 sliding_window_release_time_
= release_time
;
128 // Valid and immutable after construction time.
129 BackoffEntry::Policy backoff_policy_
;
132 // Timestamp calculated by the sliding window algorithm for when we advise
133 // clients the next request should be made, at the earliest. Advisory only,
134 // not used to deny requests.
135 base::TimeTicks sliding_window_release_time_
;
137 // A list of the recent send events. We use them to decide whether there are
138 // too many requests sent in sliding window.
139 std::queue
<base::TimeTicks
> send_log_
;
141 const base::TimeDelta sliding_window_period_
;
142 const int max_send_threshold_
;
144 // True if DisableBackoffThrottling() has been called on this object.
145 bool is_backoff_disabled_
;
147 // Access it through GetBackoffEntry() to allow a unit test seam.
148 BackoffEntry backoff_entry_
;
150 // Weak back-reference to the manager object managing us.
151 URLRequestThrottlerManager
* manager_
;
153 // Canonicalized URL string that this entry is for; used for logging only.
156 BoundNetLog net_log_
;
158 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry
);
163 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_