blob: 190c3d1949a1062686beec1595ec5ff3a260ba17 [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
Eric Orthc1fc6912019-02-15 19:33:0810#include "base/bind.h"
Hans Wennborg0924470b2020-04-27 21:08:0511#include "base/check_op.h"
Lei Zhangde197672021-04-29 08:11:2412#include "base/containers/contains.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"
ssid6d6b40102016-04-05 18:59:5616#include "base/trace_event/trace_event.h"
[email protected]1ce7b66b2010-10-12 20:32:4417#include "base/values.h"
Bence Béky3ba8c332017-12-11 20:19:2418#include "build/build_config.h"
Tsuyoshi Horo7c9c0362022-08-19 00:47:2919#include "net/base/ip_endpoint.h"
xunjieli0b7f5b62016-12-06 20:43:4820#include "net/base/trace_constants.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
299 // TODO(crbug.com/1264933): Consider dealing with the other endpoints
300 // with protocol metadata.
301 const auto ip_endpoints =
302 HostResolver::GetNonProtocolEndpoints(endpoint_results);
303 for (const auto& address : ip_endpoints) {
Matt Menke5062be22019-05-01 17:50:24304 auto range = aliases_.equal_range(address);
305 for (auto alias_it = range.first; alias_it != range.second; ++alias_it) {
306 // We found a potential alias.
307 const SpdySessionKey& alias_key = alias_it->second;
308
309 auto available_session_it = LookupAvailableSessionByKey(alias_key);
310 // It shouldn't be in the aliases table if it doesn't exist!
311 DCHECK(available_session_it != available_sessions_.end());
312
Matt Menkedf93ff72020-07-16 02:23:03313 SpdySessionKey::CompareForAliasingResult compare_result =
314 alias_key.CompareForAliasing(key);
315 // Keys must be aliasable.
316 if (!compare_result.is_potentially_aliasable)
Matt Menke5062be22019-05-01 17:50:24317 continue;
Matt Menke5062be22019-05-01 17:50:24318
319 if (is_websocket && !available_session_it->second->support_websocket())
320 continue;
321
322 // Make copy of WeakPtr as call to UnmapKey() will delete original.
323 const base::WeakPtr<SpdySession> available_session =
324 available_session_it->second;
325
326 // Need to verify that the server is authenticated to serve traffic for
327 // |host_port_proxy_pair| too.
328 if (!available_session->VerifyDomainAuthentication(
329 key.host_port_pair().host())) {
330 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 0, 2);
331 continue;
332 }
333
334 UMA_HISTOGRAM_ENUMERATION("Net.SpdyIPPoolDomainMatch", 1, 2);
335
336 bool adding_pooled_alias = true;
337
338 // If socket tags differ, see if session's socket tag can be changed.
Matt Menkedf93ff72020-07-16 02:23:03339 if (!compare_result.is_socket_tag_match) {
Matt Menke5062be22019-05-01 17:50:24340 SpdySessionKey old_key = available_session->spdy_session_key();
341 SpdySessionKey new_key(old_key.host_port_pair(), old_key.proxy_server(),
342 old_key.privacy_mode(),
Matt Menkeae58eeb2019-05-24 21:09:50343 old_key.is_proxy_session(), key.socket_tag(),
Brianna Goldstein19896852022-09-26 22:15:26344 old_key.network_anonymization_key(),
Ben Schwartz3ff4dc1e62021-04-27 21:15:23345 old_key.secure_dns_policy());
Matt Menke5062be22019-05-01 17:50:24346
347 // If there is already a session with |new_key|, skip this one.
348 // It will be found in |aliases_| in a future iteration.
349 if (available_sessions_.find(new_key) != available_sessions_.end())
350 continue;
351
352 if (!available_session->ChangeSocketTag(key.socket_tag()))
353 continue;
354
355 DCHECK(available_session->spdy_session_key() == new_key);
356
357 // If this isn't a pooled alias, but the actual session that needs to
358 // have its socket tag change, there's no need to add an alias.
359 if (new_key == key)
360 adding_pooled_alias = false;
361
362 // Remap main session key.
Eric Orthac661912022-01-10 21:44:17363 std::set<std::string> main_session_old_dns_aliases =
Cammie Smith Barnes5191037b72021-03-01 22:06:53364 GetDnsAliasesForSessionKey(old_key);
Matt Menke5062be22019-05-01 17:50:24365 UnmapKey(old_key);
Cammie Smith Barnes5191037b72021-03-01 22:06:53366 MapKeyToAvailableSession(new_key, available_session,
367 std::move(main_session_old_dns_aliases));
Matt Menke5062be22019-05-01 17:50:24368
369 // Remap alias. From this point on |alias_it| is invalid, so no more
370 // iterations of the loop should be allowed.
371 aliases_.insert(AliasMap::value_type(alias_it->first, new_key));
372 aliases_.erase(alias_it);
373
374 // Remap pooled session keys.
Peter Kastingd647fc3f2022-08-30 06:29:52375 const auto& pooled_aliases = available_session->pooled_aliases();
376 for (auto it = pooled_aliases.begin(); it != pooled_aliases.end();) {
Matt Menke5062be22019-05-01 17:50:24377 // Ignore aliases this loop is inserting.
378 if (it->socket_tag() == key.socket_tag()) {
379 ++it;
380 continue;
381 }
Matt Menkeae58eeb2019-05-24 21:09:50382
Eric Orthac661912022-01-10 21:44:17383 std::set<std::string> pooled_alias_old_dns_aliases =
Cammie Smith Barnes5191037b72021-03-01 22:06:53384 GetDnsAliasesForSessionKey(*it);
Matt Menke5062be22019-05-01 17:50:24385 UnmapKey(*it);
dalyka92863972019-10-14 20:25:58386 SpdySessionKey new_pool_alias_key = SpdySessionKey(
387 it->host_port_pair(), it->proxy_server(), it->privacy_mode(),
388 it->is_proxy_session(), key.socket_tag(),
Brianna Goldstein19896852022-09-26 22:15:26389 it->network_anonymization_key(), it->secure_dns_policy());
Cammie Smith Barnes5191037b72021-03-01 22:06:53390 MapKeyToAvailableSession(new_pool_alias_key, available_session,
391 std::move(pooled_alias_old_dns_aliases));
Matt Menke5062be22019-05-01 17:50:24392 auto old_it = it;
393 ++it;
394 available_session->RemovePooledAlias(*old_it);
395 available_session->AddPooledAlias(new_pool_alias_key);
396
397 // If this is desired key, no need to add an alias for the desired key
398 // at the end of this method.
399 if (new_pool_alias_key == key)
400 adding_pooled_alias = false;
401 }
402 }
403
404 if (adding_pooled_alias) {
405 // Add this session to the map so that we can find it next time.
Tsuyoshi Horo7c9c0362022-08-19 00:47:29406 MapKeyToAvailableSession(key, available_session, aliases);
Matt Menke5062be22019-05-01 17:50:24407 available_session->AddPooledAlias(key);
408 }
409
410 // Post task to inform pending requests for session for |key| that a
411 // matching session is now available.
Sean Maher5b9af51f2022-11-21 15:32:47412 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Matt Menke5062be22019-05-01 17:50:24413 FROM_HERE, base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
414 weak_ptr_factory_.GetWeakPtr(), key));
415
416 // Inform the caller that the Callback may be deleted if the consumer is
417 // switched over to the newly aliased session. It's not guaranteed to be
418 // deleted, as the session may be closed, or taken by yet another pending
419 // request with a different SocketTag before the the request can try and
420 // use the session.
421 return OnHostResolutionCallbackResult::kMayBeDeletedAsync;
422 }
423 }
424 return OnHostResolutionCallbackResult::kContinue;
425}
426
[email protected]04644d42013-07-08 04:29:59427void SpdySessionPool::MakeSessionUnavailable(
[email protected]795cbf82013-07-22 09:37:27428 const base::WeakPtr<SpdySession>& available_session) {
[email protected]04644d42013-07-08 04:29:59429 UnmapKey(available_session->spdy_session_key());
430 RemoveAliases(available_session->spdy_session_key());
431 const std::set<SpdySessionKey>& aliases = available_session->pooled_aliases();
Tsuyoshi Horo17ef47d02022-06-30 10:58:27432 for (const auto& alias : aliases) {
433 UnmapKey(alias);
434 RemoveAliases(alias);
[email protected]46da33be2011-07-19 21:58:04435 }
[email protected]04644d42013-07-08 04:29:59436 DCHECK(!IsSessionAvailable(available_session));
437}
[email protected]46da33be2011-07-19 21:58:04438
[email protected]04644d42013-07-08 04:29:59439void SpdySessionPool::RemoveUnavailableSession(
[email protected]795cbf82013-07-22 09:37:27440 const base::WeakPtr<SpdySession>& unavailable_session) {
[email protected]04644d42013-07-08 04:29:59441 DCHECK(!IsSessionAvailable(unavailable_session));
442
443 unavailable_session->net_log().AddEvent(
bncd4365542017-04-14 18:33:17444 NetLogEventType::HTTP2_SESSION_POOL_REMOVE_SESSION);
[email protected]04644d42013-07-08 04:29:59445
jdoerrie22a91d8b92018-10-05 08:43:26446 auto it = sessions_.find(unavailable_session.get());
[email protected]04644d42013-07-08 04:29:59447 CHECK(it != sessions_.end());
danakjaee3e1ec2016-04-16 00:23:18448 std::unique_ptr<SpdySession> owned_session(*it);
[email protected]04644d42013-07-08 04:29:59449 sessions_.erase(it);
[email protected]9f7c4fd2009-11-24 18:50:15450}
451
[email protected]41d64e82013-07-03 22:44:26452// Make a copy of |sessions_| in the Close* functions below to avoid
[email protected]04644d42013-07-08 04:29:59453// reentrancy problems. Since arbitrary functions get called by close
454// handlers, it doesn't suffice to simply increment the iterator
455// before closing.
[email protected]8b114dd72011-03-25 05:33:02456
ttuttle859dc7a2015-04-23 19:42:29457void SpdySessionPool::CloseCurrentSessions(Error error) {
[email protected]04644d42013-07-08 04:29:59458 CloseCurrentSessionsHelper(error, "Closing current sessions.",
459 false /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26460}
461
Nick Harper1f5bf272020-10-29 11:22:35462void SpdySessionPool::CloseCurrentIdleSessions(const std::string& description) {
463 CloseCurrentSessionsHelper(ERR_ABORTED, description, true /* idle_only */);
[email protected]41d64e82013-07-03 22:44:26464}
465
466void SpdySessionPool::CloseAllSessions() {
Bence Béky3ba8c332017-12-11 20:19:24467 auto is_draining = [](const SpdySession* s) { return s->IsDraining(); };
468 // Repeat until every SpdySession owned by |this| is draining.
Peter Kastinge1961a9f2022-08-30 17:26:46469 while (!base::ranges::all_of(sessions_, is_draining)) {
[email protected]04644d42013-07-08 04:29:59470 CloseCurrentSessionsHelper(ERR_ABORTED, "Closing all sessions.",
471 false /* idle_only */);
[email protected]6cbfa852012-03-14 06:35:54472 }
473}
474
danakjaee3e1ec2016-04-16 00:23:18475std::unique_ptr<base::Value> SpdySessionPool::SpdySessionPoolInfoToValue()
476 const {
Matt Menkec9a0b382022-05-27 22:31:47477 base::Value::List list;
[email protected]1ce7b66b2010-10-12 20:32:44478
Tsuyoshi Horo17ef47d02022-06-30 10:58:27479 for (const auto& available_session : available_sessions_) {
[email protected]41d64e82013-07-03 22:44:26480 // Only add the session if the key in the map matches the main
481 // host_port_proxy_pair (not an alias).
Tsuyoshi Horo17ef47d02022-06-30 10:58:27482 const SpdySessionKey& key = available_session.first;
483 const SpdySessionKey& session_key =
484 available_session.second->spdy_session_key();
Bence Békya12c7702017-12-06 21:07:28485 if (key == session_key)
Tsuyoshi Horo17ef47d02022-06-30 10:58:27486 list.Append(available_session.second->GetInfoAsValue());
[email protected]1ce7b66b2010-10-12 20:32:44487 }
Matt Menkec9a0b382022-05-27 22:31:47488 return std::make_unique<base::Value>(std::move(list));
[email protected]1ce7b66b2010-10-12 20:32:44489}
490
[email protected]66761b952010-06-25 21:30:38491void SpdySessionPool::OnIPAddressChanged() {
Stefano Duoe0b077d2022-02-03 11:16:59492 DCHECK(cleanup_sessions_on_ip_address_changed_);
[email protected]d9a2e412014-02-06 11:02:58493 WeakSessionList current_sessions = GetCurrentSessions();
494 for (WeakSessionList::const_iterator it = current_sessions.begin();
495 it != current_sessions.end(); ++it) {
496 if (!*it)
497 continue;
498
Stefano Duo76e1f8402021-10-25 10:27:53499 if (go_away_on_ip_change_) {
500 (*it)->MakeUnavailable();
501 (*it)->StartGoingAway(kLastStreamId, ERR_NETWORK_CHANGED);
502 (*it)->MaybeFinishGoingAway();
503 } else {
504 (*it)->CloseSessionOnError(ERR_NETWORK_CHANGED,
505 "Closing current sessions.");
506 DCHECK((*it)->IsDraining());
507 }
[email protected]d9a2e412014-02-06 11:02:58508 DCHECK(!IsSessionAvailable(*it));
509 }
[email protected]66761b952010-06-25 21:30:38510}
511
David Benjamin247f1ee2019-08-02 21:52:13512void SpdySessionPool::OnSSLConfigChanged(bool is_cert_database_change) {
513 CloseCurrentSessions(is_cert_database_change ? ERR_CERT_DATABASE_CHANGED
514 : ERR_NETWORK_CHANGED);
[email protected]62635c72011-03-10 04:16:25515}
516
David Benjaminbac8dff2019-08-07 01:30:41517void SpdySessionPool::OnSSLConfigForServerChanged(const HostPortPair& server) {
518 WeakSessionList current_sessions = GetCurrentSessions();
519 for (base::WeakPtr<SpdySession>& session : current_sessions) {
520 if (!session)
521 continue;
522
523 const ProxyServer& proxy_server =
524 session->spdy_session_key().proxy_server();
525 if (session->host_port_pair() == server ||
526 (proxy_server.is_http_like() && !proxy_server.is_http() &&
527 proxy_server.host_port_pair() == server)) {
528 session->MakeUnavailable();
David Benjamina68a7952021-05-26 23:26:15529 // Note this call preserves active streams but fails any streams that are
530 // waiting on a stream ID.
531 // TODO(https://crbug.com/1213609): This is not ideal, but SpdySession
532 // does not have a state that supports this.
533 session->StartGoingAway(kLastStreamId, ERR_NETWORK_CHANGED);
David Benjaminbac8dff2019-08-07 01:30:41534 session->MaybeFinishGoingAway();
535 DCHECK(!IsSessionAvailable(session));
536 }
537 }
538}
539
Eric Orthac661912022-01-10 21:44:17540std::set<std::string> SpdySessionPool::GetDnsAliasesForSessionKey(
Cammie Smith Barnes5191037b72021-03-01 22:06:53541 const SpdySessionKey& key) const {
542 auto it = dns_aliases_by_session_key_.find(key);
543 if (it == dns_aliases_by_session_key_.end())
544 return {};
545
546 return it->second;
547}
548
Matt Menke9bcb57b2019-04-12 19:09:34549void SpdySessionPool::RemoveRequestForSpdySession(SpdySessionRequest* request) {
550 DCHECK_EQ(this, request->spdy_session_pool());
551
Matt Menke9338c3792019-04-19 19:16:49552 auto iter = spdy_session_request_map_.find(request->key());
553 DCHECK(iter != spdy_session_request_map_.end());
554
555 // Resume all pending requests if it is the blocking request, which is either
556 // being canceled, or has completed.
557 if (request->is_blocking_request_for_session() &&
558 !iter->second.deferred_callbacks.empty()) {
Sean Maher5b9af51f2022-11-21 15:32:47559 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Matt Menke66e502a72019-04-15 13:34:38560 FROM_HERE,
561 base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
Matt Menke9338c3792019-04-19 19:16:49562 weak_ptr_factory_.GetWeakPtr(), request->key()));
Matt Menke66e502a72019-04-15 13:34:38563 }
xunjielia9972092017-06-15 15:57:05564
Jan Wilken Dörrie21f9de72019-06-07 10:41:53565 DCHECK(base::Contains(iter->second.request_set, request));
Matt Menke9338c3792019-04-19 19:16:49566 RemoveRequestInternal(iter, iter->second.request_set.find(request));
xunjieli87258b72017-06-08 14:19:52567}
568
Matt Menke9338c3792019-04-19 19:16:49569SpdySessionPool::RequestInfoForKey::RequestInfoForKey() = default;
570SpdySessionPool::RequestInfoForKey::~RequestInfoForKey() = default;
571
[email protected]04644d42013-07-08 04:29:59572bool SpdySessionPool::IsSessionAvailable(
[email protected]795cbf82013-07-22 09:37:27573 const base::WeakPtr<SpdySession>& session) const {
Tsuyoshi Horo17ef47d02022-06-30 10:58:27574 for (const auto& available_session : available_sessions_) {
575 if (available_session.second.get() == session.get())
[email protected]04644d42013-07-08 04:29:59576 return true;
577 }
578 return false;
579}
580
[email protected]04644d42013-07-08 04:29:59581void SpdySessionPool::MapKeyToAvailableSession(
582 const SpdySessionKey& key,
Cammie Smith Barnes5191037b72021-03-01 22:06:53583 const base::WeakPtr<SpdySession>& session,
Eric Orthac661912022-01-10 21:44:17584 std::set<std::string> dns_aliases) {
Jan Wilken Dörrie21f9de72019-06-07 10:41:53585 DCHECK(base::Contains(sessions_, session.get()));
[email protected]04644d42013-07-08 04:29:59586 std::pair<AvailableSessionMap::iterator, bool> result =
rchebf12982015-04-10 01:15:00587 available_sessions_.insert(std::make_pair(key, session));
[email protected]41d64e82013-07-03 22:44:26588 CHECK(result.second);
Cammie Smith Barnes5191037b72021-03-01 22:06:53589
590 dns_aliases_by_session_key_[key] = std::move(dns_aliases);
[email protected]aea80602009-09-18 00:55:08591}
592
[email protected]04644d42013-07-08 04:29:59593SpdySessionPool::AvailableSessionMap::iterator
594SpdySessionPool::LookupAvailableSessionByKey(
595 const SpdySessionKey& key) {
rchebf12982015-04-10 01:15:00596 return available_sessions_.find(key);
[email protected]d1eda932009-11-04 01:03:10597}
598
[email protected]04644d42013-07-08 04:29:59599void SpdySessionPool::UnmapKey(const SpdySessionKey& key) {
jdoerrie22a91d8b92018-10-05 08:43:26600 auto it = LookupAvailableSessionByKey(key);
[email protected]04644d42013-07-08 04:29:59601 CHECK(it != available_sessions_.end());
602 available_sessions_.erase(it);
Cammie Smith Barnes5191037b72021-03-01 22:06:53603 dns_aliases_by_session_key_.erase(key);
[email protected]8b114dd72011-03-25 05:33:02604}
605
[email protected]04644d42013-07-08 04:29:59606void SpdySessionPool::RemoveAliases(const SpdySessionKey& key) {
[email protected]8b114dd72011-03-25 05:33:02607 // Walk the aliases map, find references to this pair.
608 // TODO(mbelshe): Figure out if this is too expensive.
jdoerrie22a91d8b92018-10-05 08:43:26609 for (auto it = aliases_.begin(); it != aliases_.end();) {
Bence Békya12c7702017-12-06 21:07:28610 if (it->second == key) {
jdoerrie22a91d8b92018-10-05 08:43:26611 auto old_it = it;
[email protected]04644d42013-07-08 04:29:59612 ++it;
613 aliases_.erase(old_it);
614 } else {
615 ++it;
[email protected]8b114dd72011-03-25 05:33:02616 }
[email protected]04644d42013-07-08 04:29:59617 }
618}
619
[email protected]676438e2013-07-22 11:07:33620SpdySessionPool::WeakSessionList SpdySessionPool::GetCurrentSessions() const {
621 WeakSessionList current_sessions;
Tsuyoshi Horo17ef47d02022-06-30 10:58:27622 for (auto* session : sessions_) {
623 current_sessions.push_back(session->GetWeakPtr());
[email protected]676438e2013-07-22 11:07:33624 }
625 return current_sessions;
626}
627
diannahu401e364c2017-04-07 01:17:47628void SpdySessionPool::CloseCurrentSessionsHelper(Error error,
Bence Béky4e83f492018-05-13 23:14:25629 const std::string& description,
diannahu401e364c2017-04-07 01:17:47630 bool idle_only) {
[email protected]676438e2013-07-22 11:07:33631 WeakSessionList current_sessions = GetCurrentSessions();
Bence Béky3ba8c332017-12-11 20:19:24632 for (base::WeakPtr<SpdySession>& session : current_sessions) {
633 if (!session)
[email protected]04644d42013-07-08 04:29:59634 continue;
635
Bence Béky3ba8c332017-12-11 20:19:24636 if (idle_only && session->is_active())
[email protected]04644d42013-07-08 04:29:59637 continue;
638
Bence Béky3ba8c332017-12-11 20:19:24639 if (session->IsDraining())
640 continue;
641
642 session->CloseSessionOnError(error, description);
643
644 DCHECK(!IsSessionAvailable(session));
645 DCHECK(!session || session->IsDraining());
[email protected]8b114dd72011-03-25 05:33:02646 }
[email protected]aea80602009-09-18 00:55:08647}
648
Matt Menkee1d8fe92019-02-07 05:30:54649std::unique_ptr<SpdySession> SpdySessionPool::CreateSession(
650 const SpdySessionKey& key,
Matt Menkee1d8fe92019-02-07 05:30:54651 NetLog* net_log) {
652 UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionGet", IMPORTED_FROM_SOCKET,
653 SPDY_SESSION_GET_MAX);
654
Matt Menke5062be22019-05-01 17:50:24655 // If there's a pre-existing matching session, it has to be an alias. Remove
656 // the alias.
657 auto it = LookupAvailableSessionByKey(key);
658 if (it != available_sessions_.end()) {
659 DCHECK(key != it->second->spdy_session_key());
660
661 // Remove session from available sessions and from aliases, and remove
662 // key from the session's pooled alias set, so that a new session can be
663 // created with this |key|.
664 it->second->RemovePooledAlias(key);
665 UnmapKey(key);
666 RemoveAliases(key);
667 }
668
Matt Menkee1d8fe92019-02-07 05:30:54669 return std::make_unique<SpdySession>(
670 key, http_server_properties_, transport_security_state_,
David Benjamin151ec6b2019-08-02 19:38:52671 ssl_client_context_ ? ssl_client_context_->ssl_config_service() : nullptr,
672 quic_supported_versions_, enable_sending_initial_data_,
Ryan Hamilton0246af92019-08-30 19:28:56673 enable_ping_based_connection_checking_, is_http2_enabled_,
Eric Orthfc0583b2021-05-18 19:13:59674 is_quic_enabled_, session_max_recv_window_size_,
Ryan Hamiltonf01aaef52019-09-30 23:00:51675 session_max_queued_capped_frames_, initial_settings_,
Bence Békyd228fb22021-10-29 14:45:51676 enable_http2_settings_grease_, greased_http2_frame_,
677 http2_end_stream_with_data_frame_, enable_priority_update_, time_func_,
678 push_delegate_, network_quality_estimator_, net_log);
Matt Menkee1d8fe92019-02-07 05:30:54679}
680
681base::WeakPtr<SpdySession> SpdySessionPool::InsertSession(
682 const SpdySessionKey& key,
683 std::unique_ptr<SpdySession> new_session,
Cammie Smith Barnes5191037b72021-03-01 22:06:53684 const NetLogWithSource& source_net_log,
Eric Orthac661912022-01-10 21:44:17685 std::set<std::string> dns_aliases) {
Matt Menkee1d8fe92019-02-07 05:30:54686 base::WeakPtr<SpdySession> available_session = new_session->GetWeakPtr();
687 sessions_.insert(new_session.release());
Cammie Smith Barnes5191037b72021-03-01 22:06:53688 MapKeyToAvailableSession(key, available_session, std::move(dns_aliases));
Matt Menkee1d8fe92019-02-07 05:30:54689
Sean Maher5b9af51f2022-11-21 15:32:47690 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Matt Menke66e502a72019-04-15 13:34:38691 FROM_HERE, base::BindOnce(&SpdySessionPool::UpdatePendingRequests,
692 weak_ptr_factory_.GetWeakPtr(), key));
693
Eric Roman06bd9742019-07-13 15:19:13694 source_net_log.AddEventReferencingSource(
Matt Menkee1d8fe92019-02-07 05:30:54695 NetLogEventType::HTTP2_SESSION_POOL_IMPORTED_SESSION_FROM_SOCKET,
Eric Roman06bd9742019-07-13 15:19:13696 available_session->net_log().source());
Matt Menkee1d8fe92019-02-07 05:30:54697
698 // Look up the IP address for this session so that we can match
699 // future sessions (potentially to different domains) which can
700 // potentially be pooled with this one. Because GetPeerAddress()
701 // reports the proxy's address instead of the origin server, check
702 // to see if this is a direct connection.
703 if (key.proxy_server().is_direct()) {
704 IPEndPoint address;
705 if (available_session->GetPeerAddress(&address) == OK)
706 aliases_.insert(AliasMap::value_type(address, key));
707 }
708
709 return available_session;
710}
711
Matt Menke66e502a72019-04-15 13:34:38712void SpdySessionPool::UpdatePendingRequests(const SpdySessionKey& key) {
Matt Menke66e502a72019-04-15 13:34:38713 auto it = LookupAvailableSessionByKey(key);
714 if (it != available_sessions_.end()) {
715 base::WeakPtr<SpdySession> new_session = it->second->GetWeakPtr();
Matt Menke2ab5fe792019-04-18 18:14:13716 bool is_pooled = (key != new_session->spdy_session_key());
Matt Menke66e502a72019-04-15 13:34:38717 while (new_session && new_session->IsAvailable()) {
718 // Each iteration may empty out the RequestSet for |spdy_session_key| in
719 // |spdy_session_request_map_|. So each time, check for RequestSet and use
720 // the first one. Could just keep track if the last iteration removed the
721 // final request, but it's possible that responding to one request will
722 // result in cancelling another one.
723 //
724 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
725 // queue (Order by priority first, then FIFO within same priority).
726 // Unclear that it matters here.
727 auto iter = spdy_session_request_map_.find(key);
728 if (iter == spdy_session_request_map_.end())
729 break;
Matt Menke9338c3792019-04-19 19:16:49730 RequestSet* request_set = &iter->second.request_set;
Matt Menke77173952019-04-18 17:42:14731 // Find a request that can use the socket, if any.
732 RequestSet::iterator request;
733 for (request = request_set->begin(); request != request_set->end();
734 ++request) {
735 // If the request is for use with websockets, and the session doesn't
736 // support websockets, skip over the request.
737 if ((*request)->is_websocket() && !new_session->support_websocket())
738 continue;
Matt Menke2ab5fe792019-04-18 18:14:13739 // Don't use IP pooled session if not allowed.
740 if (!(*request)->enable_ip_based_pooling() && is_pooled)
741 continue;
Matt Menke77173952019-04-18 17:42:14742 break;
743 }
744 if (request == request_set->end())
745 break;
746
Matt Menke66e502a72019-04-15 13:34:38747 SpdySessionRequest::Delegate* delegate = (*request)->delegate();
Matt Menke66e502a72019-04-15 13:34:38748 RemoveRequestInternal(iter, request);
Matt Menke66e502a72019-04-15 13:34:38749 delegate->OnSpdySessionAvailable(new_session);
750 }
751 }
752
Matt Menke9338c3792019-04-19 19:16:49753 auto iter = spdy_session_request_map_.find(key);
754 if (iter == spdy_session_request_map_.end())
Matt Menkecd55e922019-04-15 16:25:08755 return;
756 // Remove all pending requests, if there are any. As a result, if one of these
757 // callbacks triggers a new RequestSession() call,
Matt Menke9338c3792019-04-19 19:16:49758 // |is_blocking_request_for_session| will be true.
759 std::list<base::RepeatingClosure> deferred_requests =
760 std::move(iter->second.deferred_callbacks);
Matt Menkecd55e922019-04-15 16:25:08761
Matt Menke9338c3792019-04-19 19:16:49762 // Delete the RequestMap if there are no SpdySessionRequests, and no deferred
763 // requests.
764 if (iter->second.request_set.empty())
765 spdy_session_request_map_.erase(iter);
766
767 // Resume any deferred requests. This needs to be after the
Matt Menke66e502a72019-04-15 13:34:38768 // OnSpdySessionAvailable() calls, to prevent requests from calling into the
769 // socket pools in cases where that's not necessary.
Matt Menke9338c3792019-04-19 19:16:49770 for (auto callback : deferred_requests) {
Matt Menke66e502a72019-04-15 13:34:38771 callback.Run();
Matt Menkecd55e922019-04-15 16:25:08772 }
Matt Menke66e502a72019-04-15 13:34:38773}
774
775void SpdySessionPool::RemoveRequestInternal(
776 SpdySessionRequestMap::iterator request_map_iterator,
777 RequestSet::iterator request_set_iterator) {
778 SpdySessionRequest* request = *request_set_iterator;
Matt Menke9338c3792019-04-19 19:16:49779 request_map_iterator->second.request_set.erase(request_set_iterator);
780 if (request->is_blocking_request_for_session()) {
781 DCHECK(request_map_iterator->second.has_blocking_request);
782 request_map_iterator->second.has_blocking_request = false;
783 }
784
785 // If both lists of requests are empty, can now remove the entry from the map.
786 if (request_map_iterator->second.request_set.empty() &&
787 request_map_iterator->second.deferred_callbacks.empty()) {
Matt Menke66e502a72019-04-15 13:34:38788 spdy_session_request_map_.erase(request_map_iterator);
Matt Menke9338c3792019-04-19 19:16:49789 }
Matt Menke66e502a72019-04-15 13:34:38790 request->OnRemovedFromPool();
791}
792
[email protected]aea80602009-09-18 00:55:08793} // namespace net