Material throbber: use in tabstrip
[chromium-blink-merge.git] / net / socket / transport_client_socket_pool.cc
blobf2d124788345989d0abcdd41a83bc0e6df43b4a9
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
3 // found in the LICENSE file.
5 #include "net/socket/transport_client_socket_pool.h"
7 #include <algorithm>
9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/strings/string_util.h"
16 #include "base/synchronization/lock.h"
17 #include "base/time/time.h"
18 #include "base/values.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h"
21 #include "net/log/net_log.h"
22 #include "net/socket/client_socket_factory.h"
23 #include "net/socket/client_socket_handle.h"
24 #include "net/socket/client_socket_pool_base.h"
25 #include "net/socket/socket_net_log_params.h"
26 #include "net/socket/tcp_client_socket.h"
28 using base::TimeDelta;
30 namespace net {
32 // TODO(willchan): Base this off RTT instead of statically setting it. Note we
33 // choose a timeout that is different from the backup connect job timer so they
34 // don't synchronize.
35 const int TransportConnectJobHelper::kIPv6FallbackTimerInMs = 300;
37 namespace {
39 // Returns true iff all addresses in |list| are in the IPv6 family.
40 bool AddressListOnlyContainsIPv6(const AddressList& list) {
41 DCHECK(!list.empty());
42 for (AddressList::const_iterator iter = list.begin(); iter != list.end();
43 ++iter) {
44 if (iter->GetFamily() != ADDRESS_FAMILY_IPV6)
45 return false;
47 return true;
50 } // namespace
52 // This lock protects |g_last_connect_time|.
53 static base::LazyInstance<base::Lock>::Leaky
54 g_last_connect_time_lock = LAZY_INSTANCE_INITIALIZER;
56 // |g_last_connect_time| has the last time a connect() call is made.
57 static base::LazyInstance<base::TimeTicks>::Leaky
58 g_last_connect_time = LAZY_INSTANCE_INITIALIZER;
60 TransportSocketParams::TransportSocketParams(
61 const HostPortPair& host_port_pair,
62 bool disable_resolver_cache,
63 bool ignore_limits,
64 const OnHostResolutionCallback& host_resolution_callback,
65 CombineConnectAndWritePolicy combine_connect_and_write_if_supported)
66 : destination_(host_port_pair),
67 ignore_limits_(ignore_limits),
68 host_resolution_callback_(host_resolution_callback),
69 combine_connect_and_write_(combine_connect_and_write_if_supported) {
70 if (disable_resolver_cache)
71 destination_.set_allow_cached_response(false);
72 // combine_connect_and_write currently translates to TCP FastOpen.
73 // Enable TCP FastOpen if user wants it.
74 if (combine_connect_and_write_ == COMBINE_CONNECT_AND_WRITE_DEFAULT) {
75 IsTCPFastOpenUserEnabled() ? combine_connect_and_write_ =
76 COMBINE_CONNECT_AND_WRITE_DESIRED :
77 COMBINE_CONNECT_AND_WRITE_PROHIBITED;
81 TransportSocketParams::~TransportSocketParams() {}
83 // TransportConnectJobs will time out after this many seconds. Note this is
84 // the total time, including both host resolution and TCP connect() times.
86 // TODO(eroman): The use of this constant needs to be re-evaluated. The time
87 // needed for TCPClientSocketXXX::Connect() can be arbitrarily long, since
88 // the address list may contain many alternatives, and most of those may
89 // timeout. Even worse, the per-connect timeout threshold varies greatly
90 // between systems (anywhere from 20 seconds to 190 seconds).
91 // See comment #12 at http://crbug.com/23364 for specifics.
92 static const int kTransportConnectJobTimeoutInSeconds = 240; // 4 minutes.
94 TransportConnectJobHelper::TransportConnectJobHelper(
95 const scoped_refptr<TransportSocketParams>& params,
96 ClientSocketFactory* client_socket_factory,
97 HostResolver* host_resolver,
98 LoadTimingInfo::ConnectTiming* connect_timing)
99 : params_(params),
100 client_socket_factory_(client_socket_factory),
101 resolver_(host_resolver),
102 next_state_(STATE_NONE),
103 connect_timing_(connect_timing) {}
105 TransportConnectJobHelper::~TransportConnectJobHelper() {}
107 int TransportConnectJobHelper::DoResolveHost(RequestPriority priority,
108 const BoundNetLog& net_log) {
109 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
110 connect_timing_->dns_start = base::TimeTicks::Now();
112 return resolver_.Resolve(
113 params_->destination(), priority, &addresses_, on_io_complete_, net_log);
116 int TransportConnectJobHelper::DoResolveHostComplete(
117 int result,
118 const BoundNetLog& net_log) {
119 connect_timing_->dns_end = base::TimeTicks::Now();
120 // Overwrite connection start time, since for connections that do not go
121 // through proxies, |connect_start| should not include dns lookup time.
122 connect_timing_->connect_start = connect_timing_->dns_end;
124 if (result == OK) {
125 // Invoke callback, and abort if it fails.
126 if (!params_->host_resolution_callback().is_null())
127 result = params_->host_resolution_callback().Run(addresses_, net_log);
129 if (result == OK)
130 next_state_ = STATE_TRANSPORT_CONNECT;
132 return result;
135 base::TimeDelta TransportConnectJobHelper::HistogramDuration(
136 ConnectionLatencyHistogram race_result) {
137 DCHECK(!connect_timing_->connect_start.is_null());
138 DCHECK(!connect_timing_->dns_start.is_null());
139 base::TimeTicks now = base::TimeTicks::Now();
140 base::TimeDelta total_duration = now - connect_timing_->dns_start;
141 UMA_HISTOGRAM_CUSTOM_TIMES("Net.DNS_Resolution_And_TCP_Connection_Latency2",
142 total_duration,
143 base::TimeDelta::FromMilliseconds(1),
144 base::TimeDelta::FromMinutes(10),
145 100);
147 base::TimeDelta connect_duration = now - connect_timing_->connect_start;
148 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency",
149 connect_duration,
150 base::TimeDelta::FromMilliseconds(1),
151 base::TimeDelta::FromMinutes(10),
152 100);
154 switch (race_result) {
155 case CONNECTION_LATENCY_IPV4_WINS_RACE:
156 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_Wins_Race",
157 connect_duration,
158 base::TimeDelta::FromMilliseconds(1),
159 base::TimeDelta::FromMinutes(10),
160 100);
161 break;
163 case CONNECTION_LATENCY_IPV4_NO_RACE:
164 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_No_Race",
165 connect_duration,
166 base::TimeDelta::FromMilliseconds(1),
167 base::TimeDelta::FromMinutes(10),
168 100);
169 break;
171 case CONNECTION_LATENCY_IPV6_RACEABLE:
172 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Raceable",
173 connect_duration,
174 base::TimeDelta::FromMilliseconds(1),
175 base::TimeDelta::FromMinutes(10),
176 100);
177 break;
179 case CONNECTION_LATENCY_IPV6_SOLO:
180 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Solo",
181 connect_duration,
182 base::TimeDelta::FromMilliseconds(1),
183 base::TimeDelta::FromMinutes(10),
184 100);
185 break;
187 default:
188 NOTREACHED();
189 break;
192 return connect_duration;
195 TransportConnectJob::TransportConnectJob(
196 const std::string& group_name,
197 RequestPriority priority,
198 const scoped_refptr<TransportSocketParams>& params,
199 base::TimeDelta timeout_duration,
200 ClientSocketFactory* client_socket_factory,
201 HostResolver* host_resolver,
202 Delegate* delegate,
203 NetLog* net_log)
204 : ConnectJob(group_name,
205 timeout_duration,
206 priority,
207 delegate,
208 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
209 helper_(params, client_socket_factory, host_resolver, &connect_timing_),
210 interval_between_connects_(CONNECT_INTERVAL_GT_20MS),
211 resolve_result_(OK),
212 connect_result_(OK) {
213 helper_.SetOnIOComplete(this);
216 TransportConnectJob::~TransportConnectJob() {
217 // We don't worry about cancelling the host resolution and TCP connect, since
218 // ~SingleRequestHostResolver and ~StreamSocket will take care of it.
221 LoadState TransportConnectJob::GetLoadState() const {
222 switch (helper_.next_state()) {
223 case TransportConnectJobHelper::STATE_RESOLVE_HOST:
224 case TransportConnectJobHelper::STATE_RESOLVE_HOST_COMPLETE:
225 return LOAD_STATE_RESOLVING_HOST;
226 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT:
227 case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE:
228 return LOAD_STATE_CONNECTING;
229 case TransportConnectJobHelper::STATE_NONE:
230 return LOAD_STATE_IDLE;
232 NOTREACHED();
233 return LOAD_STATE_IDLE;
236 void TransportConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) {
237 // If hostname resolution failed, record an empty endpoint and the result.
238 // If the actual socket Connect call failed, record the result and the last
239 // address attempted.
240 // TODO(ttuttle): Plumb into the socket layer and record *all* attempts.
241 ConnectionAttempts attempts;
242 if (resolve_result_ != OK) {
243 DCHECK_EQ(0u, helper_.addresses().size());
244 attempts.push_back(ConnectionAttempt(IPEndPoint(), resolve_result_));
245 } else if (connect_result_ != OK) {
246 DCHECK_LT(0u, helper_.addresses().size());
247 attempts.push_back(
248 ConnectionAttempt(helper_.addresses().back(), connect_result_));
250 handle->set_connection_attempts(attempts);
253 // static
254 void TransportConnectJob::MakeAddressListStartWithIPv4(AddressList* list) {
255 for (AddressList::iterator i = list->begin(); i != list->end(); ++i) {
256 if (i->GetFamily() == ADDRESS_FAMILY_IPV4) {
257 std::rotate(list->begin(), i, list->end());
258 break;
263 int TransportConnectJob::DoResolveHost() {
264 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed.
265 tracked_objects::ScopedTracker tracking_profile(
266 FROM_HERE_WITH_EXPLICIT_FUNCTION(
267 "436634 TransportConnectJob::DoResolveHost"));
269 return helper_.DoResolveHost(priority(), net_log());
272 int TransportConnectJob::DoResolveHostComplete(int result) {
273 resolve_result_ = result;
274 return helper_.DoResolveHostComplete(result, net_log());
277 int TransportConnectJob::DoTransportConnect() {
278 base::TimeTicks now = base::TimeTicks::Now();
279 base::TimeTicks last_connect_time;
281 base::AutoLock lock(g_last_connect_time_lock.Get());
282 last_connect_time = g_last_connect_time.Get();
283 *g_last_connect_time.Pointer() = now;
285 if (last_connect_time.is_null()) {
286 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
287 } else {
288 int64 interval = (now - last_connect_time).InMilliseconds();
289 if (interval <= 10)
290 interval_between_connects_ = CONNECT_INTERVAL_LE_10MS;
291 else if (interval <= 20)
292 interval_between_connects_ = CONNECT_INTERVAL_LE_20MS;
293 else
294 interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
297 helper_.set_next_state(
298 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE);
299 transport_socket_ =
300 helper_.client_socket_factory()->CreateTransportClientSocket(
301 helper_.addresses(), net_log().net_log(), net_log().source());
303 // If the list contains IPv6 and IPv4 addresses, the first address will
304 // be IPv6, and the IPv4 addresses will be tried as fallback addresses,
305 // per "Happy Eyeballs" (RFC 6555).
306 bool try_ipv6_connect_with_ipv4_fallback =
307 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6 &&
308 !AddressListOnlyContainsIPv6(helper_.addresses());
310 // Enable TCP FastOpen if indicated by transport socket params.
311 // Note: We currently do not turn on TCP FastOpen for destinations where
312 // we try a TCP connect over IPv6 with fallback to IPv4.
313 if (!try_ipv6_connect_with_ipv4_fallback &&
314 helper_.params()->combine_connect_and_write() ==
315 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED) {
316 transport_socket_->EnableTCPFastOpenIfSupported();
319 int rv = transport_socket_->Connect(helper_.on_io_complete());
320 if (rv == ERR_IO_PENDING && try_ipv6_connect_with_ipv4_fallback) {
321 fallback_timer_.Start(
322 FROM_HERE,
323 base::TimeDelta::FromMilliseconds(
324 TransportConnectJobHelper::kIPv6FallbackTimerInMs),
325 this,
326 &TransportConnectJob::DoIPv6FallbackTransportConnect);
328 return rv;
331 int TransportConnectJob::DoTransportConnectComplete(int result) {
332 if (result == OK) {
333 bool is_ipv4 =
334 helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV4;
335 TransportConnectJobHelper::ConnectionLatencyHistogram race_result =
336 TransportConnectJobHelper::CONNECTION_LATENCY_UNKNOWN;
337 if (is_ipv4) {
338 race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_NO_RACE;
339 } else {
340 if (AddressListOnlyContainsIPv6(helper_.addresses())) {
341 race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_SOLO;
342 } else {
343 race_result =
344 TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_RACEABLE;
347 base::TimeDelta connect_duration = helper_.HistogramDuration(race_result);
348 switch (interval_between_connects_) {
349 case CONNECT_INTERVAL_LE_10MS:
350 UMA_HISTOGRAM_CUSTOM_TIMES(
351 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_10ms",
352 connect_duration,
353 base::TimeDelta::FromMilliseconds(1),
354 base::TimeDelta::FromMinutes(10),
355 100);
356 break;
357 case CONNECT_INTERVAL_LE_20MS:
358 UMA_HISTOGRAM_CUSTOM_TIMES(
359 "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_20ms",
360 connect_duration,
361 base::TimeDelta::FromMilliseconds(1),
362 base::TimeDelta::FromMinutes(10),
363 100);
364 break;
365 case CONNECT_INTERVAL_GT_20MS:
366 UMA_HISTOGRAM_CUSTOM_TIMES(
367 "Net.TCP_Connection_Latency_Interval_GreaterThan_20ms",
368 connect_duration,
369 base::TimeDelta::FromMilliseconds(1),
370 base::TimeDelta::FromMinutes(10),
371 100);
372 break;
373 default:
374 NOTREACHED();
375 break;
378 SetSocket(transport_socket_.Pass());
379 fallback_timer_.Stop();
380 } else {
381 // Be a bit paranoid and kill off the fallback members to prevent reuse.
382 fallback_transport_socket_.reset();
383 fallback_addresses_.reset();
386 connect_result_ = result;
388 return result;
391 void TransportConnectJob::DoIPv6FallbackTransportConnect() {
392 // The timer should only fire while we're waiting for the main connect to
393 // succeed.
394 if (helper_.next_state() !=
395 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
396 NOTREACHED();
397 return;
400 DCHECK(!fallback_transport_socket_.get());
401 DCHECK(!fallback_addresses_.get());
403 fallback_addresses_.reset(new AddressList(helper_.addresses()));
404 MakeAddressListStartWithIPv4(fallback_addresses_.get());
405 fallback_transport_socket_ =
406 helper_.client_socket_factory()->CreateTransportClientSocket(
407 *fallback_addresses_, net_log().net_log(), net_log().source());
408 fallback_connect_start_time_ = base::TimeTicks::Now();
409 int rv = fallback_transport_socket_->Connect(
410 base::Bind(
411 &TransportConnectJob::DoIPv6FallbackTransportConnectComplete,
412 base::Unretained(this)));
413 if (rv != ERR_IO_PENDING)
414 DoIPv6FallbackTransportConnectComplete(rv);
417 void TransportConnectJob::DoIPv6FallbackTransportConnectComplete(int result) {
418 // This should only happen when we're waiting for the main connect to succeed.
419 if (helper_.next_state() !=
420 TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
421 NOTREACHED();
422 return;
425 DCHECK_NE(ERR_IO_PENDING, result);
426 DCHECK(fallback_transport_socket_.get());
427 DCHECK(fallback_addresses_.get());
429 if (result == OK) {
430 DCHECK(!fallback_connect_start_time_.is_null());
431 connect_timing_.connect_start = fallback_connect_start_time_;
432 helper_.HistogramDuration(
433 TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_WINS_RACE);
434 SetSocket(fallback_transport_socket_.Pass());
435 helper_.set_next_state(TransportConnectJobHelper::STATE_NONE);
436 transport_socket_.reset();
437 } else {
438 // Be a bit paranoid and kill off the fallback members to prevent reuse.
439 fallback_transport_socket_.reset();
440 fallback_addresses_.reset();
442 NotifyDelegateOfCompletion(result); // Deletes |this|
445 int TransportConnectJob::ConnectInternal() {
446 return helper_.DoConnectInternal(this);
449 scoped_ptr<ConnectJob>
450 TransportClientSocketPool::TransportConnectJobFactory::NewConnectJob(
451 const std::string& group_name,
452 const PoolBase::Request& request,
453 ConnectJob::Delegate* delegate) const {
454 return scoped_ptr<ConnectJob>(
455 new TransportConnectJob(group_name,
456 request.priority(),
457 request.params(),
458 ConnectionTimeout(),
459 client_socket_factory_,
460 host_resolver_,
461 delegate,
462 net_log_));
465 base::TimeDelta
466 TransportClientSocketPool::TransportConnectJobFactory::ConnectionTimeout()
467 const {
468 return base::TimeDelta::FromSeconds(kTransportConnectJobTimeoutInSeconds);
471 TransportClientSocketPool::TransportClientSocketPool(
472 int max_sockets,
473 int max_sockets_per_group,
474 HostResolver* host_resolver,
475 ClientSocketFactory* client_socket_factory,
476 NetLog* net_log)
477 : base_(NULL,
478 max_sockets,
479 max_sockets_per_group,
480 ClientSocketPool::unused_idle_socket_timeout(),
481 ClientSocketPool::used_idle_socket_timeout(),
482 new TransportConnectJobFactory(client_socket_factory,
483 host_resolver,
484 net_log)) {
485 base_.EnableConnectBackupJobs();
488 TransportClientSocketPool::~TransportClientSocketPool() {}
490 int TransportClientSocketPool::RequestSocket(
491 const std::string& group_name,
492 const void* params,
493 RequestPriority priority,
494 ClientSocketHandle* handle,
495 const CompletionCallback& callback,
496 const BoundNetLog& net_log) {
497 const scoped_refptr<TransportSocketParams>* casted_params =
498 static_cast<const scoped_refptr<TransportSocketParams>*>(params);
500 NetLogTcpClientSocketPoolRequestedSocket(net_log, casted_params);
502 return base_.RequestSocket(group_name, *casted_params, priority, handle,
503 callback, net_log);
506 void TransportClientSocketPool::NetLogTcpClientSocketPoolRequestedSocket(
507 const BoundNetLog& net_log,
508 const scoped_refptr<TransportSocketParams>* casted_params) {
509 if (net_log.GetCaptureMode().enabled()) {
510 // TODO(eroman): Split out the host and port parameters.
511 net_log.AddEvent(
512 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET,
513 CreateNetLogHostPortPairCallback(
514 &casted_params->get()->destination().host_port_pair()));
518 void TransportClientSocketPool::RequestSockets(
519 const std::string& group_name,
520 const void* params,
521 int num_sockets,
522 const BoundNetLog& net_log) {
523 const scoped_refptr<TransportSocketParams>* casted_params =
524 static_cast<const scoped_refptr<TransportSocketParams>*>(params);
526 if (net_log.GetCaptureMode().enabled()) {
527 // TODO(eroman): Split out the host and port parameters.
528 net_log.AddEvent(
529 NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKETS,
530 CreateNetLogHostPortPairCallback(
531 &casted_params->get()->destination().host_port_pair()));
534 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
537 void TransportClientSocketPool::CancelRequest(
538 const std::string& group_name,
539 ClientSocketHandle* handle) {
540 base_.CancelRequest(group_name, handle);
543 void TransportClientSocketPool::ReleaseSocket(
544 const std::string& group_name,
545 scoped_ptr<StreamSocket> socket,
546 int id) {
547 base_.ReleaseSocket(group_name, socket.Pass(), id);
550 void TransportClientSocketPool::FlushWithError(int error) {
551 base_.FlushWithError(error);
554 void TransportClientSocketPool::CloseIdleSockets() {
555 base_.CloseIdleSockets();
558 int TransportClientSocketPool::IdleSocketCount() const {
559 return base_.idle_socket_count();
562 int TransportClientSocketPool::IdleSocketCountInGroup(
563 const std::string& group_name) const {
564 return base_.IdleSocketCountInGroup(group_name);
567 LoadState TransportClientSocketPool::GetLoadState(
568 const std::string& group_name, const ClientSocketHandle* handle) const {
569 return base_.GetLoadState(group_name, handle);
572 base::DictionaryValue* TransportClientSocketPool::GetInfoAsValue(
573 const std::string& name,
574 const std::string& type,
575 bool include_nested_pools) const {
576 return base_.GetInfoAsValue(name, type);
579 base::TimeDelta TransportClientSocketPool::ConnectionTimeout() const {
580 return base_.ConnectionTimeout();
583 bool TransportClientSocketPool::IsStalled() const {
584 return base_.IsStalled();
587 void TransportClientSocketPool::AddHigherLayeredPool(
588 HigherLayeredPool* higher_pool) {
589 base_.AddHigherLayeredPool(higher_pool);
592 void TransportClientSocketPool::RemoveHigherLayeredPool(
593 HigherLayeredPool* higher_pool) {
594 base_.RemoveHigherLayeredPool(higher_pool);
597 } // namespace net