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_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
19 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
20 #include "net/url_request/url_request_throttler_entry.h" | 20 #include "net/url_request/url_request_throttler_entry.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 class BoundNetLog; | 24 class BoundNetLog; |
25 class NetLog; | 25 class NetLog; |
26 | 26 |
27 class NET_EXPORT URLRequestThrottlerManagerDelegate { | |
28 public: | |
29 virtual ~URLRequestThrottlerManagerDelegate() {} | |
30 | |
31 // Returns true if the URLRequestThrottlerManager may reject the | |
32 // request in question if it believe the server servicing the | |
33 // request to be overloaded or down. | |
34 virtual bool MayRejectRequest(URLRequest* request) = 0; | |
eroman
2012/06/05 05:30:25
Same comment as earlier, please make |request| con
Jói
2012/06/05 16:39:14
Done.
| |
35 }; | |
36 | |
27 // Class that registers URL request throttler entries for URLs being accessed | 37 // Class that registers URL request throttler entries for URLs being accessed |
28 // in order to supervise traffic. URL requests for HTTP contents should | 38 // in order to supervise traffic. URL requests for HTTP contents should |
29 // register their URLs in this manager on each request. | 39 // register their URLs in this manager on each request. |
30 // | 40 // |
31 // URLRequestThrottlerManager maintains a map of URL IDs to URL request | 41 // URLRequestThrottlerManager maintains a map of URL IDs to URL request |
32 // throttler entries. It creates URL request throttler entries when new URLs | 42 // throttler entries. It creates URL request throttler entries when new URLs |
33 // are registered, and does garbage collection from time to time in order to | 43 // are registered, and does garbage collection from time to time in order to |
34 // clean out outdated entries. URL ID consists of lowercased scheme, host, port | 44 // clean out outdated entries. URL ID consists of lowercased scheme, host, port |
35 // and path. All URLs converted to the same ID will share the same entry. | 45 // and path. All URLs converted to the same ID will share the same entry. |
36 class NET_EXPORT URLRequestThrottlerManager | 46 class NET_EXPORT URLRequestThrottlerManager |
37 : NON_EXPORTED_BASE(public base::NonThreadSafe), | 47 : NON_EXPORTED_BASE(public base::NonThreadSafe), |
38 public NetworkChangeNotifier::IPAddressObserver, | 48 public NetworkChangeNotifier::IPAddressObserver, |
39 public NetworkChangeNotifier::ConnectionTypeObserver { | 49 public NetworkChangeNotifier::ConnectionTypeObserver { |
40 public: | 50 public: |
41 URLRequestThrottlerManager(); | 51 explicit URLRequestThrottlerManager( |
52 URLRequestThrottlerManagerDelegate* delegate); | |
eroman
2012/06/05 05:30:25
Please describe ownership semantics (i.e. that |de
Jói
2012/06/05 16:39:14
This concern goes away with the switch to NetworkD
| |
42 virtual ~URLRequestThrottlerManager(); | 53 virtual ~URLRequestThrottlerManager(); |
43 | 54 |
44 // Must be called for every request, returns the URL request throttler entry | 55 // Must be called for every request, returns the URL request throttler entry |
45 // associated with the URL. The caller must inform this entry of some events. | 56 // associated with the URL. The caller must inform this entry of some events. |
46 // Please refer to url_request_throttler_entry_interface.h for further | 57 // Please refer to url_request_throttler_entry_interface.h for further |
47 // informations. | 58 // informations. |
48 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( | 59 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( |
49 const GURL& url); | 60 const GURL& url); |
50 | 61 |
51 // Adds the given host to a list of sites for which exponential back-off | 62 // Adds the given host to a list of sites for which exponential back-off |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 // we were previously getting 500 or 503 responses from (perhaps the | 117 // we were previously getting 500 or 503 responses from (perhaps the |
107 // responses are from a badly-written proxy that should have returned a | 118 // responses are from a badly-written proxy that should have returned a |
108 // 502 or 504 because it's upstream connection was down or it had no route | 119 // 502 or 504 because it's upstream connection was down or it had no route |
109 // to the server). | 120 // to the server). |
110 void OnNetworkChange(); | 121 void OnNetworkChange(); |
111 | 122 |
112 // Used by tests. | 123 // Used by tests. |
113 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } | 124 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } |
114 | 125 |
115 private: | 126 private: |
127 friend class URLRequestThrottlerEntry; | |
128 | |
116 // From each URL we generate an ID composed of the scheme, host, port and path | 129 // From each URL we generate an ID composed of the scheme, host, port and path |
117 // that allows us to uniquely map an entry to it. | 130 // that allows us to uniquely map an entry to it. |
118 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > | 131 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > |
119 UrlEntryMap; | 132 UrlEntryMap; |
120 | 133 |
121 // We maintain a set of hosts that have opted out of exponential | 134 // We maintain a set of hosts that have opted out of exponential |
122 // back-off throttling. | 135 // back-off throttling. |
123 typedef std::set<std::string> OptOutHosts; | 136 typedef std::set<std::string> OptOutHosts; |
124 | 137 |
125 // Maximum number of entries that we are willing to collect in our map. | 138 // Maximum number of entries that we are willing to collect in our map. |
126 static const unsigned int kMaximumNumberOfEntries; | 139 static const unsigned int kMaximumNumberOfEntries; |
127 // Number of requests that will be made between garbage collection. | 140 // Number of requests that will be made between garbage collection. |
128 static const unsigned int kRequestsBetweenCollecting; | 141 static const unsigned int kRequestsBetweenCollecting; |
129 | 142 |
130 // Map that contains a list of URL ID and their matching | 143 // Map that contains a list of URL ID and their matching |
131 // URLRequestThrottlerEntry. | 144 // URLRequestThrottlerEntry. |
132 UrlEntryMap url_entries_; | 145 UrlEntryMap url_entries_; |
133 | 146 |
134 // Set of hosts that have opted out. | 147 // Set of hosts that have opted out. |
135 OptOutHosts opt_out_hosts_; | 148 OptOutHosts opt_out_hosts_; |
136 | 149 |
137 // This keeps track of how many requests have been made. Used with | 150 // This keeps track of how many requests have been made. Used with |
138 // GarbageCollectEntries. | 151 // GarbageCollectEntries. |
139 unsigned int requests_since_last_gc_; | 152 unsigned int requests_since_last_gc_; |
140 | 153 |
141 // Valid after construction. | 154 // Valid after construction. |
142 GURL::Replacements url_id_replacements_; | 155 GURL::Replacements url_id_replacements_; |
143 | 156 |
144 // Whether we would like to reject outgoing HTTP requests during the back-off | |
145 // period. | |
146 bool enforce_throttling_; | |
147 | |
148 // Certain tests do not obey the net component's threading policy, so we | 157 // Certain tests do not obey the net component's threading policy, so we |
149 // keep track of whether we're being used by tests, and turn off certain | 158 // keep track of whether we're being used by tests, and turn off certain |
150 // checks. | 159 // checks. |
151 // | 160 // |
152 // TODO(joi): See if we can fix the offending unit tests and remove this | 161 // TODO(joi): See if we can fix the offending unit tests and remove this |
153 // workaround. | 162 // workaround. |
154 bool enable_thread_checks_; | 163 bool enable_thread_checks_; |
155 | 164 |
156 // Initially false, switches to true once we have logged because of back-off | 165 // Initially false, switches to true once we have logged because of back-off |
157 // being disabled for localhost. | 166 // being disabled for localhost. |
158 bool logged_for_localhost_disabled_; | 167 bool logged_for_localhost_disabled_; |
159 | 168 |
160 // NetLog to use, if configured. | 169 // NetLog to use, if configured. |
161 BoundNetLog net_log_; | 170 BoundNetLog net_log_; |
162 | 171 |
163 // Valid once we've registered for network notifications. | 172 // Valid once we've registered for network notifications. |
164 base::PlatformThreadId registered_from_thread_; | 173 base::PlatformThreadId registered_from_thread_; |
165 | 174 |
175 URLRequestThrottlerManagerDelegate* delegate_; | |
176 | |
166 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); | 177 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); |
167 }; | 178 }; |
168 | 179 |
169 } // namespace net | 180 } // namespace net |
170 | 181 |
171 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 182 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
OLD | NEW |