Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: net/url_request/url_request_throttler_entry.h

Issue 6711046: Add an opt-out header for HTTP throttling. Never throttle for localhost. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to review comments. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
7 #pragma once 7 #pragma once
8 8
9 #include <queue> 9 #include <queue>
10 #include <string> 10 #include <string>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "net/base/backoff_entry.h" 13 #include "net/base/backoff_entry.h"
14 #include "net/url_request/url_request_throttler_entry_interface.h" 14 #include "net/url_request/url_request_throttler_entry_interface.h"
15 15
16 namespace net { 16 namespace net {
17 17
18 class URLRequestThrottlerManager;
19
18 // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager. 20 // URLRequestThrottlerEntry represents an entry of URLRequestThrottlerManager.
19 // It analyzes requests of a specific URL over some period of time, in order to 21 // It analyzes requests of a specific URL over some period of time, in order to
20 // deduce the back-off time for every request. 22 // deduce the back-off time for every request.
21 // The back-off algorithm consists of two parts. Firstly, exponential back-off 23 // The back-off algorithm consists of two parts. Firstly, exponential back-off
22 // is used when receiving 5XX server errors or malformed response bodies. 24 // is used when receiving 5XX server errors or malformed response bodies.
23 // The exponential back-off rule is enforced by URLRequestHttpJob. Any 25 // The exponential back-off rule is enforced by URLRequestHttpJob. Any
24 // request sent during the back-off period will be cancelled. 26 // request sent during the back-off period will be cancelled.
25 // Secondly, a sliding window is used to count recent requests to a given 27 // Secondly, a sliding window is used to count recent requests to a given
26 // destination and provide guidance (to the application level only) on whether 28 // destination and provide guidance (to the application level only) on whether
27 // too many requests have been sent and when a good time to send the next one 29 // too many requests have been sent and when a good time to send the next one
28 // would be. This is never used to deny requests at the network level. 30 // would be. This is never used to deny requests at the network level.
29 class URLRequestThrottlerEntry : public URLRequestThrottlerEntryInterface { 31 class URLRequestThrottlerEntry : public URLRequestThrottlerEntryInterface {
30 public: 32 public:
31 // Sliding window period. 33 // Sliding window period.
32 static const int kDefaultSlidingWindowPeriodMs; 34 static const int kDefaultSlidingWindowPeriodMs;
33 35
34 // Maximum number of requests allowed in sliding window period. 36 // Maximum number of requests allowed in sliding window period.
35 static const int kDefaultMaxSendThreshold; 37 static const int kDefaultMaxSendThreshold;
36 38
39 // Number of initial errors to ignore before starting exponential back-off.
40 static const int kDefaultNumErrorsToIgnore;
41
37 // Initial delay for exponential back-off. 42 // Initial delay for exponential back-off.
38 static const int kDefaultInitialBackoffMs; 43 static const int kDefaultInitialBackoffMs;
39 44
40 // Factor by which the waiting time will be multiplied. 45 // Factor by which the waiting time will be multiplied.
41 static const double kDefaultMultiplyFactor; 46 static const double kDefaultMultiplyFactor;
42 47
43 // Fuzzing percentage. ex: 10% will spread requests randomly 48 // Fuzzing percentage. ex: 10% will spread requests randomly
44 // between 90%-100% of the calculated time. 49 // between 90%-100% of the calculated time.
45 static const double kDefaultJitterFactor; 50 static const double kDefaultJitterFactor;
46 51
47 // Maximum amount of time we are willing to delay our request. 52 // Maximum amount of time we are willing to delay our request.
48 static const int kDefaultMaximumBackoffMs; 53 static const int kDefaultMaximumBackoffMs;
49 54
50 // Time after which the entry is considered outdated. 55 // Time after which the entry is considered outdated.
51 static const int kDefaultEntryLifetimeMs; 56 static const int kDefaultEntryLifetimeMs;
52 57
53 // Name of the header that servers can use to ask clients to delay their next 58 // Name of the header that servers can use to ask clients to delay their
54 // request. 59 // next request.
55 static const char kRetryHeaderName[]; 60 static const char kRetryHeaderName[];
56 61
57 URLRequestThrottlerEntry(); 62 // Name of the header that sites can use to opt out of exponential back-off
63 // throttling.
64 static const char kExponentialThrottlingHeader[];
65
66 // Value for exponential throttling header that can be used to opt out of
67 // exponential back-off throttling.
68 static const char kExponentialThrottlingDisableValue[];
69
70 // The manager object's lifetime must enclose the lifetime of this object.
71 explicit URLRequestThrottlerEntry(URLRequestThrottlerManager* manager);
58 72
59 // The life span of instances created with this constructor is set to 73 // The life span of instances created with this constructor is set to
60 // infinite. 74 // infinite, and the number of initial errors to ignore is set to 0.
61 // It is only used by unit tests. 75 // It is only used by unit tests.
62 URLRequestThrottlerEntry(int sliding_window_period_ms, 76 URLRequestThrottlerEntry(URLRequestThrottlerManager* manager,
77 int sliding_window_period_ms,
63 int max_send_threshold, 78 int max_send_threshold,
64 int initial_backoff_ms, 79 int initial_backoff_ms,
65 double multiply_factor, 80 double multiply_factor,
66 double jitter_factor, 81 double jitter_factor,
67 int maximum_backoff_ms); 82 int maximum_backoff_ms);
68 83
69 // Used by the manager, returns true if the entry needs to be garbage 84 // Used by the manager, returns true if the entry needs to be garbage
70 // collected. 85 // collected.
71 bool IsEntryOutdated() const; 86 bool IsEntryOutdated() const;
72 87
88 // Causes this entry to never reject requests due to back-off.
89 void DisableBackoffThrottling();
90
91 // Causes this entry to NULL its manager pointer.
92 void DetachManager();
93
73 // Implementation of URLRequestThrottlerEntryInterface. 94 // Implementation of URLRequestThrottlerEntryInterface.
74 virtual bool IsDuringExponentialBackoff() const; 95 virtual bool IsDuringExponentialBackoff() const;
75 virtual int64 ReserveSendingTimeForNextRequest( 96 virtual int64 ReserveSendingTimeForNextRequest(
76 const base::TimeTicks& earliest_time); 97 const base::TimeTicks& earliest_time);
77 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const; 98 virtual base::TimeTicks GetExponentialBackoffReleaseTime() const;
78 virtual void UpdateWithResponse( 99 virtual void UpdateWithResponse(
100 const std::string& host,
79 const URLRequestThrottlerHeaderInterface* response); 101 const URLRequestThrottlerHeaderInterface* response);
80 virtual void ReceivedContentWasMalformed(); 102 virtual void ReceivedContentWasMalformed();
81 103
82 protected: 104 protected:
83 virtual ~URLRequestThrottlerEntry(); 105 virtual ~URLRequestThrottlerEntry();
84 106
85 void Initialize(); 107 void Initialize();
86 108
87 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose. 109 // Equivalent to TimeTicks::Now(), virtual to be mockable for testing purpose.
88 virtual base::TimeTicks GetTimeNow() const; 110 virtual base::TimeTicks GetTimeNow() const;
89 111
90 // Used internally to increase release time following a retry-after header. 112 // Used internally to increase release time following a retry-after header.
91 void HandleCustomRetryAfter(const std::string& header_value); 113 void HandleCustomRetryAfter(const std::string& header_value);
92 114
93 // Retrieves the backoff entry object we're using. Used to enable a 115 // Used internally to handle the opt-out header.
116 void HandleThrottlingHeader(const std::string& header_value,
117 const std::string& host);
118
119 // Retrieves the back-off entry object we're using. Used to enable a
94 // unit testing seam for dependency injection in tests. 120 // unit testing seam for dependency injection in tests.
95 virtual const BackoffEntry* GetBackoffEntry() const; 121 virtual const BackoffEntry* GetBackoffEntry() const;
96 virtual BackoffEntry* GetBackoffEntry(); 122 virtual BackoffEntry* GetBackoffEntry();
97 123
98 // Used by tests. 124 // Used by tests.
99 base::TimeTicks sliding_window_release_time() const { 125 base::TimeTicks sliding_window_release_time() const {
100 return sliding_window_release_time_; 126 return sliding_window_release_time_;
101 } 127 }
102 128
103 // Used by tests. 129 // Used by tests.
(...skipping 10 matching lines...) Expand all
114 // not used to deny requests. 140 // not used to deny requests.
115 base::TimeTicks sliding_window_release_time_; 141 base::TimeTicks sliding_window_release_time_;
116 142
117 // A list of the recent send events. We use them to decide whether there are 143 // A list of the recent send events. We use them to decide whether there are
118 // too many requests sent in sliding window. 144 // too many requests sent in sliding window.
119 std::queue<base::TimeTicks> send_log_; 145 std::queue<base::TimeTicks> send_log_;
120 146
121 const base::TimeDelta sliding_window_period_; 147 const base::TimeDelta sliding_window_period_;
122 const int max_send_threshold_; 148 const int max_send_threshold_;
123 149
150 // True if DisableBackoffThrottling() has been called on this object.
151 bool is_backoff_disabled_;
152
124 // Access it through GetBackoffEntry() to allow a unit test seam. 153 // Access it through GetBackoffEntry() to allow a unit test seam.
125 BackoffEntry backoff_entry_; 154 BackoffEntry backoff_entry_;
126 155
156 // Weak back-reference to the manager object managing us.
157 URLRequestThrottlerManager* manager_;
158
127 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry); 159 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerEntry);
128 }; 160 };
129 161
130 } // namespace net 162 } // namespace net
131 163
132 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_ 164 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_ENTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698