blob: ab959cf06e2dde5c6c2d59f2f6526a53968a99a5 [file] [log] [blame]
[email protected]e34400c32012-01-24 02:49:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ff579d42009-06-24 15:47:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/socket/client_socket_pool_base.h"
6
[email protected]5e6efa52011-06-27 17:26:417#include <math.h>
[email protected]ff579d42009-06-24 15:47:028#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:159#include "base/format_macros.h"
[email protected]5e6efa52011-06-27 17:26:4110#include "base/logging.h"
[email protected]ff579d42009-06-24 15:47:0211#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/stats_counters.h"
[email protected]7286e3fc2011-07-19 22:13:2413#include "base/stl_util.h"
[email protected]5e6efa52011-06-27 17:26:4114#include "base/string_number_conversions.h"
[email protected]fd4fe0b2010-02-08 23:02:1515#include "base/string_util.h"
[email protected]ff579d42009-06-24 15:47:0216#include "base/time.h"
[email protected]9349cfb2010-08-31 18:00:5317#include "base/values.h"
[email protected]9e743cd2010-03-16 07:03:5318#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0219#include "net/base/net_errors.h"
20#include "net/socket/client_socket_handle.h"
21
22using base::TimeDelta;
23
24namespace {
25
[email protected]64770b7d2011-11-16 04:30:4126// Indicate whether we should enable idle socket cleanup timer. When timer is
27// disabled, sockets are closed next time a socket request is made.
28bool g_cleanup_timer_enabled = true;
29
[email protected]ff579d42009-06-24 15:47:0230// The timeout value, in seconds, used to clean up idle sockets that can't be
31// reused.
32//
33// Note: It's important to close idle sockets that have received data as soon
34// as possible because the received data may cause BSOD on Windows XP under
35// some conditions. See http://crbug.com/4606.
36const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
37
[email protected]c847c2a2011-04-08 13:56:1438// Indicate whether or not we should establish a new transport layer connection
39// after a certain timeout has passed without receiving an ACK.
[email protected]06d94042010-08-25 01:45:2240bool g_connect_backup_jobs_enabled = true;
41
[email protected]5e6efa52011-06-27 17:26:4142double g_socket_reuse_policy_penalty_exponent = -1;
43int g_socket_reuse_policy = -1;
44
[email protected]ff579d42009-06-24 15:47:0245} // namespace
46
47namespace net {
48
[email protected]5e6efa52011-06-27 17:26:4149int GetSocketReusePolicy() {
50 return g_socket_reuse_policy;
51}
52
53void SetSocketReusePolicy(int policy) {
54 DCHECK_GE(policy, 0);
55 DCHECK_LE(policy, 2);
56 if (policy > 2 || policy < 0) {
57 LOG(ERROR) << "Invalid socket reuse policy";
58 return;
59 }
60
61 double exponents[] = { 0, 0.25, -1 };
62 g_socket_reuse_policy_penalty_exponent = exponents[policy];
63 g_socket_reuse_policy = policy;
64
65 VLOG(1) << "Setting g_socket_reuse_policy_penalty_exponent = "
66 << g_socket_reuse_policy_penalty_exponent;
67}
68
[email protected]2ab05b52009-07-01 23:57:5869ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3470 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3071 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5372 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5873 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3474 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5875 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0276 net_log_(net_log),
[email protected]2c2bef152010-10-13 00:55:0377 idle_(true),
78 preconnect_state_(NOT_PRECONNECT) {
[email protected]2ab05b52009-07-01 23:57:5879 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5880 DCHECK(delegate);
[email protected]06650c52010-06-03 00:49:1781 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]2ab05b52009-07-01 23:57:5882}
83
[email protected]fd7b7c92009-08-20 19:38:3084ConnectJob::~ConnectJob() {
[email protected]06650c52010-06-03 00:49:1785 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]fd7b7c92009-08-20 19:38:3086}
[email protected]2ab05b52009-07-01 23:57:5887
[email protected]2c2bef152010-10-13 00:55:0388void ConnectJob::Initialize(bool is_preconnect) {
89 if (is_preconnect)
90 preconnect_state_ = UNUSED_PRECONNECT;
91 else
92 preconnect_state_ = NOT_PRECONNECT;
93}
94
[email protected]974ebd62009-08-03 23:14:3495int ConnectJob::Connect() {
96 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0297 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3098
[email protected]a2006ece2010-04-23 16:44:0299 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:30100
[email protected]06650c52010-06-03 00:49:17101 LogConnectStart();
102
[email protected]fd7b7c92009-08-20 19:38:30103 int rv = ConnectInternal();
104
105 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:17106 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30107 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:30108 }
109
110 return rv;
111}
112
[email protected]2c2bef152010-10-13 00:55:03113void ConnectJob::UseForNormalRequest() {
114 DCHECK_EQ(UNUSED_PRECONNECT, preconnect_state_);
115 preconnect_state_ = USED_PRECONNECT;
116}
117
[email protected]3268023f2011-05-05 00:08:10118void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:17119 if (socket) {
[email protected]00cd9c42010-11-02 20:15:57120 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET, make_scoped_refptr(
121 new NetLogSourceParameter("source_dependency",
122 socket->NetLog().source())));
[email protected]06650c52010-06-03 00:49:17123 }
124 socket_.reset(socket);
125}
126
[email protected]fd7b7c92009-08-20 19:38:30127void ConnectJob::NotifyDelegateOfCompletion(int rv) {
128 // The delegate will delete |this|.
129 Delegate *delegate = delegate_;
130 delegate_ = NULL;
131
[email protected]06650c52010-06-03 00:49:17132 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30133 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34134}
135
[email protected]a796bcec2010-03-22 17:17:26136void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
137 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02138 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26139}
140
[email protected]06650c52010-06-03 00:49:17141void ConnectJob::LogConnectStart() {
142 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT,
[email protected]00cd9c42010-11-02 20:15:57143 make_scoped_refptr(new NetLogStringParameter("group_name", group_name_)));
[email protected]06650c52010-06-03 00:49:17144}
145
146void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]d7fd1782011-02-08 19:16:43147 net_log().EndEventWithNetErrorCode(
148 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17149}
150
[email protected]974ebd62009-08-03 23:14:34151void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40152 // Make sure the socket is NULL before calling into |delegate|.
153 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30154
[email protected]ec11be62010-04-28 19:28:09155 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT, NULL);
[email protected]fd7b7c92009-08-20 19:38:30156
157 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34158}
159
[email protected]d80a4322009-08-14 07:07:49160namespace internal {
161
[email protected]fd4fe0b2010-02-08 23:02:15162ClientSocketPoolBaseHelper::Request::Request(
163 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41164 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15165 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20166 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03167 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53168 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13169 : handle_(handle),
170 callback_(callback),
171 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20172 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03173 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53174 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15175
176ClientSocketPoolBaseHelper::Request::~Request() {}
177
[email protected]d80a4322009-08-14 07:07:49178ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53179 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02180 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16181 base::TimeDelta unused_idle_socket_timeout,
182 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38183 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02184 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53185 connecting_socket_count_(0),
186 handed_out_socket_count_(0),
187 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02188 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41189 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16190 unused_idle_socket_timeout_(unused_idle_socket_timeout),
191 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35192 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22193 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13194 pool_generation_number_(0),
[email protected]49639fa2011-12-20 23:22:41195 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]211d2172009-07-22 15:48:53196 DCHECK_LE(0, max_sockets_per_group);
197 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52198
[email protected]232a5812011-03-04 22:42:08199 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53200}
[email protected]ff579d42009-06-24 15:47:02201
[email protected]d80a4322009-08-14 07:07:49202ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13203 // Clean up any idle sockets and pending connect jobs. Assert that we have no
204 // remaining active sockets or pending requests. They should have all been
205 // cleaned up prior to |this| being destroyed.
206 Flush();
207 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21208 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29209 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48210 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52211
[email protected]232a5812011-03-04 22:42:08212 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02213}
214
[email protected]49639fa2011-12-20 23:22:41215ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
216
[email protected]ff579d42009-06-24 15:47:02217// InsertRequestIntoQueue inserts the request into the queue based on
218// priority. Highest priorities are closest to the front. Older requests are
219// prioritized over requests of equal priority.
220//
221// static
[email protected]d80a4322009-08-14 07:07:49222void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
223 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02224 RequestQueue::iterator it = pending_requests->begin();
[email protected]ac790b42009-12-02 04:31:31225 while (it != pending_requests->end() && r->priority() >= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02226 ++it;
227 pending_requests->insert(it, r);
228}
229
[email protected]fd7b7c92009-08-20 19:38:30230// static
231const ClientSocketPoolBaseHelper::Request*
232ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05233 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30234 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02235 group->mutable_pending_requests()->erase(it);
236 // If there are no more requests, we kill the backup timer.
237 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00238 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30239 return req;
240}
241
[email protected]51fdc7c2012-04-10 19:19:48242void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
243 CHECK(pool);
244 CHECK(!ContainsKey(higher_layer_pools_, pool));
245 higher_layer_pools_.insert(pool);
246}
247
248void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
249 CHECK(pool);
250 CHECK(ContainsKey(higher_layer_pools_, pool));
251 higher_layer_pools_.erase(pool);
252}
253
[email protected]d80a4322009-08-14 07:07:49254int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02255 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49256 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41257 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03258 CHECK(request->handle());
259
[email protected]64770b7d2011-11-16 04:30:41260 // Cleanup any timed-out idle sockets if no timer is used.
261 if (!use_cleanup_timer_)
262 CleanupIdleSockets(false);
263
[email protected]ec11be62010-04-28 19:28:09264 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]aed99ef02010-08-26 14:04:32265 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26266
[email protected]fd4fe0b2010-02-08 23:02:15267 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17268 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43269 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21270 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17271 delete request;
272 } else {
[email protected]aed99ef02010-08-26 14:04:32273 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]e7e99322010-05-04 23:30:17274 }
[email protected]fd4fe0b2010-02-08 23:02:15275 return rv;
276}
277
[email protected]2c2bef152010-10-13 00:55:03278void ClientSocketPoolBaseHelper::RequestSockets(
279 const std::string& group_name,
280 const Request& request,
281 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41282 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03283 DCHECK(!request.handle());
284
[email protected]64770b7d2011-11-16 04:30:41285 // Cleanup any timed out idle sockets if no timer is used.
286 if (!use_cleanup_timer_)
287 CleanupIdleSockets(false);
288
[email protected]2c2bef152010-10-13 00:55:03289 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03290 num_sockets = max_sockets_per_group_;
291 }
292
293 request.net_log().BeginEvent(
294 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]00cd9c42010-11-02 20:15:57295 make_scoped_refptr(new NetLogIntegerParameter(
296 "num_sockets", num_sockets)));
[email protected]2c2bef152010-10-13 00:55:03297
298 Group* group = GetOrCreateGroup(group_name);
299
[email protected]3c819f522010-12-02 02:03:12300 // RequestSocketsInternal() may delete the group.
301 bool deleted_group = false;
302
[email protected]d7fd1782011-02-08 19:16:43303 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03304 for (int num_iterations_left = num_sockets;
305 group->NumActiveSocketSlots() < num_sockets &&
306 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43307 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03308 if (rv < 0 && rv != ERR_IO_PENDING) {
309 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12310 if (!ContainsKey(group_map_, group_name))
311 deleted_group = true;
312 break;
313 }
314 if (!ContainsKey(group_map_, group_name)) {
315 // Unexpected. The group should only be getting deleted on synchronous
316 // error.
317 NOTREACHED();
318 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03319 break;
320 }
321 }
322
[email protected]3c819f522010-12-02 02:03:12323 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03324 RemoveGroup(group_name);
325
[email protected]d7fd1782011-02-08 19:16:43326 if (rv == ERR_IO_PENDING)
327 rv = OK;
328 request.net_log().EndEventWithNetErrorCode(
329 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03330}
331
[email protected]fd4fe0b2010-02-08 23:02:15332int ClientSocketPoolBaseHelper::RequestSocketInternal(
333 const std::string& group_name,
334 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49335 DCHECK_GE(request->priority(), 0);
[email protected]d80a4322009-08-14 07:07:49336 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03337 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32338 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02339
[email protected]2c2bef152010-10-13 00:55:03340 if (!(request->flags() & NO_IDLE_SOCKETS)) {
341 // Try to reuse a socket.
342 if (AssignIdleSocketToGroup(request, group))
343 return OK;
344 }
345
346 if (!preconnecting && group->TryToUsePreconnectConnectJob())
347 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10348
[email protected]43a21b82010-06-10 21:30:54349 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20350 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
351 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48352 // TODO(willchan): Consider whether or not we need to close a socket in a
353 // higher layered group. I don't think this makes sense since we would just
354 // reuse that socket then if we needed one and wouldn't make it down to this
355 // layer.
[email protected]43a21b82010-06-10 21:30:54356 request->net_log().AddEvent(
357 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
358 return ERR_IO_PENDING;
359 }
360
[email protected]5acdce12011-03-30 13:00:20361 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48362 // NOTE(mmenke): Wonder if we really need different code for each case
363 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54364 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48365 // There's an idle socket in this pool. Either that's because there's
366 // still one in this group, but we got here due to preconnecting bypassing
367 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46368 bool closed = CloseOneIdleSocketExceptInGroup(group);
369 if (preconnecting && !closed)
370 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54371 } else {
[email protected]40dee91a2012-04-17 19:25:46372 // We could check if we really have a stalled group here, but it requires
373 // a scan of all groups, so just flip a flag here, and do the check later.
374 request->net_log().AddEvent(
375 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
376 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54377 }
378 }
379
[email protected]51fdc7c2012-04-10 19:19:48380 // We couldn't find a socket to reuse, and there's space to allocate one,
381 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58382 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17383 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02384
[email protected]2c2bef152010-10-13 00:55:03385 connect_job->Initialize(preconnecting);
[email protected]2ab05b52009-07-01 23:57:58386 int rv = connect_job->Connect();
387 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17388 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03389 if (!preconnecting) {
390 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
391 handle, base::TimeDelta(), group, request->net_log());
392 } else {
393 AddIdleSocket(connect_job->ReleaseSocket(), group);
394 }
[email protected]2ab05b52009-07-01 23:57:58395 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32396 // If we don't have any sockets in this group, set a timer for potentially
397 // creating a new one. If the SYN is lost, this backup socket may complete
398 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03399 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00400 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32401 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03402 }
[email protected]6b624c62010-03-14 08:37:32403
[email protected]211d2172009-07-22 15:48:53404 connecting_socket_count_++;
405
[email protected]aed99ef02010-08-26 14:04:32406 group->AddJob(connect_job.release());
[email protected]a2006ece2010-04-23 16:44:02407 } else {
[email protected]06650c52010-06-03 00:49:17408 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10409 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52410 if (!preconnecting) {
411 DCHECK(handle);
412 connect_job->GetAdditionalErrorState(handle);
413 error_socket = connect_job->ReleaseSocket();
414 }
[email protected]e772db3f2010-07-12 18:11:13415 if (error_socket) {
416 HandOutSocket(error_socket, false /* not reused */, handle,
[email protected]aed99ef02010-08-26 14:04:32417 base::TimeDelta(), group, request->net_log());
418 } else if (group->IsEmpty()) {
419 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21420 }
[email protected]2ab05b52009-07-01 23:57:58421 }
[email protected]ff579d42009-06-24 15:47:02422
[email protected]2ab05b52009-07-01 23:57:58423 return rv;
[email protected]ff579d42009-06-24 15:47:02424}
425
[email protected]05ea9ff2010-07-15 19:08:21426bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup(
[email protected]aed99ef02010-08-26 14:04:32427 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22428 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
429 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
[email protected]5e6efa52011-06-27 17:26:41430 double max_score = -1;
[email protected]e1b54dc2010-10-06 21:27:22431
432 // Iterate through the idle sockets forwards (oldest to newest)
433 // * Delete any disconnected ones.
434 // * If we find a used idle socket, assign to |idle_socket|. At the end,
435 // the |idle_socket_it| will be set to the newest used idle socket.
436 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
437 it != idle_sockets->end();) {
438 if (!it->socket->IsConnectedAndIdle()) {
439 DecrementIdleCount();
440 delete it->socket;
441 it = idle_sockets->erase(it);
442 continue;
[email protected]eb5a99382010-07-11 03:18:26443 }
[email protected]e1b54dc2010-10-06 21:27:22444
445 if (it->socket->WasEverUsed()) {
446 // We found one we can reuse!
[email protected]5e6efa52011-06-27 17:26:41447 double score = 0;
448 int64 bytes_read = it->socket->NumBytesRead();
449 double num_kb = static_cast<double>(bytes_read) / 1024.0;
450 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds();
451 idle_time_sec = std::max(1, idle_time_sec);
452
453 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) {
454 score = num_kb / pow(idle_time_sec,
455 g_socket_reuse_policy_penalty_exponent);
456 }
457
458 // Equality to prefer recently used connection.
459 if (score >= max_score) {
460 idle_socket_it = it;
461 max_score = score;
462 }
[email protected]e1b54dc2010-10-06 21:27:22463 }
464
465 ++it;
[email protected]eb5a99382010-07-11 03:18:26466 }
[email protected]e1b54dc2010-10-06 21:27:22467
468 // If we haven't found an idle socket, that means there are no used idle
469 // sockets. Pick the oldest (first) idle socket (FIFO).
470
471 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
472 idle_socket_it = idle_sockets->begin();
473
474 if (idle_socket_it != idle_sockets->end()) {
475 DecrementIdleCount();
476 base::TimeDelta idle_time =
477 base::TimeTicks::Now() - idle_socket_it->start_time;
478 IdleSocket idle_socket = *idle_socket_it;
479 idle_sockets->erase(idle_socket_it);
480 HandOutSocket(
481 idle_socket.socket,
482 idle_socket.socket->WasEverUsed(),
483 request->handle(),
484 idle_time,
485 group,
486 request->net_log());
487 return true;
488 }
489
[email protected]eb5a99382010-07-11 03:18:26490 return false;
491}
492
[email protected]06650c52010-06-03 00:49:17493// static
494void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
495 const NetLog::Source& connect_job_source, const Request* request) {
496 request->net_log().AddEvent(
497 NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
[email protected]00cd9c42010-11-02 20:15:57498 make_scoped_refptr(new NetLogSourceParameter(
499 "source_dependency", connect_job_source)));
[email protected]06650c52010-06-03 00:49:17500}
501
[email protected]d80a4322009-08-14 07:07:49502void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21503 const std::string& group_name, ClientSocketHandle* handle) {
504 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
505 if (callback_it != pending_callback_map_.end()) {
506 int result = callback_it->second.result;
507 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10508 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21509 if (socket) {
510 if (result != OK)
511 socket->Disconnect();
512 ReleaseSocket(handle->group_name(), socket, handle->id());
513 }
514 return;
515 }
[email protected]b6501d3d2010-06-03 23:53:34516
[email protected]ff579d42009-06-24 15:47:02517 CHECK(ContainsKey(group_map_, group_name));
518
[email protected]aed99ef02010-08-26 14:04:32519 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02520
[email protected]ff579d42009-06-24 15:47:02521 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32522 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
523 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49524 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03525 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]ec11be62010-04-28 19:28:09526 req->net_log().AddEvent(NetLog::TYPE_CANCELLED, NULL);
527 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]eb5a99382010-07-11 03:18:26528
529 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32530 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
531 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26532 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34533 }
[email protected]eb5a99382010-07-11 03:18:26534 break;
[email protected]ff579d42009-06-24 15:47:02535 }
536 }
[email protected]ff579d42009-06-24 15:47:02537}
538
[email protected]2abfe90a2010-08-25 17:49:51539bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
540 return ContainsKey(group_map_, group_name);
541}
542
[email protected]d80a4322009-08-14 07:07:49543void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02544 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14545 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02546}
547
[email protected]d80a4322009-08-14 07:07:49548int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02549 const std::string& group_name) const {
550 GroupMap::const_iterator i = group_map_.find(group_name);
551 CHECK(i != group_map_.end());
552
[email protected]aed99ef02010-08-26 14:04:32553 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02554}
555
[email protected]d80a4322009-08-14 07:07:49556LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02557 const std::string& group_name,
558 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21559 if (ContainsKey(pending_callback_map_, handle))
560 return LOAD_STATE_CONNECTING;
561
[email protected]ff579d42009-06-24 15:47:02562 if (!ContainsKey(group_map_, group_name)) {
563 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
564 << " for handle: " << handle;
565 return LOAD_STATE_IDLE;
566 }
567
568 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32569 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02570
[email protected]ff579d42009-06-24 15:47:02571 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32572 RequestQueue::const_iterator it = group.pending_requests().begin();
573 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49574 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32575 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57576 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32577 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
578 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21579 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57580 }
581 return max_state;
582 } else {
583 // TODO(wtc): Add a state for being on the wait list.
584 // See http://www.crbug.com/5077.
585 return LOAD_STATE_IDLE;
586 }
[email protected]ff579d42009-06-24 15:47:02587 }
588 }
589
590 NOTREACHED();
591 return LOAD_STATE_IDLE;
592}
593
[email protected]ba00b492010-09-08 14:53:38594DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27595 const std::string& name, const std::string& type) const {
596 DictionaryValue* dict = new DictionaryValue();
597 dict->SetString("name", name);
598 dict->SetString("type", type);
599 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
600 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
601 dict->SetInteger("idle_socket_count", idle_socket_count_);
602 dict->SetInteger("max_socket_count", max_sockets_);
603 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
604 dict->SetInteger("pool_generation_number", pool_generation_number_);
605
606 if (group_map_.empty())
607 return dict;
608
609 DictionaryValue* all_groups_dict = new DictionaryValue();
610 for (GroupMap::const_iterator it = group_map_.begin();
611 it != group_map_.end(); it++) {
612 const Group* group = it->second;
613 DictionaryValue* group_dict = new DictionaryValue();
614
615 group_dict->SetInteger("pending_request_count",
616 group->pending_requests().size());
617 if (!group->pending_requests().empty()) {
618 group_dict->SetInteger("top_pending_priority",
619 group->TopPendingPriority());
620 }
621
622 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07623
624 ListValue* idle_socket_list = new ListValue();
[email protected]e1b54dc2010-10-06 21:27:22625 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07626 for (idle_socket = group->idle_sockets().begin();
627 idle_socket != group->idle_sockets().end();
628 idle_socket++) {
629 int source_id = idle_socket->socket->NetLog().source().id;
630 idle_socket_list->Append(Value::CreateIntegerValue(source_id));
631 }
632 group_dict->Set("idle_sockets", idle_socket_list);
633
634 ListValue* connect_jobs_list = new ListValue();
[email protected]2c2bef152010-10-13 00:55:03635 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07636 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
637 int source_id = (*job)->net_log().source().id;
638 connect_jobs_list->Append(Value::CreateIntegerValue(source_id));
639 }
640 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27641
642 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48643 group->IsStalledOnPoolMaxSockets(
644 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00645 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27646
647 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
648 }
649 dict->Set("groups", all_groups_dict);
650 return dict;
651}
652
[email protected]d80a4322009-08-14 07:07:49653bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16654 base::TimeTicks now,
655 base::TimeDelta timeout) const {
656 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01657 if (timed_out)
658 return true;
659 if (socket->WasEverUsed())
660 return !socket->IsConnectedAndIdle();
661 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02662}
663
[email protected]d80a4322009-08-14 07:07:49664void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02665 if (idle_socket_count_ == 0)
666 return;
667
668 // Current time value. Retrieving it once at the function start rather than
669 // inside the inner loop, since it shouldn't change by any meaningful amount.
670 base::TimeTicks now = base::TimeTicks::Now();
671
672 GroupMap::iterator i = group_map_.begin();
673 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32674 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02675
[email protected]e1b54dc2010-10-06 21:27:22676 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32677 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16678 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01679 j->socket->WasEverUsed() ?
680 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16681 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02682 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32683 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02684 DecrementIdleCount();
685 } else {
686 ++j;
687 }
688 }
689
690 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32691 if (group->IsEmpty()) {
692 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02693 } else {
694 ++i;
695 }
696 }
697}
698
[email protected]aed99ef02010-08-26 14:04:32699ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
700 const std::string& group_name) {
701 GroupMap::iterator it = group_map_.find(group_name);
702 if (it != group_map_.end())
703 return it->second;
704 Group* group = new Group;
705 group_map_[group_name] = group;
706 return group;
707}
708
709void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
710 GroupMap::iterator it = group_map_.find(group_name);
711 CHECK(it != group_map_.end());
712
713 RemoveGroup(it);
714}
715
716void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
717 delete it->second;
718 group_map_.erase(it);
719}
720
[email protected]06d94042010-08-25 01:45:22721// static
[email protected]2d6728692011-03-12 01:39:55722bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
723 return g_connect_backup_jobs_enabled;
724}
725
726// static
[email protected]636b8252011-04-08 19:56:54727bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
728 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22729 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54730 return old_value;
[email protected]06d94042010-08-25 01:45:22731}
732
733void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
734 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
735}
736
[email protected]d80a4322009-08-14 07:07:49737void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41738 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
739 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02740}
741
[email protected]d80a4322009-08-14 07:07:49742void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02743 if (--idle_socket_count_ == 0)
744 timer_.Stop();
745}
746
[email protected]64770b7d2011-11-16 04:30:41747// static
748bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
749 return g_cleanup_timer_enabled;
750}
751
752// static
753bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
754 bool old_value = g_cleanup_timer_enabled;
755 g_cleanup_timer_enabled = enabled;
756 return old_value;
757}
758
759void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
760 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
761 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
762}
763
[email protected]eb5a99382010-07-11 03:18:26764void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10765 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26766 int id) {
[email protected]ff579d42009-06-24 15:47:02767 GroupMap::iterator i = group_map_.find(group_name);
768 CHECK(i != group_map_.end());
769
[email protected]aed99ef02010-08-26 14:04:32770 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02771
[email protected]b1f031dd2010-03-02 23:19:33772 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53773 handed_out_socket_count_--;
774
[email protected]aed99ef02010-08-26 14:04:32775 CHECK_GT(group->active_socket_count(), 0);
776 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02777
[email protected]a7e38572010-06-07 18:22:24778 const bool can_reuse = socket->IsConnectedAndIdle() &&
779 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02780 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26781 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01782 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32783 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02784 } else {
785 delete socket;
786 }
[email protected]05ea9ff2010-07-15 19:08:21787
[email protected]eb5a99382010-07-11 03:18:26788 CheckForStalledSocketGroups();
789}
[email protected]ff579d42009-06-24 15:47:02790
[email protected]eb5a99382010-07-11 03:18:26791void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
792 // If we have idle sockets, see if we can give one to the top-stalled group.
793 std::string top_group_name;
794 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21795 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26796 return;
[email protected]4f2abec2010-02-03 18:10:16797
[email protected]eb5a99382010-07-11 03:18:26798 if (ReachedMaxSocketsLimit()) {
799 if (idle_socket_count() > 0) {
800 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54801 } else {
[email protected]eb5a99382010-07-11 03:18:26802 // We can't activate more sockets since we're already at our global
803 // limit.
[email protected]4f2abec2010-02-03 18:10:16804 return;
[email protected]d7027bb2010-05-10 18:58:54805 }
[email protected]4f2abec2010-02-03 18:10:16806 }
[email protected]eb5a99382010-07-11 03:18:26807
808 // Note: we don't loop on waking stalled groups. If the stalled group is at
809 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21810 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26811 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21812 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02813}
814
[email protected]211d2172009-07-22 15:48:53815// Search for the highest priority pending request, amongst the groups that
816// are not at the |max_sockets_per_group_| limit. Note: for requests with
817// the same priority, the winner is based on group hash ordering (and not
818// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48819bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
820 Group** group,
821 std::string* group_name) const {
822 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53823 Group* top_group = NULL;
824 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21825 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48826 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53827 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32828 Group* curr_group = i->second;
829 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53830 if (queue.empty())
831 continue;
[email protected]51fdc7c2012-04-10 19:19:48832 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
833 if (!group)
834 return true;
[email protected]05ea9ff2010-07-15 19:08:21835 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41836 bool has_higher_priority = !top_group ||
[email protected]aed99ef02010-08-26 14:04:32837 curr_group->TopPendingPriority() < top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41838 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32839 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41840 top_group_name = &i->first;
841 }
[email protected]211d2172009-07-22 15:48:53842 }
843 }
[email protected]05ea9ff2010-07-15 19:08:21844
[email protected]211d2172009-07-22 15:48:53845 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48846 CHECK(group);
[email protected]211d2172009-07-22 15:48:53847 *group = top_group;
848 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48849 } else {
850 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53851 }
[email protected]05ea9ff2010-07-15 19:08:21852 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53853}
854
[email protected]d80a4322009-08-14 07:07:49855void ClientSocketPoolBaseHelper::OnConnectJobComplete(
856 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58857 DCHECK_NE(ERR_IO_PENDING, result);
858 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02859 GroupMap::iterator group_it = group_map_.find(group_name);
860 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32861 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02862
[email protected]3268023f2011-05-05 00:08:10863 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02864
[email protected]9e743cd2010-03-16 07:03:53865 BoundNetLog job_log = job->net_log();
[email protected]5fc08e32009-07-15 17:09:57866
[email protected]4d3b05d2010-01-27 21:27:29867 if (result == OK) {
868 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32869 RemoveConnectJob(job, group);
870 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29871 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02872 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17873 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29874 HandOutSocket(
875 socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32876 base::TimeDelta(), group, r->net_log());
[email protected]06650c52010-06-03 00:49:17877 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]05ea9ff2010-07-15 19:08:21878 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57879 } else {
[email protected]0f873e82010-09-02 16:09:01880 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32881 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21882 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57883 }
[email protected]94c20472010-01-14 08:14:36884 } else {
[email protected]e772db3f2010-07-12 18:11:13885 // If we got a socket, it must contain error information so pass that
886 // up so that the caller can retrieve it.
887 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32888 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29889 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02890 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17891 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18892 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32893 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13894 if (socket.get()) {
895 handed_out_socket = true;
896 HandOutSocket(socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32897 base::TimeDelta(), group, r->net_log());
[email protected]e772db3f2010-07-12 18:11:13898 }
[email protected]d7fd1782011-02-08 19:16:43899 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL,
900 result);
[email protected]05ea9ff2010-07-15 19:08:21901 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18902 } else {
[email protected]aed99ef02010-08-26 14:04:32903 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29904 }
[email protected]05ea9ff2010-07-15 19:08:21905 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32906 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21907 CheckForStalledSocketGroups();
908 }
[email protected]ff579d42009-06-24 15:47:02909 }
[email protected]ff579d42009-06-24 15:47:02910}
911
[email protected]66761b952010-06-25 21:30:38912void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
913 Flush();
914}
915
[email protected]a7e38572010-06-07 18:22:24916void ClientSocketPoolBaseHelper::Flush() {
917 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14918 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52919 CloseIdleSockets();
[email protected]06f92462010-08-31 19:24:14920 AbortAllRequests();
[email protected]a554a8262010-05-20 00:13:52921}
922
[email protected]51fdc7c2012-04-10 19:19:48923bool ClientSocketPoolBaseHelper::IsStalled() const {
924 // If we are not using |max_sockets_|, then clearly we are not stalled
925 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
926 return false;
927 // So in order to be stalled we need to be using |max_sockets_| AND
928 // we need to have a request that is actually stalled on the global
929 // socket limit. To find such a request, we look for a group that
930 // a has more requests that jobs AND where the number of jobs is less
931 // than |max_sockets_per_group_|. (If the number of jobs is equal to
932 // |max_sockets_per_group_|, then the request is stalled on the group,
933 // which does not count.)
934 for (GroupMap::const_iterator it = group_map_.begin();
935 it != group_map_.end(); ++it) {
936 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
937 return true;
938 }
939 return false;
940}
941
[email protected]2c2bef152010-10-13 00:55:03942void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29943 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33944 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53945 connecting_socket_count_--;
946
[email protected]25eea382010-07-10 23:55:26947 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32948 DCHECK(ContainsKey(group->jobs(), job));
949 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26950
951 // If we've got no more jobs for this group, then we no longer need a
952 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32953 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00954 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26955
[email protected]8ae03f42010-07-07 19:08:10956 DCHECK(job);
957 delete job;
[email protected]2ab05b52009-07-01 23:57:58958}
[email protected]ff579d42009-06-24 15:47:02959
[email protected]8ae03f42010-07-07 19:08:10960void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21961 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51962 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21963 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32964 RemoveGroup(group_name);
965 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51966 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10967}
[email protected]06650c52010-06-03 00:49:17968
[email protected]8ae03f42010-07-07 19:08:10969void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21970 const std::string& group_name, Group* group) {
971 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32972 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21973 if (rv != ERR_IO_PENDING) {
974 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02975 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51976 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32977 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10978
[email protected]d7fd1782011-02-08 19:16:43979 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41980 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58981 }
982}
983
[email protected]d80a4322009-08-14 07:07:49984void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10985 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58986 bool reused,
987 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29988 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15989 Group* group,
[email protected]9e743cd2010-03-16 07:03:53990 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58991 DCHECK(socket);
992 handle->set_socket(socket);
993 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29994 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24995 handle->set_pool_id(pool_generation_number_);
[email protected]211d2172009-07-22 15:48:53996
[email protected]d13f51b2010-04-27 23:20:45997 if (reused) {
[email protected]ec11be62010-04-28 19:28:09998 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45999 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]00cd9c42010-11-02 20:15:571000 make_scoped_refptr(new NetLogIntegerParameter(
1001 "idle_ms", static_cast<int>(idle_time.InMilliseconds()))));
[email protected]fd4fe0b2010-02-08 23:02:151002 }
[email protected]d13f51b2010-04-27 23:20:451003
[email protected]06650c52010-06-03 00:49:171004 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]00cd9c42010-11-02 20:15:571005 make_scoped_refptr(new NetLogSourceParameter(
1006 "source_dependency", socket->NetLog().source())));
[email protected]fd4fe0b2010-02-08 23:02:151007
[email protected]211d2172009-07-22 15:48:531008 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321009 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021010}
1011
[email protected]d80a4322009-08-14 07:07:491012void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:101013 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:571014 DCHECK(socket);
1015 IdleSocket idle_socket;
1016 idle_socket.socket = socket;
1017 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571018
[email protected]aed99ef02010-08-26 14:04:321019 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571020 IncrementIdleCount();
1021}
1022
[email protected]d80a4322009-08-14 07:07:491023void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541024 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321025 Group* group = i->second;
1026 connecting_socket_count_ -= group->jobs().size();
1027 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321028
[email protected]5fc08e32009-07-15 17:09:571029 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321030 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141031 // RemoveGroup() will call .erase() which will invalidate the iterator,
1032 // but i will already have been incremented to a valid iterator before
1033 // RemoveGroup() is called.
1034 RemoveGroup(i++);
1035 } else {
1036 ++i;
1037 }
1038 }
1039 DCHECK_EQ(0, connecting_socket_count_);
1040}
1041
1042void ClientSocketPoolBaseHelper::AbortAllRequests() {
1043 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1044 Group* group = i->second;
1045
1046 RequestQueue pending_requests;
1047 pending_requests.swap(*group->mutable_pending_requests());
1048 for (RequestQueue::iterator it2 = pending_requests.begin();
1049 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031050 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141051 InvokeUserCallbackLater(
1052 request->handle(), request->callback(), ERR_ABORTED);
1053 }
1054
1055 // Delete group if no longer needed.
1056 if (group->IsEmpty()) {
1057 // RemoveGroup() will call .erase() which will invalidate the iterator,
1058 // but i will already have been incremented to a valid iterator before
1059 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321060 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541061 } else {
1062 ++i;
[email protected]5fc08e32009-07-15 17:09:571063 }
1064 }
1065}
1066
[email protected]d80a4322009-08-14 07:07:491067bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531068 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541069 int total = handed_out_socket_count_ + connecting_socket_count_ +
1070 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201071 // There can be more sockets than the limit since some requests can ignore
1072 // the limit
[email protected]c901f6d2010-04-27 17:48:281073 if (total < max_sockets_)
1074 return false;
[email protected]c901f6d2010-04-27 17:48:281075 return true;
[email protected]211d2172009-07-22 15:48:531076}
1077
[email protected]51fdc7c2012-04-10 19:19:481078bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1079 if (idle_socket_count() == 0)
1080 return false;
1081 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461082}
1083
1084bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1085 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541086 CHECK_GT(idle_socket_count(), 0);
1087
1088 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321089 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461090 if (exception_group == group)
1091 continue;
[email protected]e1b54dc2010-10-06 21:27:221092 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541093
[email protected]e1b54dc2010-10-06 21:27:221094 if (!idle_sockets->empty()) {
1095 delete idle_sockets->front().socket;
1096 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541097 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321098 if (group->IsEmpty())
1099 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541100
[email protected]dcbe168a2010-12-02 03:14:461101 return true;
[email protected]43a21b82010-06-10 21:30:541102 }
1103 }
1104
[email protected]51fdc7c2012-04-10 19:19:481105 return false;
1106}
[email protected]dcbe168a2010-12-02 03:14:461107
[email protected]51fdc7c2012-04-10 19:19:481108bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1109 // This pool doesn't have any idle sockets. It's possible that a pool at a
1110 // higher layer is holding one of this sockets active, but it's actually idle.
1111 // Query the higher layers.
1112 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1113 it != higher_layer_pools_.end(); ++it) {
1114 if ((*it)->CloseOneIdleConnection())
1115 return true;
1116 }
[email protected]dcbe168a2010-12-02 03:14:461117 return false;
[email protected]43a21b82010-06-10 21:30:541118}
1119
[email protected]05ea9ff2010-07-15 19:08:211120void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411121 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211122 CHECK(!ContainsKey(pending_callback_map_, handle));
1123 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1124 MessageLoop::current()->PostTask(
1125 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411126 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1127 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211128}
1129
1130void ClientSocketPoolBaseHelper::InvokeUserCallback(
1131 ClientSocketHandle* handle) {
1132 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1133
1134 // Exit if the request has already been cancelled.
1135 if (it == pending_callback_map_.end())
1136 return;
1137
1138 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411139 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211140 int result = it->second.result;
1141 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411142 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211143}
1144
[email protected]aed99ef02010-08-26 14:04:321145ClientSocketPoolBaseHelper::Group::Group()
1146 : active_socket_count_(0),
[email protected]96b939a2012-01-20 21:52:151147 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
[email protected]aed99ef02010-08-26 14:04:321148
[email protected]e4d9a9722011-05-12 00:16:001149ClientSocketPoolBaseHelper::Group::~Group() {
1150 CleanupBackupJob();
[email protected]aed99ef02010-08-26 14:04:321151}
1152
1153void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1154 const std::string& group_name,
1155 ClientSocketPoolBaseHelper* pool) {
1156 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411157 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321158 return;
1159
1160 MessageLoop::current()->PostDelayedTask(
1161 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411162 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1163 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001164 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321165}
1166
[email protected]2c2bef152010-10-13 00:55:031167bool ClientSocketPoolBaseHelper::Group::TryToUsePreconnectConnectJob() {
1168 for (std::set<ConnectJob*>::iterator it = jobs_.begin();
1169 it != jobs_.end(); ++it) {
1170 ConnectJob* job = *it;
1171 if (job->is_unused_preconnect()) {
1172 job->UseForNormalRequest();
1173 return true;
1174 }
1175 }
1176 return false;
1177}
1178
[email protected]aed99ef02010-08-26 14:04:321179void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1180 std::string group_name,
1181 ClientSocketPoolBaseHelper* pool) {
1182 // If there are no more jobs pending, there is no work to do.
1183 // If we've done our cleanups correctly, this should not happen.
1184 if (jobs_.empty()) {
1185 NOTREACHED();
1186 return;
1187 }
1188
1189 // If our backup job is waiting on DNS, or if we can't create any sockets
1190 // right now due to limits, just reset the timer.
1191 if (pool->ReachedMaxSocketsLimit() ||
1192 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1193 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1194 StartBackupSocketTimer(group_name, pool);
1195 return;
1196 }
1197
[email protected]a9fc8fc2011-05-10 02:41:071198 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441199 return;
[email protected]4baaf9d2010-08-31 15:15:441200
[email protected]aed99ef02010-08-26 14:04:321201 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1202 group_name, **pending_requests_.begin(), pool);
1203 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED, NULL);
1204 SIMPLE_STATS_COUNTER("socket.backup_created");
1205 int rv = backup_job->Connect();
1206 pool->connecting_socket_count_++;
1207 AddJob(backup_job);
1208 if (rv != ERR_IO_PENDING)
1209 pool->OnConnectJobComplete(rv, backup_job);
1210}
1211
1212void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
1213 // Delete active jobs.
1214 STLDeleteElements(&jobs_);
1215
1216 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411217 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321218}
1219
[email protected]d80a4322009-08-14 07:07:491220} // namespace internal
1221
[email protected]ff579d42009-06-24 15:47:021222} // namespace net