[email protected] | fba65f7b | 2012-03-15 05:22:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_request_throttler_manager.h" |
| 6 | |
[email protected] | b1a761e | 2011-02-14 23:17:47 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
tfarina | 7ba5a62 | 2016-02-23 23:21:44 | [diff] [blame] | 9 | #include "net/base/url_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 10 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 11 | #include "net/log/net_log_event_type.h" |
| 12 | #include "net/log/net_log_source_type.h" |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
| 16 | const unsigned int URLRequestThrottlerManager::kMaximumNumberOfEntries = 1500; |
| 17 | const unsigned int URLRequestThrottlerManager::kRequestsBetweenCollecting = 200; |
| 18 | |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 19 | URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 20 | : requests_since_last_gc_(0), |
[email protected] | 332a8c96 | 2013-07-17 22:31:23 | [diff] [blame] | 21 | enable_thread_checks_(false), |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 22 | logged_for_localhost_disabled_(false), |
| 23 | registered_from_thread_(base::kInvalidThreadId) { |
| 24 | url_id_replacements_.ClearPassword(); |
| 25 | url_id_replacements_.ClearUsername(); |
| 26 | url_id_replacements_.ClearQuery(); |
| 27 | url_id_replacements_.ClearRef(); |
| 28 | |
| 29 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 30 | NetworkChangeNotifier::AddConnectionTypeObserver(this); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | URLRequestThrottlerManager::~URLRequestThrottlerManager() { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 34 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 35 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 36 | NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 37 | |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 38 | // Since the manager object might conceivably go away before the |
| 39 | // entries, detach the entries' back-pointer to the manager. |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 40 | UrlEntryMap::iterator i = url_entries_.begin(); |
| 41 | while (i != url_entries_.end()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 42 | if (i->second.get() != NULL) { |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 43 | i->second->DetachManager(); |
| 44 | } |
| 45 | ++i; |
| 46 | } |
| 47 | |
| 48 | // Delete all entries. |
| 49 | url_entries_.clear(); |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | scoped_refptr<URLRequestThrottlerEntryInterface> |
| 53 | URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 54 | #if DCHECK_IS_ON() |
| 55 | DCHECK(!enable_thread_checks_ || thread_checker_.CalledOnValidThread()); |
| 56 | #endif |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 57 | |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 58 | // Normalize the url. |
| 59 | std::string url_id = GetIdFromUrl(url); |
| 60 | |
| 61 | // Periodically garbage collect old entries. |
| 62 | GarbageCollectEntriesIfNecessary(); |
| 63 | |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 64 | // Find the entry in the map or create a new NULL entry. |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 65 | scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 66 | |
| 67 | // If the entry exists but could be garbage collected at this point, we |
| 68 | // start with a fresh entry so that we possibly back off a bit less |
| 69 | // aggressively (i.e. this resets the error count when the entry's URL |
| 70 | // hasn't been requested in long enough). |
| 71 | if (entry.get() && entry->IsEntryOutdated()) { |
| 72 | entry = NULL; |
| 73 | } |
| 74 | |
| 75 | // Create the entry if needed. |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 76 | if (entry.get() == NULL) { |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 77 | entry = new URLRequestThrottlerEntry(this, url_id); |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 78 | |
| 79 | // We only disable back-off throttling on an entry that we have |
| 80 | // just constructed. This is to allow unit tests to explicitly override |
xunjieli | 041e939 | 2015-05-19 21:51:33 | [diff] [blame] | 81 | // the entry for localhost URLs. |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 82 | std::string host = url.host(); |
xunjieli | 041e939 | 2015-05-19 21:51:33 | [diff] [blame] | 83 | if (IsLocalhost(host)) { |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 84 | if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { |
| 85 | logged_for_localhost_disabled_ = true; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 86 | net_log_.AddEvent(NetLogEventType::THROTTLING_DISABLED_FOR_HOST, |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 87 | NetLog::StringCallback("host", &host)); |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 332a8c96 | 2013-07-17 22:31:23 | [diff] [blame] | 90 | // TODO(joi): Once sliding window is separate from back-off throttling, |
| 91 | // we can simply return a dummy implementation of |
xunjieli | 041e939 | 2015-05-19 21:51:33 | [diff] [blame] | 92 | // URLRequestThrottlerEntryInterface here that never blocks anything. |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 93 | entry->DisableBackoffThrottling(); |
| 94 | } |
| 95 | } |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 96 | |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 97 | return entry; |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 100 | void URLRequestThrottlerManager::OverrideEntryForTests( |
| 101 | const GURL& url, |
| 102 | URLRequestThrottlerEntry* entry) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 103 | // Normalize the url. |
| 104 | std::string url_id = GetIdFromUrl(url); |
| 105 | |
| 106 | // Periodically garbage collect old entries. |
| 107 | GarbageCollectEntriesIfNecessary(); |
| 108 | |
| 109 | url_entries_[url_id] = entry; |
| 110 | } |
| 111 | |
| 112 | void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { |
| 113 | // Normalize the url. |
| 114 | std::string url_id = GetIdFromUrl(url); |
| 115 | url_entries_.erase(url_id); |
| 116 | } |
| 117 | |
[email protected] | 332a8c96 | 2013-07-17 22:31:23 | [diff] [blame] | 118 | void URLRequestThrottlerManager::set_enable_thread_checks(bool enable) { |
| 119 | enable_thread_checks_ = enable; |
| 120 | } |
| 121 | |
| 122 | bool URLRequestThrottlerManager::enable_thread_checks() const { |
| 123 | return enable_thread_checks_; |
| 124 | } |
| 125 | |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 126 | void URLRequestThrottlerManager::set_net_log(NetLog* net_log) { |
| 127 | DCHECK(net_log); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 128 | net_log_ = NetLogWithSource::Make( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 129 | net_log, NetLogSourceType::EXPONENTIAL_BACKOFF_THROTTLING); |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | NetLog* URLRequestThrottlerManager::net_log() const { |
[email protected] | fba65f7b | 2012-03-15 05:22:13 | [diff] [blame] | 133 | return net_log_.net_log(); |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void URLRequestThrottlerManager::OnIPAddressChanged() { |
| 137 | OnNetworkChange(); |
| 138 | } |
| 139 | |
[email protected] | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 140 | void URLRequestThrottlerManager::OnConnectionTypeChanged( |
| 141 | NetworkChangeNotifier::ConnectionType type) { |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 142 | OnNetworkChange(); |
| 143 | } |
| 144 | |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 145 | std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { |
| 146 | if (!url.is_valid()) |
| 147 | return url.possibly_invalid_spec(); |
| 148 | |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 149 | GURL id = url.ReplaceComponents(url_id_replacements_); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 150 | return base::ToLowerASCII(id.spec()); |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 153 | void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { |
| 154 | requests_since_last_gc_++; |
| 155 | if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 156 | return; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 157 | requests_since_last_gc_ = 0; |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 158 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 159 | GarbageCollectEntries(); |
| 160 | } |
| 161 | |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 162 | void URLRequestThrottlerManager::GarbageCollectEntries() { |
[email protected] | c35d550 | 2011-03-02 01:27:14 | [diff] [blame] | 163 | UrlEntryMap::iterator i = url_entries_.begin(); |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 164 | while (i != url_entries_.end()) { |
[email protected] | a1077c34 | 2011-04-05 13:53:00 | [diff] [blame] | 165 | if ((i->second)->IsEntryOutdated()) { |
[email protected] | 30e427d | 2011-02-25 02:26:47 | [diff] [blame] | 166 | url_entries_.erase(i++); |
| 167 | } else { |
| 168 | ++i; |
| 169 | } |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // In case something broke we want to make sure not to grow indefinitely. |
| 173 | while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 174 | url_entries_.erase(url_entries_.begin()); |
| 175 | } |
| 176 | } |
| 177 | |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 178 | void URLRequestThrottlerManager::OnNetworkChange() { |
| 179 | // Remove all entries. Any entries that in-flight requests have a reference |
| 180 | // to will live until those requests end, and these entries may be |
| 181 | // inconsistent with new entries for the same URLs, but since what we |
[email protected] | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 182 | // want is a clean slate for the new connection type, this is OK. |
[email protected] | 82c71ba | 2011-05-27 18:51:53 | [diff] [blame] | 183 | url_entries_.clear(); |
| 184 | requests_since_last_gc_ = 0; |
| 185 | } |
| 186 | |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 187 | } // namespace net |