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

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

Issue 6966038: Anti-DDoS enhancements: Log to net log, UMA stats, improved policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove NON_EXPORTED_BASE where not needed. Created 9 years, 7 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) 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_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/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
16 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/non_thread_safe.h"
17 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
18 #include "net/base/net_api.h" 18 #include "net/base/net_api.h"
19 #include "net/base/network_change_notifier.h"
19 #include "net/url_request/url_request_throttler_entry.h" 20 #include "net/url_request/url_request_throttler_entry.h"
20 21
21 namespace net { 22 namespace net {
22 23
24 class BoundNetLog;
25 class NetLog;
26
23 // Class that registers URL request throttler entries for URLs being accessed 27 // Class that registers URL request throttler entries for URLs being accessed
24 // in order to supervise traffic. URL requests for HTTP contents should 28 // in order to supervise traffic. URL requests for HTTP contents should
25 // register their URLs in this manager on each request. 29 // register their URLs in this manager on each request.
26 // 30 //
27 // URLRequestThrottlerManager maintains a map of URL IDs to URL request 31 // URLRequestThrottlerManager maintains a map of URL IDs to URL request
28 // throttler entries. It creates URL request throttler entries when new URLs 32 // throttler entries. It creates URL request throttler entries when new URLs
29 // are registered, and does garbage collection from time to time in order to 33 // are registered, and does garbage collection from time to time in order to
30 // clean out outdated entries. URL ID consists of lowercased scheme, host, port 34 // clean out outdated entries. URL ID consists of lowercased scheme, host, port
31 // and path. All URLs converted to the same ID will share the same entry. 35 // and path. All URLs converted to the same ID will share the same entry.
32 // 36 //
33 // NOTE: All usage of this singleton object must be on the same thread, 37 // NOTE: All usage of this singleton object must be on the same thread,
34 // although to allow it to be used as a singleton, construction and destruction 38 // although to allow it to be used as a singleton, construction and destruction
35 // can occur on a separate thread. 39 // can occur on a separate thread.
36 class NET_API URLRequestThrottlerManager 40 class URLRequestThrottlerManager
37 : NON_EXPORTED_BASE(public base::NonThreadSafe) { 41 : NON_EXPORTED_BASE(public base::NonThreadSafe),
42 public NetworkChangeNotifier::IPAddressObserver,
43 public NetworkChangeNotifier::OnlineStateObserver {
38 public: 44 public:
39 static URLRequestThrottlerManager* GetInstance(); 45 static URLRequestThrottlerManager* GetInstance();
40 46
41 // Must be called for every request, returns the URL request throttler entry 47 // Must be called for every request, returns the URL request throttler entry
42 // associated with the URL. The caller must inform this entry of some events. 48 // associated with the URL. The caller must inform this entry of some events.
43 // Please refer to url_request_throttler_entry_interface.h for further 49 // Please refer to url_request_throttler_entry_interface.h for further
44 // informations. 50 // informations.
45 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( 51 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl(
46 const GURL& url); 52 const GURL& url);
47 53
(...skipping 16 matching lines...) Expand all
64 // Turns threading model verification on or off. Any code that correctly 70 // Turns threading model verification on or off. Any code that correctly
65 // uses the network stack should preferably call this function to enable 71 // uses the network stack should preferably call this function to enable
66 // verification of correct adherence to the network stack threading model. 72 // verification of correct adherence to the network stack threading model.
67 void set_enable_thread_checks(bool enable); 73 void set_enable_thread_checks(bool enable);
68 bool enable_thread_checks() const; 74 bool enable_thread_checks() const;
69 75
70 // Whether throttling is enabled or not. 76 // Whether throttling is enabled or not.
71 void set_enforce_throttling(bool enforce); 77 void set_enforce_throttling(bool enforce);
72 bool enforce_throttling(); 78 bool enforce_throttling();
73 79
80 // Sets the NetLog instance to use.
81 void set_net_log(NetLog* net_log);
82 NetLog* net_log() const;
83
84 // IPAddressObserver interface.
85 void OnIPAddressChanged() OVERRIDE;
yzshen1 2011/05/25 03:49:06 Might be good to add 'virtual'.
Jói 2011/05/25 14:30:35 Done.
86
87 // OnlineStateObserver interface.
88 void OnOnlineStateChanged(bool online) OVERRIDE;
yzshen1 2011/05/25 03:49:06 ditto.
Jói 2011/05/25 14:30:35 Done.
89
74 protected: 90 protected:
75 URLRequestThrottlerManager(); 91 URLRequestThrottlerManager();
76 ~URLRequestThrottlerManager(); 92 ~URLRequestThrottlerManager();
77 93
78 // Method that allows us to transform a URL into an ID that can be used in our 94 // Method that allows us to transform a URL into an ID that can be used in our
79 // map. Resulting IDs will be lowercase and consist of the scheme, host, port 95 // map. Resulting IDs will be lowercase and consist of the scheme, host, port
80 // and path (without query string, fragment, etc.). 96 // and path (without query string, fragment, etc.).
81 // If the URL is invalid, the invalid spec will be returned, without any 97 // If the URL is invalid, the invalid spec will be returned, without any
82 // transformation. 98 // transformation.
83 std::string GetIdFromUrl(const GURL& url) const; 99 std::string GetIdFromUrl(const GURL& url) const;
84 100
85 // Method that ensures the map gets cleaned from time to time. The period at 101 // Method that ensures the map gets cleaned from time to time. The period at
86 // which garbage collecting happens is adjustable with the 102 // which garbage collecting happens is adjustable with the
87 // kRequestBetweenCollecting constant. 103 // kRequestBetweenCollecting constant.
88 void GarbageCollectEntriesIfNecessary(); 104 void GarbageCollectEntriesIfNecessary();
89 105
90 // Method that does the actual work of garbage collecting. 106 // Method that does the actual work of garbage collecting.
91 void GarbageCollectEntries(); 107 void GarbageCollectEntries();
92 108
109 // When we switch from online to offline or change IP addresses, we
110 // clear all back-off history. This is a precaution in case the change in
111 // online state now lets us communicate without error with servers that
112 // we were previously getting 500 or 503 responses from (perhaps the
113 // responses are from a badly-written proxy that should have returned a
114 // 502 or 504 because it's upstream connection was down or it had no route
115 // to the server).
116 void OnNetworkChange();
117
93 // Used by tests. 118 // Used by tests.
94 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } 119 int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
95 120
96 private: 121 private:
97 friend struct DefaultSingletonTraits<URLRequestThrottlerManager>; 122 friend struct DefaultSingletonTraits<URLRequestThrottlerManager>;
98 123
99 // From each URL we generate an ID composed of the scheme, host, port and path 124 // From each URL we generate an ID composed of the scheme, host, port and path
100 // that allows us to uniquely map an entry to it. 125 // that allows us to uniquely map an entry to it.
101 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > 126 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> >
102 UrlEntryMap; 127 UrlEntryMap;
(...skipping 26 matching lines...) Expand all
129 bool enforce_throttling_; 154 bool enforce_throttling_;
130 155
131 // Certain tests do not obey the net component's threading policy, so we 156 // Certain tests do not obey the net component's threading policy, so we
132 // keep track of whether we're being used by tests, and turn off certain 157 // keep track of whether we're being used by tests, and turn off certain
133 // checks. 158 // checks.
134 // 159 //
135 // TODO(joi): See if we can fix the offending unit tests and remove this 160 // TODO(joi): See if we can fix the offending unit tests and remove this
136 // workaround. 161 // workaround.
137 bool enable_thread_checks_; 162 bool enable_thread_checks_;
138 163
164 // Initially false, switches to true once we have logged because of back-off
165 // being disabled for localhost.
166 bool logged_for_localhost_disabled_;
167
168 // NetLog to use, or NULL if none configured.
169 scoped_ptr<BoundNetLog> net_log_;
170
139 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); 171 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager);
140 }; 172 };
141 173
142 } // namespace net 174 } // namespace net
143 175
144 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ 176 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698