Update V8 to version 4.6.55.
[chromium-blink-merge.git] / net / url_request / url_request_throttler_entry.h
blob264766a22a40dcec2df2ae7c82c0a5d24e6f72d3
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_
8 #include <queue>
9 #include <string>
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"
17 namespace net {
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 {
35 public:
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,
74 double jitter_factor,
75 int maximum_backoff_ms);
77 // Used by the manager, returns true if the entry needs to be garbage
78 // collected.
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.
85 void DetachManager();
87 // Implementation of URLRequestThrottlerEntryInterface.
88 bool ShouldRejectRequest(const URLRequest& request) const override;
89 int64 ReserveSendingTimeForNextRequest(
90 const base::TimeTicks& earliest_time) override;
91 base::TimeTicks GetExponentialBackoffReleaseTime() const override;
92 void UpdateWithResponse(int status_code) override;
93 void ReceivedContentWasMalformed(int response_code) override;
95 protected:
96 ~URLRequestThrottlerEntry() override;
98 void Initialize();
100 // Returns true if the given response code is considered a success for
101 // throttling purposes.
102 bool IsConsideredSuccess(int response_code);
104 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
105 virtual base::TimeTicks ImplGetTimeNow() const;
107 // Retrieves the back-off entry object we're using. Used to enable a
108 // unit testing seam for dependency injection in tests.
109 virtual const BackoffEntry* GetBackoffEntry() const;
110 virtual BackoffEntry* GetBackoffEntry();
112 // Returns true if |load_flags| contains a flag that indicates an
113 // explicit request by the user to load the resource. We never
114 // throttle requests with such load flags.
115 static bool ExplicitUserRequest(const int load_flags);
117 // Used by tests.
118 base::TimeTicks sliding_window_release_time() const {
119 return sliding_window_release_time_;
122 // Used by tests.
123 void set_sliding_window_release_time(const base::TimeTicks& release_time) {
124 sliding_window_release_time_ = release_time;
127 // Valid and immutable after construction time.
128 BackoffEntry::Policy backoff_policy_;
130 private:
131 // Timestamp calculated by the sliding window algorithm for when we advise
132 // clients the next request should be made, at the earliest. Advisory only,
133 // not used to deny requests.
134 base::TimeTicks sliding_window_release_time_;
136 // A list of the recent send events. We use them to decide whether there are
137 // too many requests sent in sliding window.
138 std::queue<base::TimeTicks> send_log_;
140 const base::TimeDelta sliding_window_period_;
141 const int max_send_threshold_;
143 // True if DisableBackoffThrottling() has been called on this object.
144 bool is_backoff_disabled_;
146 // Access it through GetBackoffEntry() to allow a unit test seam.
147 BackoffEntry backoff_entry_;
149 // Weak back-reference to the manager object managing us.
150 URLRequestThrottlerManager* manager_;
152 // Canonicalized URL string that this entry is for; used for logging only.
153 std::string url_id_;
155 BoundNetLog net_log_;
157 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry);
160 } // namespace net
162 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_