blob: 104ac177d22af042f6de139a0df3b1a1dd016fc6 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]aea80602009-09-18 00:55:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Bence Béky94658bf2018-05-11 19:22:585#include "net/spdy/spdy_session_pool.h"
[email protected]aea80602009-09-18 00:55:086
Eric Orthac661912022-01-10 21:44:177#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
9
Hans Wennborg0924470b2020-04-27 21:08:0510#include "base/check_op.h"
Lei Zhangde197672021-04-29 08:11:2411#include "base/containers/contains.h"
Avi Drissman41c4a412023-01-11 22:45:3712#include "base/functional/bind.h"
asvitkinec3c93722015-06-17 14:48:3713#include "base/metrics/histogram_macros.h"
Peter Kastinge1961a9f2022-08-30 17:26:4614#include "base/ranges/algorithm.h"
Sean Maher5b9af51f2022-11-21 15:32:4715#include "base/task/single_thread_task_runner.h"
[email protected]1ce7b66b2010-10-12 20:32:4416#include "base/values.h"
Bence Béky3ba8c332017-12-11 20:19:2417#include "build/build_config.h"
Tsuyoshi Horo7c9c0362022-08-19 00:47:2918#include "net/base/ip_endpoint.h"
xunjieli0b7f5b62016-12-06 20:43:4819#include "net/base/trace_constants.h"
Stefano Duo6bfd45d2023-04-03 16:38:2220#include "net/base/tracing.h"
Eric Orthc1fc6912019-02-15 19:33:0821#include "net/dns/host_resolver.h"
Matt Menke991dd7e2021-07-22 17:31:5122#include "net/dns/public/host_resolver_source.h"
[email protected]f4580332010-09-25 21:20:2723#include "net/http/http_network_session.h"
[email protected]53bfa31c2011-11-15 19:20:3124#include "net/http/http_server_properties.h"
Bence Béky6b44abf2018-04-11 10:32:5125#include "net/http/http_stream_request.h"
mikecirone8b85c432016-09-08 19:11:0026#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1927#include "net/log/net_log_source.h"
28#include "net/log/net_log_with_source.h"
Matt Menke3c5581752019-03-27 01:19:1829#include "net/socket/client_socket_handle.h"
Bence Béky94658bf2018-05-11 19:22:5830#include "net/spdy/spdy_session.h"
Ryan Hamiltonea4fa192022-04-12 18:30:4931#include "net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_constants.h"
32#include "net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_static_table.h"
[email protected]aea80602009-09-18 00:55:0833
34namespace net {
35
[email protected]8b114dd72011-03-25 05:33:0236namespace {
37
38enum SpdySessionGetTypes {
39 CREATED_NEW = 0,
40 FOUND_EXISTING = 1,
41 FOUND_EXISTING_FROM_IP_POOL = 2,
42 IMPORTED_FROM_SOCKET = 3,
43 SPDY_SESSION_GET_MAX = 4
44};
45
[email protected]41d64e82013-07-03 22:44:2646} // namespace
[email protected]aea80602009-09-18 00:55:0847
Matt Menke9bcb57b2019-04-12 19:09:3448SpdySessionPool::SpdySessionRequest::Delegate::Delegate() = default;
49SpdySessionPool::SpdySessionRequest::Delegate::~Delegate() = default;
50
51SpdySessionPool::SpdySessionRequest::SpdySessionRequest(
52 const SpdySessionKey& key,
Matt Menke2ab5fe792019-04-18 18:14:1353 bool enable_ip_based_pooling,
Matt Menke77173952019-04-18 17:42:1454 bool is_websocket,
Matt Menke9338c3792019-04-19 19:16:4955 bool is_blocking_request_for_session,
Matt Menke9bcb57b2019-04-12 19:09:3456 Delegate* delegate,
57 SpdySessionPool* spdy_session_pool)
Matt Menke77173952019-04-18 17:42:1458 : key_(key),
Matt Menke2ab5fe792019-04-18 18:14:1359 enable_ip_based_pooling_(enable_ip_based_pooling),
Matt Menke77173952019-04-18 17:42:1460 is_websocket_(is_websocket),
Matt Menke9338c3792019-04-19 19:16:4961 is_blocking_request_for_session_(is_blocking_request_for_session),
Matt Menke77173952019-04-18 17:42:1462 delegate_(delegate),
63 spdy_session_pool_(spdy_session_pool) {}
Matt Menke9bcb57b2019-04-12 19:09:3464
65SpdySessionPool::SpdySessionRequest::~SpdySessionRequest() {
66 if (spdy_session_pool_)
67 spdy_session_pool_->RemoveRequestForSpdySession(this);
68}
69
70void SpdySessionPool::SpdySessionRequest::OnRemovedFromPool() {
71 DCHECK(spdy_session_pool_);
72 spdy_session_pool_ = nullptr;
73}
74
[email protected]61b4efc2012-04-27 18:12:5075SpdySessionPool::SpdySessionPool(
76 HostResolver* resolver,
David Benjamin151ec6b2019-08-02 19:38:5277 SSLClientContext* ssl_client_context,
bnc525e175a2016-06-20 12:36:4078 HttpServerProperties* http_server_properties,
[email protected]5db452202014-08-19 05:22:1579 TransportSecurityState* transport_security_state,
Nick Harper23290b82019-05-02 00:02:5680 const quic::ParsedQuicVersionVector& quic_supported_versions,
[email protected]f9cf5572012-12-04 15:52:0981 bool enable_ping_based_connection_checking,
Ryan Hamilton0246af92019-08-30 19:28:5682 bool is_http2_enabled,
83 bool is_quic_enabled,
bnc8f0f3b62015-04-08 04:37:2384 size_t session_max_recv_window_size,
David Schinazi410676a2019-08-13 22:31:0585 int session_max_queued_capped_frames,
Ryan Hamilton0239aac2018-05-19 00:03:1386 const spdy::SettingsMap& initial_settings,
Bence Békyd228fb22021-10-29 14:45:5187 bool enable_http2_settings_grease,
Anton Bikineev068d2912021-05-15 20:43:5288 const absl::optional<GreasedHttp2Frame>& greased_http2_frame,
Bence Békycb4be9e2020-07-09 11:18:1489 bool http2_end_stream_with_data_frame,
Bence Békye84179d2021-01-13 14:36:1690 bool enable_priority_update,
Stefano Duo76e1f8402021-10-25 10:27:5391 bool go_away_on_ip_change,
Tarun Bansal647b30d2018-10-19 21:21:4792 SpdySessionPool::TimeFunc time_func,
Stefano Duoe0b077d2022-02-03 11:16:5993 NetworkQualityEstimator* network_quality_estimator,
94 bool cleanup_sessions_on_ip_address_changed)
[email protected]53bfa31c2011-11-15 19:20:3195 : http_server_properties_(http_server_properties),
[email protected]5db452202014-08-19 05:22:1596 transport_security_state_(transport_security_state),
David Benjamin151ec6b2019-08-02 19:38:5297 ssl_client_context_(ssl_client_context),
[email protected]b9ec6882011-07-01 07:40:2698 resolver_(resolver),
zhongyi8ac630d2017-06-26 19:46:3399 quic_supported_versions_(quic_supported_versions),
[email protected]f9cf5572012-12-04 15:52:09100 enable_ping_based_connection_checking_(
101 enable_ping_based_connection_checking),
Ryan Hamilton0246af92019-08-30 19:28:56102 is_http2_enabled_(is_http2_enabled),
103 is_quic_enabled_(is_quic_enabled),
bnc8f0f3b62015-04-08 04:37:23104 session_max_recv_window_size_(session_max_recv_window_size),
David Schinazi410676a2019-08-13 22:31:05105 session_max_queued_capped_frames_(session_max_queued_capped_frames),
bnc3171a2432016-12-28 18:40:26106 initial_settings_(initial_settings),
Bence Békyd228fb22021-10-29 14:45:51107 enable_http2_settings_grease_(enable_http2_settings_grease),
Bence Béky6a070bcd2018-09-06 15:02:43108 greased_http2_frame_(greased_http2_frame),
Bence Békycb4be9e2020-07-09 11:18:14109 http2_end_stream_with_data_frame_(http2_end_stream_with_data_frame),
Bence Békye84179d2021-01-13 14:36:16110 enable_priority_update_(enable_priority_update),
Stefano Duo76e1f8402021-10-25 10:27:53111 go_away_on_ip_change_(go_away_on_ip_change),
[email protected]f9cf5572012-12-04 15:52:09112 time_func_(time_func),
Stefano Duoe0b077d2022-02-03 11:16:59113 network_quality_estimator_(network_quality_estimator),
114 cleanup_sessions_on_ip_address_changed_(
115 cleanup_sessions_on_ip_address_changed) {
116 if (cleanup_sessions_on_ip_address_changed_)
117 NetworkChangeNotifier::AddIPAddressObserver(this);
David Benjamin151ec6b2019-08-02 19:38:52118 if (ssl_client_context_)
119 ssl_client_context_->AddObserver(this);
[email protected]b846acd2010-06-07 18:13:10120}
121
[email protected]955fc2e72010-02-08 20:37:30122SpdySessionPool::~SpdySessionPool() {
Matt Menkea6f2e8f2019-05-29 22:54:35123#if DCHECK_IS_ON()
124 for (const auto& request_info : spdy_session_request_map_) {
125 // The should be no pending SpdySessionRequests on destruction, though there
126 // may be callbacks waiting to be invoked, since they use weak pointers and
127 // there's no API to unregister them.
128 DCHECK(request_info.second.request_set.empty());
129 }
130#endif // DCHECK_IS_ON()
131
Bence Béky3ba8c332017-12-11 20:19:24132 // TODO(bnc): CloseAllSessions() is also called in HttpNetworkSession
133 // destructor, one of the two calls should be removed.
[email protected]d1eda932009-11-04 01:03:10134 CloseAllSessions();
[email protected]b846acd2010-06-07 18:13:10135
[email protected]975da41a2014-06-05 03:36:24136 while (!sessions_.empty()) {
137 // Destroy sessions to enforce that lifetime is scoped to SpdySessionPool.
138 // Write callbacks queued upon session drain are not invoked.
139 RemoveUnavailableSession((*sessions_.begin())->GetWeakPtr());
140 }
141
David Benjamin151ec6b2019-08-02 19:38:52142 if (ssl_client_context_)
143 ssl_client_context_->RemoveObserver(this);
Stefano Duoe0b077d2022-02-03 11:16:59144 if (cleanup_sessions_on_ip_address_changed_)
145 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]d1eda932009-11-04 01:03:10146}
[email protected]aea80602009-09-18 00:55:08147
Bence Béky01d4c232021-03-23 03:09:17148int SpdySessionPool::CreateAvailableSessionFromSocketHandle(
Matt Menkee1d8fe92019-02-07 05:30:54149 const SpdySessionKey& key,
Matt Menkee1d8fe92019-02-07 05:30:54150 std::unique_ptr<ClientSocketHandle> client_socket_handle,
Bence Béky01d4c232021-03-23 03:09:17151 const NetLogWithSource& net_log,
152 base::WeakPtr<SpdySession>* session) {
Matt Menkee1d8fe92019-02-07 05:30:54153 TRACE_EVENT0(NetTracingCategory(),
154 "SpdySessionPool::CreateAvailableSessionFromSocketHandle");
155
156 std::unique_ptr<SpdySession> new_session =
Eric Orthfc0583b2021-05-18 19:13:59157 CreateSession(key, net_log.net_log());
Eric Orthac661912022-01-10 21:44:17158 std::set<std::string> dns_aliases =
Cammie Smith Barnes5191037b72021-03-01 22:06:53159 client_socket_handle->socket()->GetDnsAliases();
160
Matt Menkee1d8fe92019-02-07 05:30:54161 new_session->InitializeWithSocketHandle(std::move(client_socket_handle),
162 this);
Bence Béky01d4c232021-03-23 03:09:17163 *session = InsertSession(key, std::move(new_session), net_log,
164 std::move(dns_aliases));
165
166 if (!(*session)->HasAcceptableTransportSecurity()) {
167 (*session)->CloseSessionOnError(ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY,
168 "");
169 return ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY;
170 }
171
Bence Béky78b542c2021-03-25 19:30:37172 int rv = (*session)->ParseAlps();
173 if (rv != OK) {
174 DCHECK_NE(ERR_IO_PENDING, rv);
175 // ParseAlps() already closed the connection on error.
176 return rv;
177 }
178
Bence Béky01d4c232021-03-23 03:09:17179 return OK;
Matt Menkee1d8fe92019-02-07 05:30:54180}
181
[email protected]09356c652014-03-25 15:36:10182base::WeakPtr<SpdySession> SpdySessionPool::CreateAvailableSessionFromSocket(
[email protected]04644d42013-07-08 04:29:59183 const SpdySessionKey& key,
Matt Menkee1d8fe92019-02-07 05:30:54184 std::unique_ptr<StreamSocket> socket_stream,
185 const LoadTimingInfo::ConnectTiming& connect_timing,
Bence Béky0ef1556e2017-06-30 19:52:52186 const NetLogWithSource& net_log) {
Alexandr Ilin33126632018-11-14 14:48:17187 TRACE_EVENT0(NetTracingCategory(),
xunjieli0b7f5b62016-12-06 20:43:48188 "SpdySessionPool::CreateAvailableSessionFromSocket");
[email protected]88a332622013-07-30 07:13:32189
Matt Menkee1d8fe92019-02-07 05:30:54190 std::unique_ptr<SpdySession> new_session =
Eric Orthfc0583b2021-05-18 19:13:59191 CreateSession(key, net_log.net_log());
Eric Orthac661912022-01-10 21:44:17192 std::set<std::string> dns_aliases = socket_stream->GetDnsAliases();
[email protected]04644d42013-07-08 04:29:59193
Matt Menkee1d8fe92019-02-07 05:30:54194 new_session->InitializeWithSocket(std::move(socket_stream), connect_timing,
195 this);
[email protected]04644d42013-07-08 04:29:59196
Cammie Smith Barnes5191037b72021-03-01 22:06:53197 return InsertSession(key, std::move(new_session), net_log,
198 std::move(dns_aliases));
[email protected]04644d42013-07-08 04:29:59199}
200
[email protected]795cbf82013-07-22 09:37:27201base::WeakPtr<SpdySession> SpdySessionPool::FindAvailableSession(
[email protected]04644d42013-07-08 04:29:59202 const SpdySessionKey& key,
bnc9ead3ae2017-03-16 00:48:15203 bool enable_ip_based_pooling,
Bence Békyd885b2b2018-02-03 00:58:31204 bool is_websocket,
tfarina428341112016-09-22 13:38:20205 const NetLogWithSource& net_log) {
jdoerrie22a91d8b92018-10-05 08:43:26206 auto it = LookupAvailableSessionByKey(key);
Matt Menkeea6c5b42019-05-02 17:21:16207 if (it == available_sessions_.end() ||
208 (is_websocket && !it->second->support_websocket())) {
Bence Béky53a5aef2018-03-29 21:54:12209 return base::WeakPtr<SpdySession>();
[email protected]41d64e82013-07-03 22:44:26210 }
211
Matt Menkeea6c5b42019-05-02 17:21:16212 if (key == it->second->spdy_session_key()) {
213 UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", FOUND_EXISTING,
214 SPDY_SESSION_GET_MAX);
Eric Roman06bd9742019-07-13 15:19:13215 net_log.AddEventReferencingSource(
Matt Menkeea6c5b42019-05-02 17:21:16216 NetLogEventType::HTTP2_SESSION_POOL_FOUND_EXISTING_SESSION,
Eric Roman06bd9742019-07-13 15:19:13217 it->second->net_log().source());
Matt Menkeea6c5b42019-05-02 17:21:16218 return it->second;
219 }
bnc9ead3ae2017-03-16 00:48:15220
Matt Menkeea6c5b42019-05-02 17:21:16221 if (enable_ip_based_pooling) {
222 UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", FOUND_EXISTING_FROM_IP_POOL,
223 SPDY_SESSION_GET_MAX);
Eric Roman06bd9742019-07-13 15:19:13224 net_log.AddEventReferencingSource(
Matt Menkeea6c5b42019-05-02 17:21:16225 NetLogEventType::HTTP2_SESSION_POOL_FOUND_EXISTING_SESSION_FROM_IP_POOL,
Eric Roman06bd9742019-07-13 15:19:13226 it->second->net_log().source());
Matt Menkeea6c5b42019-05-02 17:21:16227 return it->second;
[email protected]6cd3bd202010-08-30 05:23:06228 }
[email protected]aea80602009-09-18 00:55:08229
[email protected]795cbf82013-07-22 09:37:27230 return base::WeakPtr<SpdySession>();
[email protected]aea80602009-09-18 00:55:08231}
232
Fan Yangf7faef322022-04-29 01:42:42233bool SpdySessionPool::HasAvailableSession(const SpdySessionKey& key,
234 bool is_websocket) const {
235 const auto it = available_sessions_.find(key);
236 return it != available_sessions_.end() &&
237 (!is_websocket || it->second->support_websocket());
238}
239
Matt Menkecd55e922019-04-15 16:25:08240base::WeakPtr<SpdySession> SpdySessionPool::RequestSession(
241 const SpdySessionKey& key,
242 bool enable_ip_based_pooling,
243 bool is_websocket,
244 const NetLogWithSource& net_log,
Matt Menke9338c3792019-04-19 19:16:49245 base::RepeatingClosure on_blocking_request_destroyed_callback,
Matt Menkecd55e922019-04-15 16:25:08246 SpdySessionRequest::Delegate* delegate,
247 std::unique_ptr<SpdySessionRequest>* spdy_session_request,
Matt Menke9338c3792019-04-19 19:16:49248 bool* is_blocking_request_for_session) {
Matt Menkecd55e922019-04-15 16:25:08249 DCHECK(delegate);
250
251 base::WeakPtr<SpdySession> spdy_session =
252 FindAvailableSession(key, enable_ip_based_pooling, is_websocket, net_log);
253 if (spdy_session) {
254 // This value doesn't really matter, but best to always populate it, for
255 // consistency.
Matt Menke9338c3792019-04-19 19:16:49256 *is_blocking_request_for_session = true;
Matt Menkecd55e922019-04-15 16:25:08257 return spdy_session;
258 }
259
Matt Menke9338c3792019-04-19 19:16:49260 RequestInfoForKey* request_info = &spdy_session_request_map_[key];
261 *is_blocking_request_for_session = !request_info->has_blocking_request;
Matt Menke2ab5fe792019-04-18 18:14:13262 *spdy_session_request = std::make_unique<SpdySessionRequest>(
Matt Menke9338c3792019-04-19 19:16:49263 key, enable_ip_based_pooling, is_websocket,
264 *is_blocking_request_for_session, delegate, this);
265 request_info->request_set.insert(spdy_session_request->get());
Matt Menkecd55e922019-04-15 16:25:08266
Matt Menke9338c3792019-04-19 19:16:49267 if (*is_blocking_request_for_session) {
268 request_info->has_blocking_request = true;
269 } else if (on_blocking_request_destroyed_callback) {
270 request_info->deferred_callbacks.push_back(
271 on_blocking_request_destroyed_callback);
Matt Menkecd55e922019-04-15 16:25:08272 }
273 return nullptr;
274}
275
Matt Menke5062be22019-05-01 17:50:24276OnHostResolutionCallbackResult SpdySessionPool::OnHostResolutionComplete(
277 const SpdySessionKey& key,
278 bool is_websocket,
Tsuyoshi Horo7c9c0362022-08-19 00:47:29279 const std::vector<HostResolverEndpointResult>& endpoint_results,
280 const std::set<std::string>& aliases) {
Matt Menke5062be22019-05-01 17:50:24281 // If there are no pending requests for that alias, nothing to do.
282 if (spdy_session_request_map_.find(key) == spdy_session_request_map_.end())
283 return OnHostResolutionCallbackResult::kContinue;
284
285 // Check if there's already a matching session. If so, there may already
286 // be a pending task to inform consumers of the alias. In this case, do
287 // nothing, but inform the caller to wait for such a task to run.
288 auto existing_session_it = LookupAvailableSessionByKey(key);
289 if (existing_session_it != available_sessions_.end()) {
Adam Ricebeaae322021-06-30 23:56:29290 if (is_websocket && !existing_session_it->second->support_websocket()) {
291 // We don't look for aliased sessions because it would not be possible to
292 // add them to the available_sessions_ map. See https://crbug.com/1220771.
293 return OnHostResolutionCallbackResult::kContinue;
294 }
Matt Menke5062be22019-05-01 17:50:24295
Adam Ricebeaae322021-06-30 23:56:29296 return OnHostResolutionCallbackResult::kMayBeDeletedAsync;
Matt Menke5062be22019-05-01 17:50:24297 }
Tsuyoshi Horo7c9c0362022-08-19 00:47:29298
David Benjaminebf43c32023-05-19 21:21:52299 for (const auto& endpoint : endpoint_results) {
300 // If `endpoint` has no associated ALPN protocols, it is TCP-based and thus
301 // would have been eligible for connecting with HTTP/2.
302 if (!endpoint.metadata.supported_protocol_alpns.empty() &&
303 !base::Contains(endpoint.metadata.supported_protocol_alpns, "h2")) {
304 continue;
305 }
306 for (const auto& address : endpoint.ip_endpoints) {
307 auto range = aliases_.equal_range(address);
308 for (auto alias_it = range.first; alias_it != range.second; ++alias_it) {
309 // We found a potential alias.
310 const SpdySessionKey& alias_key = alias_it->second;
Matt Menke5062be22019-05-01 17:50:24311
David Benjaminebf43c32023-05-19 21:21:52312 auto available_session_it = LookupAvailableSessionByKey(alias_key);
313 // It shouldn't be in the aliases table if it doesn't exist!
314 DCHECK(available_session_it != available_sessions_.end());
Matt Menke5062be22019-05-01 17:50:24315
David Benjaminebf43c32023-05-19 21:21:52316 SpdySessionKey::CompareForAliasingResult compare_result =
317 alias_key.CompareForAliasing(key);
318 // Keys must be aliasable.
319 if (!compare_result.is_potentially_aliasable) {
Matt Menke5062be22019-05-01 17:50:24320 continue;
David Benjaminebf43c32023-05-19 21:21:52321 }
Matt Menke5062be22019-05-01 17:50:24322
David Benjaminebf43c32023-05-19 21:21:52323 if (is_websocket &&
324 !available_session_it->second->support_websocket()) {
Matt Menke5062be22019-05-01 17:50:24325 continue;
David Benjaminebf43c32023-05-19 21:21:52326 }
Matt Menke5062be22019-05-01 17:50:24327
David Benjaminebf43c32023-05-19 21:21:52328 // Make copy of WeakPtr as call to UnmapKey() will delete original.
329 const base::WeakPtr<SpdySession> available_session =
330 available_session_it->second;
Matt Menke5062be22019-05-01 17:50:24331
David Benjaminebf43c32023-05-19 21:21:52332 // Need to verify that the server is authenticated to serve traffic for
333 // |host_port_proxy_pair| too.
334 if (!available_session->VerifyDomainAuthentication(
335 key.host_port_pair().host())) {
336 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2);
337 continue;
338 }
Matt Menke5062be22019-05-01 17:50:24339
David Benjaminebf43c32023-05-19 21:21:52340 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2);
Matt Menke5062be22019-05-01 17:50:24341
David Benjaminebf43c32023-05-19 21:21:52342 bool adding_pooled_alias = true;
Matt Menke5062be22019-05-01 17:50:24343
David Benjaminebf43c32023-05-19 21:21:52344 // If socket tags differ, see if session's socket tag can be changed.
345 if (!compare_result.is_socket_tag_match) {
346 SpdySessionKey old_key = available_session->spdy_session_key();
347 SpdySessionKey new_key(old_key.host_port_pair(),
348 old_key.proxy_server(), old_key.privacy_mode(),
349 old_key.is_proxy_session(), key.socket_tag(),
350 old_key.network_anonymization_key(),
351 old_key.secure_dns_policy());
352
353 // If there is already a session with |new_key|, skip this one.
354 // It will be found in |aliases_| in a future iteration.
355 if (available_sessions_.find(new_key) != available_sessions_.end()) {
Matt Menke5062be22019-05-01 17:50:24356 continue;
357 }
Matt Menkeae58eeb2019-05-24 21:09:50358
David Benjaminebf43c32023-05-19 21:21:52359 if (!available_session->ChangeSocketTag(key.socket_tag())) {
360 continue;
361 }
Matt Menke5062be22019-05-01 17:50:24362
David Benjaminebf43c32023-05-19 21:21:52363 DCHECK(available_session->spdy_session_key() == new_key);
364
365 // If this isn't a pooled alias, but the actual session that needs to
366 // have its socket tag change, there's no need to add an alias.
367 if (new_key == key) {
Matt Menke5062be22019-05-01 17:50:24368 adding_pooled_alias = false;
David Benjaminebf43c32023-05-19 21:21:52369 }
370
371 // Remap main session key.
372 std::set<std::string> main_session_old_dns_aliases =
373 GetDnsAliasesForSessionKey(old_key);
374 UnmapKey(old_key);
375 MapKeyToAvailableSession(new_key, available_session,
376 std::move(main_session_old_dns_aliases));
377
378 // Remap alias. From this point on |alias_it| is invalid, so no more
379 // iterations of the loop should be allowed.
380 aliases_.insert(AliasMap::value_type(alias_it->first, new_key));
381 aliases_.erase(alias_it);
382
383 // Remap pooled session keys.
384 const auto& pooled_aliases = available_session->pooled_aliases();
385 for (auto it = pooled_aliases.begin(); it != pooled_aliases.end();) {
386 // Ignore aliases this loop is inserting.
387 if (it->socket_tag() == key.socket_tag()) {
388 ++it;
389 continue;
390 }
391
392 std::set<std::string> pooled_alias_old_dns_aliases =
393 GetDnsAliasesForSessionKey(*it);
394 UnmapKey(*it);
395 SpdySessionKey new_pool_alias_key = SpdySessionKey(
396 it->host_port_pair(), it->proxy_server(), it->privacy_mode(),
397 it->is_proxy_session(), key.socket_tag(),
398 it->network_anonymization_key(), it->secure_dns_policy());
399 MapKeyToAvailableSession(new_pool_alias_key, available_session,
400 std::move(pooled_alias_old_dns_aliases));
401 auto old_it = it;
402 ++it;
403 available_session->RemovePooledAlias(*old_it);
404 available_session->AddPooledAlias(new_pool_alias_key);
405
406 // If this is desired key, no need to add an alias for the desired
407 // key at the end of this method.
408 if (new_pool_alias_key == key) {
409 adding_pooled_alias = false;
410 }
411 }
Matt Menke5062be22019-05-01 17:50:24412 }
David Benjaminebf43c32023-05-19 21:21:52413
414 if (adding_pooled_alias) {
415 // Add this session to the map so that we can find it next time.
416 MapKeyToAvailableSession(key, available_session, aliases);
417 available_session->AddPooledAlias(key);
418 }
419
420 // Post task to inform pending requests for session for |key| that a
421 // matching session is now available.
422 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
423 FROM_HERE, base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
424 weak_ptr_factory_.GetWeakPtr(), key));
425
426 // Inform the caller that the Callback may be deleted if the consumer is
427 // switched over to the newly aliased session. It's not guaranteed to be
428 // deleted, as the session may be closed, or taken by yet another
429 // pending request with a different SocketTag before the the request can
430 // try and use the session.
431 return OnHostResolutionCallbackResult::kMayBeDeletedAsync;
Matt Menke5062be22019-05-01 17:50:24432 }
Matt Menke5062be22019-05-01 17:50:24433 }
434 }
435 return OnHostResolutionCallbackResult::kContinue;
436}
437
[email protected]04644d42013-07-08 04:29:59438void SpdySessionPool::MakeSessionUnavailable(
[email protected]795cbf82013-07-22 09:37:27439 const base::WeakPtr<SpdySession>& available_session) {
[email protected]04644d42013-07-08 04:29:59440 UnmapKey(available_session->spdy_session_key());
441 RemoveAliases(available_session->spdy_session_key());
442 const std::set<SpdySessionKey>& aliases = available_session->pooled_aliases();
Tsuyoshi Horo17ef47d02022-06-30 10:58:27443 for (const auto& alias : aliases) {
444 UnmapKey(alias);
445 RemoveAliases(alias);
[email protected]46da33be2011-07-19 21:58:04446 }
[email protected]04644d42013-07-08 04:29:59447 DCHECK(!IsSessionAvailable(available_session));
448}
[email protected]46da33be2011-07-19 21:58:04449
[email protected]04644d42013-07-08 04:29:59450void SpdySessionPool::RemoveUnavailableSession(
[email protected]795cbf82013-07-22 09:37:27451 const base::WeakPtr<SpdySession>& unavailable_session) {
[email protected]04644d42013-07-08 04:29:59452 DCHECK(!IsSessionAvailable(unavailable_session));
453
454 unavailable_session->net_log().AddEvent(
bncd4365542017-04-14 18:33:17455 NetLogEventType::HTTP2_SESSION_POOL_REMOVE_SESSION);
[email protected]04644d42013-07-08 04:29:59456
jdoerrie22a91d8b92018-10-05 08:43:26457 auto it = sessions_.find(unavailable_session.get());
[email protected]04644d42013-07-08 04:29:59458 CHECK(it != sessions_.end());
danakjaee3e1ec2016-04-16 00:23:18459 std::unique_ptr<SpdySession> owned_session(*it);
[email protected]04644d42013-07-08 04:29:59460 sessions_.erase(it);
[email protected]9f7c4fd2009-11-24 18:50:15461}
462
[email protected]41d64e82013-07-03 22:44:26463// Make a copy of |sessions_| in the Close* functions below to avoid
[email protected]04644d42013-07-08 04:29:59464// reentrancy problems. Since arbitrary functions get called by close
465// handlers, it doesn't suffice to simply increment the iterator
466// before closing.
[email protected]8b114dd72011-03-25 05:33:02467
ttuttle859dc7a2015-04-23 19:42:29468void SpdySessionPool::CloseCurrentSessions(Error error) {
[email protected]04644d42013-07-08 04:29:59469 CloseCurrentSessionsHelper(error, "Closing current sessions.",
470 false /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26471}
472
Nick Harper1f5bf272020-10-29 11:22:35473void SpdySessionPool::CloseCurrentIdleSessions(const std::string& description) {
474 CloseCurrentSessionsHelper(ERR_ABORTED, description, true /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26475}
476
477void SpdySessionPool::CloseAllSessions() {
Bence Béky3ba8c332017-12-11 20:19:24478 auto is_draining = [](const SpdySession* s) { return s->IsDraining(); };
479 // Repeat until every SpdySession owned by |this| is draining.
Peter Kastinge1961a9f2022-08-30 17:26:46480 while (!base::ranges::all_of(sessions_, is_draining)) {
[email protected]04644d42013-07-08 04:29:59481 CloseCurrentSessionsHelper(ERR_ABORTED, "Closing all sessions.",
482 false /* idle_only */);
[email protected]6cbfa852012-03-14 06:35:54483 }
484}
485
Matt Mueller0c1d46d22023-04-21 19:04:00486void SpdySessionPool::MakeCurrentSessionsGoingAway(Error error) {
487 WeakSessionList current_sessions = GetCurrentSessions();
488 for (base::WeakPtr<SpdySession>& session : current_sessions) {
489 if (!session) {
490 continue;
491 }
492
493 session->MakeUnavailable();
494 session->StartGoingAway(kLastStreamId, error);
495 session->MaybeFinishGoingAway();
496 DCHECK(!IsSessionAvailable(session));
497 }
498}
499
danakjaee3e1ec2016-04-16 00:23:18500std::unique_ptr<base::Value> SpdySessionPool::SpdySessionPoolInfoToValue()
501 const {
Matt Menkec9a0b382022-05-27 22:31:47502 base::Value::List list;
[email protected]1ce7b66b2010-10-12 20:32:44503
Tsuyoshi Horo17ef47d02022-06-30 10:58:27504 for (const auto& available_session : available_sessions_) {
[email protected]41d64e82013-07-03 22:44:26505 // Only add the session if the key in the map matches the main
506 // host_port_proxy_pair (not an alias).
Tsuyoshi Horo17ef47d02022-06-30 10:58:27507 const SpdySessionKey& key = available_session.first;
508 const SpdySessionKey& session_key =
509 available_session.second->spdy_session_key();
Bence Békya12c7702017-12-06 21:07:28510 if (key == session_key)
Tsuyoshi Horo17ef47d02022-06-30 10:58:27511 list.Append(available_session.second->GetInfoAsValue());
[email protected]1ce7b66b2010-10-12 20:32:44512 }
Matt Menkec9a0b382022-05-27 22:31:47513 return std::make_unique<base::Value>(std::move(list));
[email protected]1ce7b66b2010-10-12 20:32:44514}
515
[email protected]66761b952010-06-25 21:30:38516void SpdySessionPool::OnIPAddressChanged() {
Stefano Duoe0b077d2022-02-03 11:16:59517 DCHECK(cleanup_sessions_on_ip_address_changed_);
Matt Mueller0c1d46d22023-04-21 19:04:00518 if (go_away_on_ip_change_) {
519 MakeCurrentSessionsGoingAway(ERR_NETWORK_CHANGED);
520 } else {
521 CloseCurrentSessions(ERR_NETWORK_CHANGED);
[email protected]d9a2e412014-02-06 11:02:58522 }
[email protected]66761b952010-06-25 21:30:38523}
524
Matt Muellere37c0862023-04-08 00:17:07525void SpdySessionPool::OnSSLConfigChanged(
526 SSLClientContext::SSLConfigChangeType change_type) {
527 switch (change_type) {
528 case SSLClientContext::SSLConfigChangeType::kSSLConfigChanged:
Matt Mueller0c1d46d22023-04-21 19:04:00529 MakeCurrentSessionsGoingAway(ERR_NETWORK_CHANGED);
Matt Muellere37c0862023-04-08 00:17:07530 break;
531 case SSLClientContext::SSLConfigChangeType::kCertDatabaseChanged:
Matt Mueller0c1d46d22023-04-21 19:04:00532 MakeCurrentSessionsGoingAway(ERR_CERT_DATABASE_CHANGED);
Matt Muellere37c0862023-04-08 00:17:07533 break;
534 case SSLClientContext::SSLConfigChangeType::kCertVerifierChanged:
Matt Mueller0c1d46d22023-04-21 19:04:00535 MakeCurrentSessionsGoingAway(ERR_CERT_VERIFIER_CHANGED);
Matt Muellere37c0862023-04-08 00:17:07536 break;
537 };
[email protected]62635c72011-03-10 04:16:25538}
539
Matt Muellere82a5cf42023-06-14 02:12:30540void SpdySessionPool::OnSSLConfigForServersChanged(
541 const base::flat_set<HostPortPair>& servers) {
David Benjaminbac8dff2019-08-07 01:30:41542 WeakSessionList current_sessions = GetCurrentSessions();
543 for (base::WeakPtr<SpdySession>& session : current_sessions) {
544 if (!session)
545 continue;
546
547 const ProxyServer& proxy_server =
548 session->spdy_session_key().proxy_server();
Matt Muellere82a5cf42023-06-14 02:12:30549 if (servers.contains(session->host_port_pair()) ||
David Benjaminbac8dff2019-08-07 01:30:41550 (proxy_server.is_http_like() && !proxy_server.is_http() &&
Matt Muellere82a5cf42023-06-14 02:12:30551 servers.contains(proxy_server.host_port_pair()))) {
David Benjaminbac8dff2019-08-07 01:30:41552 session->MakeUnavailable();
David Benjamina68a7952021-05-26 23:26:15553 // Note this call preserves active streams but fails any streams that are
554 // waiting on a stream ID.
555 // TODO(https://crbug.com/1213609): This is not ideal, but SpdySession
556 // does not have a state that supports this.
557 session->StartGoingAway(kLastStreamId, ERR_NETWORK_CHANGED);
David Benjaminbac8dff2019-08-07 01:30:41558 session->MaybeFinishGoingAway();
559 DCHECK(!IsSessionAvailable(session));
560 }
561 }
562}
563
Eric Orthac661912022-01-10 21:44:17564std::set<std::string> SpdySessionPool::GetDnsAliasesForSessionKey(
Cammie Smith Barnes5191037b72021-03-01 22:06:53565 const SpdySessionKey& key) const {
566 auto it = dns_aliases_by_session_key_.find(key);
567 if (it == dns_aliases_by_session_key_.end())
568 return {};
569
570 return it->second;
571}
572
Matt Menke9bcb57b2019-04-12 19:09:34573void SpdySessionPool::RemoveRequestForSpdySession(SpdySessionRequest* request) {
574 DCHECK_EQ(this, request->spdy_session_pool());
575
Matt Menke9338c3792019-04-19 19:16:49576 auto iter = spdy_session_request_map_.find(request->key());
577 DCHECK(iter != spdy_session_request_map_.end());
578
579 // Resume all pending requests if it is the blocking request, which is either
580 // being canceled, or has completed.
581 if (request->is_blocking_request_for_session() &&
582 !iter->second.deferred_callbacks.empty()) {
Sean Maher5b9af51f2022-11-21 15:32:47583 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Matt Menke66e502a72019-04-15 13:34:38584 FROM_HERE,
585 base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
Matt Menke9338c3792019-04-19 19:16:49586 weak_ptr_factory_.GetWeakPtr(), request->key()));
Matt Menke66e502a72019-04-15 13:34:38587 }
xunjielia9972092017-06-15 15:57:05588
Jan Wilken Dörrie21f9de72019-06-07 10:41:53589 DCHECK(base::Contains(iter->second.request_set, request));
Matt Menke9338c3792019-04-19 19:16:49590 RemoveRequestInternal(iter, iter->second.request_set.find(request));
xunjieli87258b72017-06-08 14:19:52591}
592
Matt Menke9338c3792019-04-19 19:16:49593SpdySessionPool::RequestInfoForKey::RequestInfoForKey() = default;
594SpdySessionPool::RequestInfoForKey::~RequestInfoForKey() = default;
595
[email protected]04644d42013-07-08 04:29:59596bool SpdySessionPool::IsSessionAvailable(
[email protected]795cbf82013-07-22 09:37:27597 const base::WeakPtr<SpdySession>& session) const {
Tsuyoshi Horo17ef47d02022-06-30 10:58:27598 for (const auto& available_session : available_sessions_) {
599 if (available_session.second.get() == session.get())
[email protected]04644d42013-07-08 04:29:59600 return true;
601 }
602 return false;
603}
604
[email protected]04644d42013-07-08 04:29:59605void SpdySessionPool::MapKeyToAvailableSession(
606 const SpdySessionKey& key,
Cammie Smith Barnes5191037b72021-03-01 22:06:53607 const base::WeakPtr<SpdySession>& session,
Eric Orthac661912022-01-10 21:44:17608 std::set<std::string> dns_aliases) {
Jan Wilken Dörrie21f9de72019-06-07 10:41:53609 DCHECK(base::Contains(sessions_, session.get()));
[email protected]04644d42013-07-08 04:29:59610 std::pair<AvailableSessionMap::iterator, bool> result =
rchebf12982015-04-10 01:15:00611 available_sessions_.insert(std::make_pair(key, session));
[email protected]41d64e82013-07-03 22:44:26612 CHECK(result.second);
Cammie Smith Barnes5191037b72021-03-01 22:06:53613
614 dns_aliases_by_session_key_[key] = std::move(dns_aliases);
[email protected]aea80602009-09-18 00:55:08615}
616
[email protected]04644d42013-07-08 04:29:59617SpdySessionPool::AvailableSessionMap::iterator
618SpdySessionPool::LookupAvailableSessionByKey(
619 const SpdySessionKey& key) {
rchebf12982015-04-10 01:15:00620 return available_sessions_.find(key);
[email protected]d1eda932009-11-04 01:03:10621}
622
[email protected]04644d42013-07-08 04:29:59623void SpdySessionPool::UnmapKey(const SpdySessionKey& key) {
jdoerrie22a91d8b92018-10-05 08:43:26624 auto it = LookupAvailableSessionByKey(key);
[email protected]04644d42013-07-08 04:29:59625 CHECK(it != available_sessions_.end());
626 available_sessions_.erase(it);
Cammie Smith Barnes5191037b72021-03-01 22:06:53627 dns_aliases_by_session_key_.erase(key);
[email protected]8b114dd72011-03-25 05:33:02628}
629
[email protected]04644d42013-07-08 04:29:59630void SpdySessionPool::RemoveAliases(const SpdySessionKey& key) {
[email protected]8b114dd72011-03-25 05:33:02631 // Walk the aliases map, find references to this pair.
632 // TODO(mbelshe): Figure out if this is too expensive.
jdoerrie22a91d8b92018-10-05 08:43:26633 for (auto it = aliases_.begin(); it != aliases_.end();) {
Bence Békya12c7702017-12-06 21:07:28634 if (it->second == key) {
jdoerrie22a91d8b92018-10-05 08:43:26635 auto old_it = it;
[email protected]04644d42013-07-08 04:29:59636 ++it;
637 aliases_.erase(old_it);
638 } else {
639 ++it;
[email protected]8b114dd72011-03-25 05:33:02640 }
[email protected]04644d42013-07-08 04:29:59641 }
642}
643
[email protected]676438e2013-07-22 11:07:33644SpdySessionPool::WeakSessionList SpdySessionPool::GetCurrentSessions() const {
645 WeakSessionList current_sessions;
Tsuyoshi Horo17ef47d02022-06-30 10:58:27646 for (auto* session : sessions_) {
647 current_sessions.push_back(session->GetWeakPtr());
[email protected]676438e2013-07-22 11:07:33648 }
649 return current_sessions;
650}
651
diannahu401e364c2017-04-07 01:17:47652void SpdySessionPool::CloseCurrentSessionsHelper(Error error,
Bence Béky4e83f492018-05-13 23:14:25653 const std::string& description,
diannahu401e364c2017-04-07 01:17:47654 bool idle_only) {
[email protected]676438e2013-07-22 11:07:33655 WeakSessionList current_sessions = GetCurrentSessions();
Bence Béky3ba8c332017-12-11 20:19:24656 for (base::WeakPtr<SpdySession>& session : current_sessions) {
657 if (!session)
[email protected]04644d42013-07-08 04:29:59658 continue;
659
Bence Béky3ba8c332017-12-11 20:19:24660 if (idle_only && session->is_active())
[email protected]04644d42013-07-08 04:29:59661 continue;
662
Bence Béky3ba8c332017-12-11 20:19:24663 if (session->IsDraining())
664 continue;
665
666 session->CloseSessionOnError(error, description);
667
668 DCHECK(!IsSessionAvailable(session));
669 DCHECK(!session || session->IsDraining());
[email protected]8b114dd72011-03-25 05:33:02670 }
[email protected]aea80602009-09-18 00:55:08671}
672
Matt Menkee1d8fe92019-02-07 05:30:54673std::unique_ptr<SpdySession> SpdySessionPool::CreateSession(
674 const SpdySessionKey& key,
Matt Menkee1d8fe92019-02-07 05:30:54675 NetLog* net_log) {
676 UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", IMPORTED_FROM_SOCKET,
677 SPDY_SESSION_GET_MAX);
678
Matt Menke5062be22019-05-01 17:50:24679 // If there's a pre-existing matching session, it has to be an alias. Remove
680 // the alias.
681 auto it = LookupAvailableSessionByKey(key);
682 if (it != available_sessions_.end()) {
683 DCHECK(key != it->second->spdy_session_key());
684
685 // Remove session from available sessions and from aliases, and remove
686 // key from the session's pooled alias set, so that a new session can be
687 // created with this |key|.
688 it->second->RemovePooledAlias(key);
689 UnmapKey(key);
690 RemoveAliases(key);
691 }
692
Matt Menkee1d8fe92019-02-07 05:30:54693 return std::make_unique<SpdySession>(
694 key, http_server_properties_, transport_security_state_,
David Benjamin151ec6b2019-08-02 19:38:52695 ssl_client_context_ ? ssl_client_context_->ssl_config_service() : nullptr,
696 quic_supported_versions_, enable_sending_initial_data_,
Ryan Hamilton0246af92019-08-30 19:28:56697 enable_ping_based_connection_checking_, is_http2_enabled_,
Eric Orthfc0583b2021-05-18 19:13:59698 is_quic_enabled_, session_max_recv_window_size_,
Ryan Hamiltonf01aaef52019-09-30 23:00:51699 session_max_queued_capped_frames_, initial_settings_,
Bence Békyd228fb22021-10-29 14:45:51700 enable_http2_settings_grease_, greased_http2_frame_,
701 http2_end_stream_with_data_frame_, enable_priority_update_, time_func_,
Bence Békyc880f902023-06-21 21:11:53702 network_quality_estimator_, net_log);
Matt Menkee1d8fe92019-02-07 05:30:54703}
704
705base::WeakPtr<SpdySession> SpdySessionPool::InsertSession(
706 const SpdySessionKey& key,
707 std::unique_ptr<SpdySession> new_session,
Cammie Smith Barnes5191037b72021-03-01 22:06:53708 const NetLogWithSource& source_net_log,
Eric Orthac661912022-01-10 21:44:17709 std::set<std::string> dns_aliases) {
Matt Menkee1d8fe92019-02-07 05:30:54710 base::WeakPtr<SpdySession> available_session = new_session->GetWeakPtr();
711 sessions_.insert(new_session.release());
Cammie Smith Barnes5191037b72021-03-01 22:06:53712 MapKeyToAvailableSession(key, available_session, std::move(dns_aliases));
Matt Menkee1d8fe92019-02-07 05:30:54713
Sean Maher5b9af51f2022-11-21 15:32:47714 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Matt Menke66e502a72019-04-15 13:34:38715 FROM_HERE, base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
716 weak_ptr_factory_.GetWeakPtr(), key));
717
Eric Roman06bd9742019-07-13 15:19:13718 source_net_log.AddEventReferencingSource(
Matt Menkee1d8fe92019-02-07 05:30:54719 NetLogEventType::HTTP2_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET,
Eric Roman06bd9742019-07-13 15:19:13720 available_session->net_log().source());
Matt Menkee1d8fe92019-02-07 05:30:54721
722 // Look up the IP address for this session so that we can match
723 // future sessions (potentially to different domains) which can
724 // potentially be pooled with this one. Because GetPeerAddress()
725 // reports the proxy's address instead of the origin server, check
726 // to see if this is a direct connection.
727 if (key.proxy_server().is_direct()) {
728 IPEndPoint address;
729 if (available_session->GetPeerAddress(&address) == OK)
730 aliases_.insert(AliasMap::value_type(address, key));
731 }
732
733 return available_session;
734}
735
Matt Menke66e502a72019-04-15 13:34:38736void SpdySessionPool::UpdatePendingRequests(const SpdySessionKey& key) {
Matt Menke66e502a72019-04-15 13:34:38737 auto it = LookupAvailableSessionByKey(key);
738 if (it != available_sessions_.end()) {
739 base::WeakPtr<SpdySession> new_session = it->second->GetWeakPtr();
Matt Menke2ab5fe792019-04-18 18:14:13740 bool is_pooled = (key != new_session->spdy_session_key());
Matt Menke66e502a72019-04-15 13:34:38741 while (new_session && new_session->IsAvailable()) {
742 // Each iteration may empty out the RequestSet for |spdy_session_key| in
743 // |spdy_session_request_map_|. So each time, check for RequestSet and use
744 // the first one. Could just keep track if the last iteration removed the
745 // final request, but it's possible that responding to one request will
746 // result in cancelling another one.
747 //
748 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
749 // queue (Order by priority first, then FIFO within same priority).
750 // Unclear that it matters here.
751 auto iter = spdy_session_request_map_.find(key);
752 if (iter == spdy_session_request_map_.end())
753 break;
Matt Menke9338c3792019-04-19 19:16:49754 RequestSet* request_set = &iter->second.request_set;
Matt Menke77173952019-04-18 17:42:14755 // Find a request that can use the socket, if any.
756 RequestSet::iterator request;
757 for (request = request_set->begin(); request != request_set->end();
758 ++request) {
759 // If the request is for use with websockets, and the session doesn't
760 // support websockets, skip over the request.
761 if ((*request)->is_websocket() && !new_session->support_websocket())
762 continue;
Matt Menke2ab5fe792019-04-18 18:14:13763 // Don't use IP pooled session if not allowed.
764 if (!(*request)->enable_ip_based_pooling() && is_pooled)
765 continue;
Matt Menke77173952019-04-18 17:42:14766 break;
767 }
768 if (request == request_set->end())
769 break;
770
Matt Menke66e502a72019-04-15 13:34:38771 SpdySessionRequest::Delegate* delegate = (*request)->delegate();
Matt Menke66e502a72019-04-15 13:34:38772 RemoveRequestInternal(iter, request);
Matt Menke66e502a72019-04-15 13:34:38773 delegate->OnSpdySessionAvailable(new_session);
774 }
775 }
776
Matt Menke9338c3792019-04-19 19:16:49777 auto iter = spdy_session_request_map_.find(key);
778 if (iter == spdy_session_request_map_.end())
Matt Menkecd55e922019-04-15 16:25:08779 return;
780 // Remove all pending requests, if there are any. As a result, if one of these
781 // callbacks triggers a new RequestSession() call,
Matt Menke9338c3792019-04-19 19:16:49782 // |is_blocking_request_for_session| will be true.
783 std::list<base::RepeatingClosure> deferred_requests =
784 std::move(iter->second.deferred_callbacks);
Matt Menkecd55e922019-04-15 16:25:08785
Matt Menke9338c3792019-04-19 19:16:49786 // Delete the RequestMap if there are no SpdySessionRequests, and no deferred
787 // requests.
788 if (iter->second.request_set.empty())
789 spdy_session_request_map_.erase(iter);
790
791 // Resume any deferred requests. This needs to be after the
Matt Menke66e502a72019-04-15 13:34:38792 // OnSpdySessionAvailable() calls, to prevent requests from calling into the
793 // socket pools in cases where that's not necessary.
Matt Menke9338c3792019-04-19 19:16:49794 for (auto callback : deferred_requests) {
Matt Menke66e502a72019-04-15 13:34:38795 callback.Run();
Matt Menkecd55e922019-04-15 16:25:08796 }
Matt Menke66e502a72019-04-15 13:34:38797}
798
799void SpdySessionPool::RemoveRequestInternal(
800 SpdySessionRequestMap::iterator request_map_iterator,
801 RequestSet::iterator request_set_iterator) {
802 SpdySessionRequest* request = *request_set_iterator;
Matt Menke9338c3792019-04-19 19:16:49803 request_map_iterator->second.request_set.erase(request_set_iterator);
804 if (request->is_blocking_request_for_session()) {
805 DCHECK(request_map_iterator->second.has_blocking_request);
806 request_map_iterator->second.has_blocking_request = false;
807 }
808
809 // If both lists of requests are empty, can now remove the entry from the map.
810 if (request_map_iterator->second.request_set.empty() &&
811 request_map_iterator->second.deferred_callbacks.empty()) {
Matt Menke66e502a72019-04-15 13:34:38812 spdy_session_request_map_.erase(request_map_iterator);
Matt Menke9338c3792019-04-19 19:16:49813 }
Matt Menke66e502a72019-04-15 13:34:38814 request->OnRemovedFromPool();
815}
816
[email protected]aea80602009-09-18 00:55:08817} // namespace net