| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/client_socket_pool_manager.h" | 5 #include "net/socket/client_socket_pool_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 11 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 12 #include "net/http/http_network_session.h" | |
| 13 #include "net/http/http_proxy_client_socket_pool.h" | 13 #include "net/http/http_proxy_client_socket_pool.h" |
| 14 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
| 15 #include "net/http/http_stream_factory.h" | 15 #include "net/http/http_stream_factory.h" |
| 16 #include "net/proxy/proxy_info.h" | 16 #include "net/proxy/proxy_info.h" |
| 17 #include "net/socket/client_socket_handle.h" | 17 #include "net/socket/client_socket_handle.h" |
| 18 #include "net/socket/socks_client_socket_pool.h" | 18 #include "net/socket/socks_client_socket_pool.h" |
| 19 #include "net/socket/ssl_client_socket_pool.h" | 19 #include "net/socket/ssl_client_socket_pool.h" |
| 20 #include "net/socket/transport_client_socket_pool.h" | 20 #include "net/socket/transport_client_socket_pool.h" |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Limit of sockets of each socket pool. | 26 // Limit of sockets of each socket pool. |
| 27 int g_max_sockets_per_pool = 256; | 27 int g_max_sockets_per_pool[] = { |
| 28 256, // NORMAL_SOCKET_POOL |
| 29 256 // WEBSOCKET_SOCKET_POOL |
| 30 }; |
| 31 |
| 32 COMPILE_ASSERT(arraysize(g_max_sockets_per_pool) == |
| 33 HttpNetworkSession::NUM_SOCKET_POOL_TYPES, |
| 34 max_sockets_per_pool_length_mismatch); |
| 28 | 35 |
| 29 // Default to allow up to 6 connections per host. Experiment and tuning may | 36 // Default to allow up to 6 connections per host. Experiment and tuning may |
| 30 // try other values (greater than 0). Too large may cause many problems, such | 37 // try other values (greater than 0). Too large may cause many problems, such |
| 31 // as home routers blocking the connections!?!? See http://crbug.com/12066. | 38 // as home routers blocking the connections!?!? See http://crbug.com/12066. |
| 32 int g_max_sockets_per_group = 6; | 39 // |
| 40 // WebSocket connections are long-lived, and should be treated differently |
| 41 // than normal other connections. 6 connections per group sounded too small |
| 42 // for such use, thus we use a larger limit which was determined somewhat |
| 43 // arbitrarily. |
| 44 // TODO(yutak): Look at the usage and determine the right value after |
| 45 // WebSocket protocol stack starts to work. |
| 46 int g_max_sockets_per_group[] = { |
| 47 6, // NORMAL_SOCKET_POOL |
| 48 30 // WEBSOCKET_SOCKET_POOL |
| 49 }; |
| 50 |
| 51 COMPILE_ASSERT(arraysize(g_max_sockets_per_group) == |
| 52 HttpNetworkSession::NUM_SOCKET_POOL_TYPES, |
| 53 max_sockets_per_group_length_mismatch); |
| 33 | 54 |
| 34 // The max number of sockets to allow per proxy server. This applies both to | 55 // The max number of sockets to allow per proxy server. This applies both to |
| 35 // http and SOCKS proxies. See http://crbug.com/12066 and | 56 // http and SOCKS proxies. See http://crbug.com/12066 and |
| 36 // http://crbug.com/44501 for details about proxy server connection limits. | 57 // http://crbug.com/44501 for details about proxy server connection limits. |
| 37 int g_max_sockets_per_proxy_server = kDefaultMaxSocketsPerProxyServer; | 58 int g_max_sockets_per_proxy_server[] = { |
| 59 kDefaultMaxSocketsPerProxyServer, // NORMAL_SOCKET_POOL |
| 60 kDefaultMaxSocketsPerProxyServer // WEBSOCKET_SOCKET_POOL |
| 61 }; |
| 62 |
| 63 COMPILE_ASSERT(arraysize(g_max_sockets_per_proxy_server) == |
| 64 HttpNetworkSession::NUM_SOCKET_POOL_TYPES, |
| 65 max_sockets_per_proxy_server_length_mismatch); |
| 38 | 66 |
| 39 // The meat of the implementation for the InitSocketHandleForHttpRequest, | 67 // The meat of the implementation for the InitSocketHandleForHttpRequest, |
| 40 // InitSocketHandleForRawConnect and PreconnectSocketsForHttpRequest methods. | 68 // InitSocketHandleForRawConnect and PreconnectSocketsForHttpRequest methods. |
| 41 int InitSocketPoolHelper(const GURL& request_url, | 69 int InitSocketPoolHelper(const GURL& request_url, |
| 42 const HttpRequestHeaders& request_extra_headers, | 70 const HttpRequestHeaders& request_extra_headers, |
| 43 int request_load_flags, | 71 int request_load_flags, |
| 44 RequestPriority request_priority, | 72 RequestPriority request_priority, |
| 45 HttpNetworkSession* session, | 73 HttpNetworkSession* session, |
| 46 const ProxyInfo& proxy_info, | 74 const ProxyInfo& proxy_info, |
| 47 bool force_spdy_over_ssl, | 75 bool force_spdy_over_ssl, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 num_preconnect_streams, net_log); | 209 num_preconnect_streams, net_log); |
| 182 return OK; | 210 return OK; |
| 183 } | 211 } |
| 184 | 212 |
| 185 return socket_handle->Init(connection_group, ssl_params, | 213 return socket_handle->Init(connection_group, ssl_params, |
| 186 request_priority, callback, ssl_pool, | 214 request_priority, callback, ssl_pool, |
| 187 net_log); | 215 net_log); |
| 188 } | 216 } |
| 189 | 217 |
| 190 // Finally, get the connection started. | 218 // Finally, get the connection started. |
| 219 |
| 191 if (proxy_info.is_http() || proxy_info.is_https()) { | 220 if (proxy_info.is_http() || proxy_info.is_https()) { |
| 192 HttpProxyClientSocketPool* pool = | 221 HttpProxyClientSocketPool* pool = |
| 193 session->GetSocketPoolForHTTPProxy( | 222 session->GetSocketPoolForHTTPProxy( |
| 194 HttpNetworkSession::NORMAL_SOCKET_POOL, | 223 HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 195 *proxy_host_port); | 224 *proxy_host_port); |
| 196 if (num_preconnect_streams) { | 225 if (num_preconnect_streams) { |
| 197 RequestSocketsForPool(pool, connection_group, http_proxy_params, | 226 RequestSocketsForPool(pool, connection_group, http_proxy_params, |
| 198 num_preconnect_streams, net_log); | 227 num_preconnect_streams, net_log); |
| 199 return OK; | 228 return OK; |
| 200 } | 229 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 request_priority, callback, | 263 request_priority, callback, |
| 235 pool, net_log); | 264 pool, net_log); |
| 236 } | 265 } |
| 237 | 266 |
| 238 } // namespace | 267 } // namespace |
| 239 | 268 |
| 240 ClientSocketPoolManager::ClientSocketPoolManager() {} | 269 ClientSocketPoolManager::ClientSocketPoolManager() {} |
| 241 ClientSocketPoolManager::~ClientSocketPoolManager() {} | 270 ClientSocketPoolManager::~ClientSocketPoolManager() {} |
| 242 | 271 |
| 243 // static | 272 // static |
| 244 int ClientSocketPoolManager::max_sockets_per_pool() { | 273 int ClientSocketPoolManager::max_sockets_per_pool( |
| 245 return g_max_sockets_per_pool; | 274 HttpNetworkSession::SocketPoolType pool_type) { |
| 275 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 276 return g_max_sockets_per_pool[pool_type]; |
| 246 } | 277 } |
| 247 | 278 |
| 248 // static | 279 // static |
| 249 void ClientSocketPoolManager::set_max_sockets_per_pool(int socket_count) { | 280 void ClientSocketPoolManager::set_max_sockets_per_pool( |
| 281 HttpNetworkSession::SocketPoolType pool_type, |
| 282 int socket_count) { |
| 250 DCHECK_LT(0, socket_count); | 283 DCHECK_LT(0, socket_count); |
| 251 DCHECK_GT(1000, socket_count); // Sanity check. | 284 DCHECK_GT(1000, socket_count); // Sanity check. |
| 252 g_max_sockets_per_pool = socket_count; | 285 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 253 DCHECK_GE(g_max_sockets_per_pool, g_max_sockets_per_group); | 286 g_max_sockets_per_pool[pool_type] = socket_count; |
| 287 DCHECK_GE(g_max_sockets_per_pool[pool_type], |
| 288 g_max_sockets_per_group[pool_type]); |
| 254 } | 289 } |
| 255 | 290 |
| 256 // static | 291 // static |
| 257 int ClientSocketPoolManager::max_sockets_per_group() { | 292 int ClientSocketPoolManager::max_sockets_per_group( |
| 258 return g_max_sockets_per_group; | 293 HttpNetworkSession::SocketPoolType pool_type) { |
| 294 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 295 return g_max_sockets_per_group[pool_type]; |
| 259 } | 296 } |
| 260 | 297 |
| 261 // static | 298 // static |
| 262 void ClientSocketPoolManager::set_max_sockets_per_group(int socket_count) { | 299 void ClientSocketPoolManager::set_max_sockets_per_group( |
| 300 HttpNetworkSession::SocketPoolType pool_type, |
| 301 int socket_count) { |
| 263 DCHECK_LT(0, socket_count); | 302 DCHECK_LT(0, socket_count); |
| 264 // The following is a sanity check... but we should NEVER be near this value. | 303 // The following is a sanity check... but we should NEVER be near this value. |
| 265 DCHECK_GT(100, socket_count); | 304 DCHECK_GT(100, socket_count); |
| 266 g_max_sockets_per_group = socket_count; | 305 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 306 g_max_sockets_per_group[pool_type] = socket_count; |
| 267 | 307 |
| 268 DCHECK_GE(g_max_sockets_per_pool, g_max_sockets_per_group); | 308 DCHECK_GE(g_max_sockets_per_pool[pool_type], |
| 269 DCHECK_GE(g_max_sockets_per_proxy_server, g_max_sockets_per_group); | 309 g_max_sockets_per_group[pool_type]); |
| 310 DCHECK_GE(g_max_sockets_per_proxy_server[pool_type], |
| 311 g_max_sockets_per_group[pool_type]); |
| 270 } | 312 } |
| 271 | 313 |
| 272 // static | 314 // static |
| 273 int ClientSocketPoolManager::max_sockets_per_proxy_server() { | 315 int ClientSocketPoolManager::max_sockets_per_proxy_server( |
| 274 return g_max_sockets_per_proxy_server; | 316 HttpNetworkSession::SocketPoolType pool_type) { |
| 317 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 318 return g_max_sockets_per_proxy_server[pool_type]; |
| 275 } | 319 } |
| 276 | 320 |
| 277 // static | 321 // static |
| 278 void ClientSocketPoolManager::set_max_sockets_per_proxy_server( | 322 void ClientSocketPoolManager::set_max_sockets_per_proxy_server( |
| 323 HttpNetworkSession::SocketPoolType pool_type, |
| 279 int socket_count) { | 324 int socket_count) { |
| 280 DCHECK_LT(0, socket_count); | 325 DCHECK_LT(0, socket_count); |
| 281 DCHECK_GT(100, socket_count); // Sanity check. | 326 DCHECK_GT(100, socket_count); // Sanity check. |
| 327 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES); |
| 282 // Assert this case early on. The max number of sockets per group cannot | 328 // Assert this case early on. The max number of sockets per group cannot |
| 283 // exceed the max number of sockets per proxy server. | 329 // exceed the max number of sockets per proxy server. |
| 284 DCHECK_LE(g_max_sockets_per_group, socket_count); | 330 DCHECK_LE(g_max_sockets_per_group[pool_type], socket_count); |
| 285 g_max_sockets_per_proxy_server = socket_count; | 331 g_max_sockets_per_proxy_server[pool_type] = socket_count; |
| 286 } | 332 } |
| 287 | 333 |
| 288 int InitSocketHandleForHttpRequest( | 334 int InitSocketHandleForHttpRequest( |
| 289 const GURL& request_url, | 335 const GURL& request_url, |
| 290 const HttpRequestHeaders& request_extra_headers, | 336 const HttpRequestHeaders& request_extra_headers, |
| 291 int request_load_flags, | 337 int request_load_flags, |
| 292 RequestPriority request_priority, | 338 RequestPriority request_priority, |
| 293 HttpNetworkSession* session, | 339 HttpNetworkSession* session, |
| 294 const ProxyInfo& proxy_info, | 340 const ProxyInfo& proxy_info, |
| 295 bool force_spdy_over_ssl, | 341 bool force_spdy_over_ssl, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 const BoundNetLog& net_log, | 389 const BoundNetLog& net_log, |
| 344 int num_preconnect_streams) { | 390 int num_preconnect_streams) { |
| 345 return InitSocketPoolHelper( | 391 return InitSocketPoolHelper( |
| 346 request_url, request_extra_headers, request_load_flags, request_priority, | 392 request_url, request_extra_headers, request_load_flags, request_priority, |
| 347 session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn, | 393 session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn, |
| 348 ssl_config_for_origin, ssl_config_for_proxy, false, net_log, | 394 ssl_config_for_origin, ssl_config_for_proxy, false, net_log, |
| 349 num_preconnect_streams, NULL, CompletionCallback()); | 395 num_preconnect_streams, NULL, CompletionCallback()); |
| 350 } | 396 } |
| 351 | 397 |
| 352 } // namespace net | 398 } // namespace net |
| OLD | NEW |