blob: 609208127f3a9bbb4eb1d3a0a23a7939cd483925 [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]8159a1c2012-06-07 00:00:1077 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5878 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5879 DCHECK(delegate);
[email protected]3aa4af042012-06-14 21:02:3180 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]2ab05b52009-07-01 23:57:5881}
82
[email protected]fd7b7c92009-08-20 19:38:3083ConnectJob::~ConnectJob() {
[email protected]3aa4af042012-06-14 21:02:3184 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]fd7b7c92009-08-20 19:38:3085}
[email protected]2ab05b52009-07-01 23:57:5886
[email protected]974ebd62009-08-03 23:14:3487int ConnectJob::Connect() {
88 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0289 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3090
[email protected]a2006ece2010-04-23 16:44:0291 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3092
[email protected]06650c52010-06-03 00:49:1793 LogConnectStart();
94
[email protected]fd7b7c92009-08-20 19:38:3095 int rv = ConnectInternal();
96
97 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:1798 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3099 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:30100 }
101
102 return rv;
103}
104
[email protected]3268023f2011-05-05 00:08:10105void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:17106 if (socket) {
[email protected]3aa4af042012-06-14 21:02:31107 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
108 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17109 }
110 socket_.reset(socket);
111}
112
[email protected]fd7b7c92009-08-20 19:38:30113void ConnectJob::NotifyDelegateOfCompletion(int rv) {
114 // The delegate will delete |this|.
115 Delegate *delegate = delegate_;
116 delegate_ = NULL;
117
[email protected]06650c52010-06-03 00:49:17118 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30119 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34120}
121
[email protected]a796bcec2010-03-22 17:17:26122void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
123 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02124 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26125}
126
[email protected]06650c52010-06-03 00:49:17127void ConnectJob::LogConnectStart() {
128 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT,
[email protected]3aa4af042012-06-14 21:02:31129 NetLog::StringCallback("group_name", &group_name_));
[email protected]06650c52010-06-03 00:49:17130}
131
132void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]d7fd1782011-02-08 19:16:43133 net_log().EndEventWithNetErrorCode(
134 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17135}
136
[email protected]974ebd62009-08-03 23:14:34137void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40138 // Make sure the socket is NULL before calling into |delegate|.
139 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30140
[email protected]3aa4af042012-06-14 21:02:31141 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30142
143 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34144}
145
[email protected]d80a4322009-08-14 07:07:49146namespace internal {
147
[email protected]fd4fe0b2010-02-08 23:02:15148ClientSocketPoolBaseHelper::Request::Request(
149 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41150 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15151 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20152 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03153 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53154 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13155 : handle_(handle),
156 callback_(callback),
157 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20158 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03159 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53160 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15161
162ClientSocketPoolBaseHelper::Request::~Request() {}
163
[email protected]d80a4322009-08-14 07:07:49164ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53165 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02166 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16167 base::TimeDelta unused_idle_socket_timeout,
168 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38169 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02170 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53171 connecting_socket_count_(0),
172 handed_out_socket_count_(0),
173 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02174 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41175 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16176 unused_idle_socket_timeout_(unused_idle_socket_timeout),
177 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35178 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22179 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13180 pool_generation_number_(0),
[email protected]49639fa2011-12-20 23:22:41181 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]211d2172009-07-22 15:48:53182 DCHECK_LE(0, max_sockets_per_group);
183 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52184
[email protected]232a5812011-03-04 22:42:08185 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53186}
[email protected]ff579d42009-06-24 15:47:02187
[email protected]d80a4322009-08-14 07:07:49188ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13189 // Clean up any idle sockets and pending connect jobs. Assert that we have no
190 // remaining active sockets or pending requests. They should have all been
191 // cleaned up prior to |this| being destroyed.
192 Flush();
193 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21194 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29195 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48196 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52197
[email protected]232a5812011-03-04 22:42:08198 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02199}
200
[email protected]2a848e0e2012-08-09 22:27:31201ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
202 : result(OK) {
203}
204
205ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
206 const CompletionCallback& callback_in, int result_in)
207 : callback(callback_in),
208 result(result_in) {
209}
210
[email protected]49639fa2011-12-20 23:22:41211ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
212
[email protected]ff579d42009-06-24 15:47:02213// InsertRequestIntoQueue inserts the request into the queue based on
214// priority. Highest priorities are closest to the front. Older requests are
215// prioritized over requests of equal priority.
216//
217// static
[email protected]d80a4322009-08-14 07:07:49218void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
219 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02220 RequestQueue::iterator it = pending_requests->begin();
[email protected]31ae7ab2012-04-24 21:09:05221 while (it != pending_requests->end() && r->priority() <= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02222 ++it;
223 pending_requests->insert(it, r);
224}
225
[email protected]fd7b7c92009-08-20 19:38:30226// static
227const ClientSocketPoolBaseHelper::Request*
228ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05229 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30230 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02231 group->mutable_pending_requests()->erase(it);
232 // If there are no more requests, we kill the backup timer.
233 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00234 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30235 return req;
236}
237
[email protected]51fdc7c2012-04-10 19:19:48238void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
239 CHECK(pool);
240 CHECK(!ContainsKey(higher_layer_pools_, pool));
241 higher_layer_pools_.insert(pool);
242}
243
244void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
245 CHECK(pool);
246 CHECK(ContainsKey(higher_layer_pools_, pool));
247 higher_layer_pools_.erase(pool);
248}
249
[email protected]d80a4322009-08-14 07:07:49250int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02251 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49252 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41253 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03254 CHECK(request->handle());
255
[email protected]64770b7d2011-11-16 04:30:41256 // Cleanup any timed-out idle sockets if no timer is used.
257 if (!use_cleanup_timer_)
258 CleanupIdleSockets(false);
259
[email protected]3aa4af042012-06-14 21:02:31260 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32261 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26262
[email protected]fd4fe0b2010-02-08 23:02:15263 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17264 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43265 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21266 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17267 delete request;
268 } else {
[email protected]aed99ef02010-08-26 14:04:32269 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]e7e99322010-05-04 23:30:17270 }
[email protected]fd4fe0b2010-02-08 23:02:15271 return rv;
272}
273
[email protected]2c2bef152010-10-13 00:55:03274void ClientSocketPoolBaseHelper::RequestSockets(
275 const std::string& group_name,
276 const Request& request,
277 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41278 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03279 DCHECK(!request.handle());
280
[email protected]64770b7d2011-11-16 04:30:41281 // Cleanup any timed out idle sockets if no timer is used.
282 if (!use_cleanup_timer_)
283 CleanupIdleSockets(false);
284
[email protected]2c2bef152010-10-13 00:55:03285 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03286 num_sockets = max_sockets_per_group_;
287 }
288
289 request.net_log().BeginEvent(
290 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31291 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03292
293 Group* group = GetOrCreateGroup(group_name);
294
[email protected]3c819f522010-12-02 02:03:12295 // RequestSocketsInternal() may delete the group.
296 bool deleted_group = false;
297
[email protected]d7fd1782011-02-08 19:16:43298 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03299 for (int num_iterations_left = num_sockets;
300 group->NumActiveSocketSlots() < num_sockets &&
301 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43302 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03303 if (rv < 0 && rv != ERR_IO_PENDING) {
304 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12305 if (!ContainsKey(group_map_, group_name))
306 deleted_group = true;
307 break;
308 }
309 if (!ContainsKey(group_map_, group_name)) {
310 // Unexpected. The group should only be getting deleted on synchronous
311 // error.
312 NOTREACHED();
313 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03314 break;
315 }
316 }
317
[email protected]3c819f522010-12-02 02:03:12318 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03319 RemoveGroup(group_name);
320
[email protected]d7fd1782011-02-08 19:16:43321 if (rv == ERR_IO_PENDING)
322 rv = OK;
323 request.net_log().EndEventWithNetErrorCode(
324 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03325}
326
[email protected]fd4fe0b2010-02-08 23:02:15327int ClientSocketPoolBaseHelper::RequestSocketInternal(
328 const std::string& group_name,
329 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49330 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03331 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32332 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02333
[email protected]2c2bef152010-10-13 00:55:03334 if (!(request->flags() & NO_IDLE_SOCKETS)) {
335 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10336 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03337 return OK;
338 }
339
[email protected]8159a1c2012-06-07 00:00:10340 // If there are more ConnectJobs than pending requests, don't need to do
341 // anything. Can just wait for the extra job to connect, and then assign it
342 // to the request.
343 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03344 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10345
[email protected]43a21b82010-06-10 21:30:54346 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20347 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
348 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48349 // TODO(willchan): Consider whether or not we need to close a socket in a
350 // higher layered group. I don't think this makes sense since we would just
351 // reuse that socket then if we needed one and wouldn't make it down to this
352 // layer.
[email protected]43a21b82010-06-10 21:30:54353 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31354 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54355 return ERR_IO_PENDING;
356 }
357
[email protected]5acdce12011-03-30 13:00:20358 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48359 // NOTE(mmenke): Wonder if we really need different code for each case
360 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54361 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48362 // There's an idle socket in this pool. Either that's because there's
363 // still one in this group, but we got here due to preconnecting bypassing
364 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46365 bool closed = CloseOneIdleSocketExceptInGroup(group);
366 if (preconnecting && !closed)
367 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54368 } else {
[email protected]40dee91a2012-04-17 19:25:46369 // We could check if we really have a stalled group here, but it requires
370 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31371 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46372 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54373 }
374 }
375
[email protected]51fdc7c2012-04-10 19:19:48376 // We couldn't find a socket to reuse, and there's space to allocate one,
377 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58378 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17379 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02380
[email protected]2ab05b52009-07-01 23:57:58381 int rv = connect_job->Connect();
382 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17383 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03384 if (!preconnecting) {
385 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
386 handle, base::TimeDelta(), group, request->net_log());
387 } else {
388 AddIdleSocket(connect_job->ReleaseSocket(), group);
389 }
[email protected]2ab05b52009-07-01 23:57:58390 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32391 // If we don't have any sockets in this group, set a timer for potentially
392 // creating a new one. If the SYN is lost, this backup socket may complete
393 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03394 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00395 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32396 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03397 }
[email protected]6b624c62010-03-14 08:37:32398
[email protected]211d2172009-07-22 15:48:53399 connecting_socket_count_++;
400
[email protected]8159a1c2012-06-07 00:00:10401 group->AddJob(connect_job.release(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02402 } else {
[email protected]06650c52010-06-03 00:49:17403 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10404 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52405 if (!preconnecting) {
406 DCHECK(handle);
407 connect_job->GetAdditionalErrorState(handle);
408 error_socket = connect_job->ReleaseSocket();
409 }
[email protected]e772db3f2010-07-12 18:11:13410 if (error_socket) {
411 HandOutSocket(error_socket, false /* not reused */, handle,
[email protected]aed99ef02010-08-26 14:04:32412 base::TimeDelta(), group, request->net_log());
413 } else if (group->IsEmpty()) {
414 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21415 }
[email protected]2ab05b52009-07-01 23:57:58416 }
[email protected]ff579d42009-06-24 15:47:02417
[email protected]2ab05b52009-07-01 23:57:58418 return rv;
[email protected]ff579d42009-06-24 15:47:02419}
420
[email protected]8159a1c2012-06-07 00:00:10421bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32422 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22423 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
424 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
[email protected]5e6efa52011-06-27 17:26:41425 double max_score = -1;
[email protected]e1b54dc2010-10-06 21:27:22426
427 // Iterate through the idle sockets forwards (oldest to newest)
428 // * Delete any disconnected ones.
429 // * If we find a used idle socket, assign to |idle_socket|. At the end,
430 // the |idle_socket_it| will be set to the newest used idle socket.
431 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
432 it != idle_sockets->end();) {
433 if (!it->socket->IsConnectedAndIdle()) {
434 DecrementIdleCount();
435 delete it->socket;
436 it = idle_sockets->erase(it);
437 continue;
[email protected]eb5a99382010-07-11 03:18:26438 }
[email protected]e1b54dc2010-10-06 21:27:22439
440 if (it->socket->WasEverUsed()) {
441 // We found one we can reuse!
[email protected]5e6efa52011-06-27 17:26:41442 double score = 0;
443 int64 bytes_read = it->socket->NumBytesRead();
444 double num_kb = static_cast<double>(bytes_read) / 1024.0;
445 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds();
446 idle_time_sec = std::max(1, idle_time_sec);
447
448 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) {
449 score = num_kb / pow(idle_time_sec,
450 g_socket_reuse_policy_penalty_exponent);
451 }
452
453 // Equality to prefer recently used connection.
454 if (score >= max_score) {
455 idle_socket_it = it;
456 max_score = score;
457 }
[email protected]e1b54dc2010-10-06 21:27:22458 }
459
460 ++it;
[email protected]eb5a99382010-07-11 03:18:26461 }
[email protected]e1b54dc2010-10-06 21:27:22462
463 // If we haven't found an idle socket, that means there are no used idle
464 // sockets. Pick the oldest (first) idle socket (FIFO).
465
466 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
467 idle_socket_it = idle_sockets->begin();
468
469 if (idle_socket_it != idle_sockets->end()) {
470 DecrementIdleCount();
471 base::TimeDelta idle_time =
472 base::TimeTicks::Now() - idle_socket_it->start_time;
473 IdleSocket idle_socket = *idle_socket_it;
474 idle_sockets->erase(idle_socket_it);
475 HandOutSocket(
476 idle_socket.socket,
477 idle_socket.socket->WasEverUsed(),
478 request->handle(),
479 idle_time,
480 group,
481 request->net_log());
482 return true;
483 }
484
[email protected]eb5a99382010-07-11 03:18:26485 return false;
486}
487
[email protected]06650c52010-06-03 00:49:17488// static
489void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
490 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31491 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
492 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17493}
494
[email protected]d80a4322009-08-14 07:07:49495void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21496 const std::string& group_name, ClientSocketHandle* handle) {
497 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
498 if (callback_it != pending_callback_map_.end()) {
499 int result = callback_it->second.result;
500 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10501 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21502 if (socket) {
503 if (result != OK)
504 socket->Disconnect();
505 ReleaseSocket(handle->group_name(), socket, handle->id());
506 }
507 return;
508 }
[email protected]b6501d3d2010-06-03 23:53:34509
[email protected]ff579d42009-06-24 15:47:02510 CHECK(ContainsKey(group_map_, group_name));
511
[email protected]aed99ef02010-08-26 14:04:32512 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02513
[email protected]ff579d42009-06-24 15:47:02514 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32515 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
516 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49517 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03518 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31519 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
520 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26521
522 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32523 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
524 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26525 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34526 }
[email protected]eb5a99382010-07-11 03:18:26527 break;
[email protected]ff579d42009-06-24 15:47:02528 }
529 }
[email protected]ff579d42009-06-24 15:47:02530}
531
[email protected]2abfe90a2010-08-25 17:49:51532bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
533 return ContainsKey(group_map_, group_name);
534}
535
[email protected]d80a4322009-08-14 07:07:49536void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02537 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14538 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02539}
540
[email protected]d80a4322009-08-14 07:07:49541int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02542 const std::string& group_name) const {
543 GroupMap::const_iterator i = group_map_.find(group_name);
544 CHECK(i != group_map_.end());
545
[email protected]aed99ef02010-08-26 14:04:32546 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02547}
548
[email protected]d80a4322009-08-14 07:07:49549LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02550 const std::string& group_name,
551 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21552 if (ContainsKey(pending_callback_map_, handle))
553 return LOAD_STATE_CONNECTING;
554
[email protected]ff579d42009-06-24 15:47:02555 if (!ContainsKey(group_map_, group_name)) {
556 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
557 << " for handle: " << handle;
558 return LOAD_STATE_IDLE;
559 }
560
561 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32562 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02563
[email protected]ff579d42009-06-24 15:47:02564 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32565 RequestQueue::const_iterator it = group.pending_requests().begin();
566 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49567 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32568 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57569 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32570 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
571 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21572 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57573 }
574 return max_state;
575 } else {
576 // TODO(wtc): Add a state for being on the wait list.
577 // See http://www.crbug.com/5077.
578 return LOAD_STATE_IDLE;
579 }
[email protected]ff579d42009-06-24 15:47:02580 }
581 }
582
583 NOTREACHED();
584 return LOAD_STATE_IDLE;
585}
586
[email protected]ba00b492010-09-08 14:53:38587DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27588 const std::string& name, const std::string& type) const {
589 DictionaryValue* dict = new DictionaryValue();
590 dict->SetString("name", name);
591 dict->SetString("type", type);
592 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
593 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
594 dict->SetInteger("idle_socket_count", idle_socket_count_);
595 dict->SetInteger("max_socket_count", max_sockets_);
596 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
597 dict->SetInteger("pool_generation_number", pool_generation_number_);
598
599 if (group_map_.empty())
600 return dict;
601
602 DictionaryValue* all_groups_dict = new DictionaryValue();
603 for (GroupMap::const_iterator it = group_map_.begin();
604 it != group_map_.end(); it++) {
605 const Group* group = it->second;
606 DictionaryValue* group_dict = new DictionaryValue();
607
608 group_dict->SetInteger("pending_request_count",
609 group->pending_requests().size());
610 if (!group->pending_requests().empty()) {
611 group_dict->SetInteger("top_pending_priority",
612 group->TopPendingPriority());
613 }
614
615 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07616
617 ListValue* idle_socket_list = new ListValue();
[email protected]e1b54dc2010-10-06 21:27:22618 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07619 for (idle_socket = group->idle_sockets().begin();
620 idle_socket != group->idle_sockets().end();
621 idle_socket++) {
622 int source_id = idle_socket->socket->NetLog().source().id;
623 idle_socket_list->Append(Value::CreateIntegerValue(source_id));
624 }
625 group_dict->Set("idle_sockets", idle_socket_list);
626
627 ListValue* connect_jobs_list = new ListValue();
[email protected]2c2bef152010-10-13 00:55:03628 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07629 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
630 int source_id = (*job)->net_log().source().id;
631 connect_jobs_list->Append(Value::CreateIntegerValue(source_id));
632 }
633 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27634
635 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48636 group->IsStalledOnPoolMaxSockets(
637 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00638 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27639
640 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
641 }
642 dict->Set("groups", all_groups_dict);
643 return dict;
644}
645
[email protected]d80a4322009-08-14 07:07:49646bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16647 base::TimeTicks now,
648 base::TimeDelta timeout) const {
649 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01650 if (timed_out)
651 return true;
652 if (socket->WasEverUsed())
653 return !socket->IsConnectedAndIdle();
654 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02655}
656
[email protected]d80a4322009-08-14 07:07:49657void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02658 if (idle_socket_count_ == 0)
659 return;
660
661 // Current time value. Retrieving it once at the function start rather than
662 // inside the inner loop, since it shouldn't change by any meaningful amount.
663 base::TimeTicks now = base::TimeTicks::Now();
664
665 GroupMap::iterator i = group_map_.begin();
666 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32667 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02668
[email protected]e1b54dc2010-10-06 21:27:22669 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32670 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16671 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01672 j->socket->WasEverUsed() ?
673 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16674 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02675 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32676 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02677 DecrementIdleCount();
678 } else {
679 ++j;
680 }
681 }
682
683 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32684 if (group->IsEmpty()) {
685 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02686 } else {
687 ++i;
688 }
689 }
690}
691
[email protected]aed99ef02010-08-26 14:04:32692ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
693 const std::string& group_name) {
694 GroupMap::iterator it = group_map_.find(group_name);
695 if (it != group_map_.end())
696 return it->second;
697 Group* group = new Group;
698 group_map_[group_name] = group;
699 return group;
700}
701
702void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
703 GroupMap::iterator it = group_map_.find(group_name);
704 CHECK(it != group_map_.end());
705
706 RemoveGroup(it);
707}
708
709void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
710 delete it->second;
711 group_map_.erase(it);
712}
713
[email protected]06d94042010-08-25 01:45:22714// static
[email protected]2d6728692011-03-12 01:39:55715bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
716 return g_connect_backup_jobs_enabled;
717}
718
719// static
[email protected]636b8252011-04-08 19:56:54720bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
721 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22722 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54723 return old_value;
[email protected]06d94042010-08-25 01:45:22724}
725
726void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
727 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
728}
729
[email protected]d80a4322009-08-14 07:07:49730void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41731 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
732 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02733}
734
[email protected]d80a4322009-08-14 07:07:49735void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02736 if (--idle_socket_count_ == 0)
737 timer_.Stop();
738}
739
[email protected]64770b7d2011-11-16 04:30:41740// static
741bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
742 return g_cleanup_timer_enabled;
743}
744
745// static
746bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
747 bool old_value = g_cleanup_timer_enabled;
748 g_cleanup_timer_enabled = enabled;
749 return old_value;
750}
751
752void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
753 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
754 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
755}
756
[email protected]eb5a99382010-07-11 03:18:26757void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10758 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26759 int id) {
[email protected]ff579d42009-06-24 15:47:02760 GroupMap::iterator i = group_map_.find(group_name);
761 CHECK(i != group_map_.end());
762
[email protected]aed99ef02010-08-26 14:04:32763 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02764
[email protected]b1f031dd2010-03-02 23:19:33765 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53766 handed_out_socket_count_--;
767
[email protected]aed99ef02010-08-26 14:04:32768 CHECK_GT(group->active_socket_count(), 0);
769 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02770
[email protected]a7e38572010-06-07 18:22:24771 const bool can_reuse = socket->IsConnectedAndIdle() &&
772 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02773 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26774 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01775 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32776 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02777 } else {
778 delete socket;
779 }
[email protected]05ea9ff2010-07-15 19:08:21780
[email protected]eb5a99382010-07-11 03:18:26781 CheckForStalledSocketGroups();
782}
[email protected]ff579d42009-06-24 15:47:02783
[email protected]eb5a99382010-07-11 03:18:26784void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
785 // If we have idle sockets, see if we can give one to the top-stalled group.
786 std::string top_group_name;
787 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21788 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26789 return;
[email protected]4f2abec2010-02-03 18:10:16790
[email protected]eb5a99382010-07-11 03:18:26791 if (ReachedMaxSocketsLimit()) {
792 if (idle_socket_count() > 0) {
793 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54794 } else {
[email protected]eb5a99382010-07-11 03:18:26795 // We can't activate more sockets since we're already at our global
796 // limit.
[email protected]4f2abec2010-02-03 18:10:16797 return;
[email protected]d7027bb2010-05-10 18:58:54798 }
[email protected]4f2abec2010-02-03 18:10:16799 }
[email protected]eb5a99382010-07-11 03:18:26800
801 // Note: we don't loop on waking stalled groups. If the stalled group is at
802 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21803 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26804 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21805 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02806}
807
[email protected]211d2172009-07-22 15:48:53808// Search for the highest priority pending request, amongst the groups that
809// are not at the |max_sockets_per_group_| limit. Note: for requests with
810// the same priority, the winner is based on group hash ordering (and not
811// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48812bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
813 Group** group,
814 std::string* group_name) const {
815 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53816 Group* top_group = NULL;
817 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21818 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48819 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53820 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32821 Group* curr_group = i->second;
822 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53823 if (queue.empty())
824 continue;
[email protected]51fdc7c2012-04-10 19:19:48825 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
826 if (!group)
827 return true;
[email protected]05ea9ff2010-07-15 19:08:21828 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41829 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05830 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41831 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32832 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41833 top_group_name = &i->first;
834 }
[email protected]211d2172009-07-22 15:48:53835 }
836 }
[email protected]05ea9ff2010-07-15 19:08:21837
[email protected]211d2172009-07-22 15:48:53838 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48839 CHECK(group);
[email protected]211d2172009-07-22 15:48:53840 *group = top_group;
841 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48842 } else {
843 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53844 }
[email protected]05ea9ff2010-07-15 19:08:21845 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53846}
847
[email protected]d80a4322009-08-14 07:07:49848void ClientSocketPoolBaseHelper::OnConnectJobComplete(
849 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58850 DCHECK_NE(ERR_IO_PENDING, result);
851 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02852 GroupMap::iterator group_it = group_map_.find(group_name);
853 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32854 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02855
[email protected]3268023f2011-05-05 00:08:10856 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02857
[email protected]9e743cd2010-03-16 07:03:53858 BoundNetLog job_log = job->net_log();
[email protected]5fc08e32009-07-15 17:09:57859
[email protected]4d3b05d2010-01-27 21:27:29860 if (result == OK) {
861 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32862 RemoveConnectJob(job, group);
863 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29864 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02865 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17866 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29867 HandOutSocket(
868 socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32869 base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31870 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21871 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57872 } else {
[email protected]0f873e82010-09-02 16:09:01873 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32874 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21875 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57876 }
[email protected]94c20472010-01-14 08:14:36877 } else {
[email protected]e772db3f2010-07-12 18:11:13878 // If we got a socket, it must contain error information so pass that
879 // up so that the caller can retrieve it.
880 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32881 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29882 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02883 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17884 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18885 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32886 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13887 if (socket.get()) {
888 handed_out_socket = true;
889 HandOutSocket(socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32890 base::TimeDelta(), group, r->net_log());
[email protected]e772db3f2010-07-12 18:11:13891 }
[email protected]d7fd1782011-02-08 19:16:43892 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL,
893 result);
[email protected]05ea9ff2010-07-15 19:08:21894 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18895 } else {
[email protected]aed99ef02010-08-26 14:04:32896 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29897 }
[email protected]05ea9ff2010-07-15 19:08:21898 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32899 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21900 CheckForStalledSocketGroups();
901 }
[email protected]ff579d42009-06-24 15:47:02902 }
[email protected]ff579d42009-06-24 15:47:02903}
904
[email protected]66761b952010-06-25 21:30:38905void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
906 Flush();
907}
908
[email protected]a7e38572010-06-07 18:22:24909void ClientSocketPoolBaseHelper::Flush() {
910 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14911 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52912 CloseIdleSockets();
[email protected]06f92462010-08-31 19:24:14913 AbortAllRequests();
[email protected]a554a8262010-05-20 00:13:52914}
915
[email protected]51fdc7c2012-04-10 19:19:48916bool ClientSocketPoolBaseHelper::IsStalled() const {
917 // If we are not using |max_sockets_|, then clearly we are not stalled
918 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
919 return false;
920 // So in order to be stalled we need to be using |max_sockets_| AND
921 // we need to have a request that is actually stalled on the global
922 // socket limit. To find such a request, we look for a group that
923 // a has more requests that jobs AND where the number of jobs is less
924 // than |max_sockets_per_group_|. (If the number of jobs is equal to
925 // |max_sockets_per_group_|, then the request is stalled on the group,
926 // which does not count.)
927 for (GroupMap::const_iterator it = group_map_.begin();
928 it != group_map_.end(); ++it) {
929 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
930 return true;
931 }
932 return false;
933}
934
[email protected]2c2bef152010-10-13 00:55:03935void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29936 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33937 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53938 connecting_socket_count_--;
939
[email protected]25eea382010-07-10 23:55:26940 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32941 DCHECK(ContainsKey(group->jobs(), job));
942 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26943
944 // If we've got no more jobs for this group, then we no longer need a
945 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32946 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00947 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26948
[email protected]8ae03f42010-07-07 19:08:10949 DCHECK(job);
950 delete job;
[email protected]2ab05b52009-07-01 23:57:58951}
[email protected]ff579d42009-06-24 15:47:02952
[email protected]8ae03f42010-07-07 19:08:10953void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21954 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51955 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21956 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32957 RemoveGroup(group_name);
958 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51959 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10960}
[email protected]06650c52010-06-03 00:49:17961
[email protected]8ae03f42010-07-07 19:08:10962void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21963 const std::string& group_name, Group* group) {
964 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32965 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21966 if (rv != ERR_IO_PENDING) {
967 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02968 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51969 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32970 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10971
[email protected]d7fd1782011-02-08 19:16:43972 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41973 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58974 }
975}
976
[email protected]d80a4322009-08-14 07:07:49977void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10978 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58979 bool reused,
980 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29981 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15982 Group* group,
[email protected]9e743cd2010-03-16 07:03:53983 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58984 DCHECK(socket);
985 handle->set_socket(socket);
986 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29987 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24988 handle->set_pool_id(pool_generation_number_);
[email protected]211d2172009-07-22 15:48:53989
[email protected]d13f51b2010-04-27 23:20:45990 if (reused) {
[email protected]ec11be62010-04-28 19:28:09991 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45992 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:31993 NetLog::IntegerCallback(
994 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:15995 }
[email protected]d13f51b2010-04-27 23:20:45996
[email protected]06650c52010-06-03 00:49:17997 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]3aa4af042012-06-14 21:02:31998 socket->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:15999
[email protected]211d2172009-07-22 15:48:531000 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321001 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021002}
1003
[email protected]d80a4322009-08-14 07:07:491004void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:101005 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:571006 DCHECK(socket);
1007 IdleSocket idle_socket;
1008 idle_socket.socket = socket;
1009 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571010
[email protected]aed99ef02010-08-26 14:04:321011 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571012 IncrementIdleCount();
1013}
1014
[email protected]d80a4322009-08-14 07:07:491015void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541016 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321017 Group* group = i->second;
1018 connecting_socket_count_ -= group->jobs().size();
1019 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321020
[email protected]5fc08e32009-07-15 17:09:571021 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321022 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141023 // RemoveGroup() will call .erase() which will invalidate the iterator,
1024 // but i will already have been incremented to a valid iterator before
1025 // RemoveGroup() is called.
1026 RemoveGroup(i++);
1027 } else {
1028 ++i;
1029 }
1030 }
1031 DCHECK_EQ(0, connecting_socket_count_);
1032}
1033
1034void ClientSocketPoolBaseHelper::AbortAllRequests() {
1035 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1036 Group* group = i->second;
1037
1038 RequestQueue pending_requests;
1039 pending_requests.swap(*group->mutable_pending_requests());
1040 for (RequestQueue::iterator it2 = pending_requests.begin();
1041 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031042 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141043 InvokeUserCallbackLater(
1044 request->handle(), request->callback(), ERR_ABORTED);
1045 }
1046
1047 // Delete group if no longer needed.
1048 if (group->IsEmpty()) {
1049 // RemoveGroup() will call .erase() which will invalidate the iterator,
1050 // but i will already have been incremented to a valid iterator before
1051 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321052 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541053 } else {
1054 ++i;
[email protected]5fc08e32009-07-15 17:09:571055 }
1056 }
1057}
1058
[email protected]d80a4322009-08-14 07:07:491059bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531060 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541061 int total = handed_out_socket_count_ + connecting_socket_count_ +
1062 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201063 // There can be more sockets than the limit since some requests can ignore
1064 // the limit
[email protected]c901f6d2010-04-27 17:48:281065 if (total < max_sockets_)
1066 return false;
[email protected]c901f6d2010-04-27 17:48:281067 return true;
[email protected]211d2172009-07-22 15:48:531068}
1069
[email protected]51fdc7c2012-04-10 19:19:481070bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1071 if (idle_socket_count() == 0)
1072 return false;
1073 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461074}
1075
1076bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1077 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541078 CHECK_GT(idle_socket_count(), 0);
1079
1080 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321081 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461082 if (exception_group == group)
1083 continue;
[email protected]e1b54dc2010-10-06 21:27:221084 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541085
[email protected]e1b54dc2010-10-06 21:27:221086 if (!idle_sockets->empty()) {
1087 delete idle_sockets->front().socket;
1088 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541089 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321090 if (group->IsEmpty())
1091 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541092
[email protected]dcbe168a2010-12-02 03:14:461093 return true;
[email protected]43a21b82010-06-10 21:30:541094 }
1095 }
1096
[email protected]51fdc7c2012-04-10 19:19:481097 return false;
1098}
[email protected]dcbe168a2010-12-02 03:14:461099
[email protected]51fdc7c2012-04-10 19:19:481100bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1101 // This pool doesn't have any idle sockets. It's possible that a pool at a
1102 // higher layer is holding one of this sockets active, but it's actually idle.
1103 // Query the higher layers.
1104 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1105 it != higher_layer_pools_.end(); ++it) {
1106 if ((*it)->CloseOneIdleConnection())
1107 return true;
1108 }
[email protected]dcbe168a2010-12-02 03:14:461109 return false;
[email protected]43a21b82010-06-10 21:30:541110}
1111
[email protected]05ea9ff2010-07-15 19:08:211112void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411113 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211114 CHECK(!ContainsKey(pending_callback_map_, handle));
1115 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1116 MessageLoop::current()->PostTask(
1117 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411118 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1119 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211120}
1121
1122void ClientSocketPoolBaseHelper::InvokeUserCallback(
1123 ClientSocketHandle* handle) {
1124 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1125
1126 // Exit if the request has already been cancelled.
1127 if (it == pending_callback_map_.end())
1128 return;
1129
1130 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411131 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211132 int result = it->second.result;
1133 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411134 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211135}
1136
[email protected]aed99ef02010-08-26 14:04:321137ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101138 : unassigned_job_count_(0),
1139 active_socket_count_(0),
[email protected]96b939a2012-01-20 21:52:151140 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
[email protected]aed99ef02010-08-26 14:04:321141
[email protected]e4d9a9722011-05-12 00:16:001142ClientSocketPoolBaseHelper::Group::~Group() {
1143 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101144 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321145}
1146
1147void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1148 const std::string& group_name,
1149 ClientSocketPoolBaseHelper* pool) {
1150 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411151 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321152 return;
1153
1154 MessageLoop::current()->PostDelayedTask(
1155 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411156 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1157 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001158 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321159}
1160
[email protected]8159a1c2012-06-07 00:00:101161bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1162 SanityCheck();
1163
1164 if (unassigned_job_count_ == 0)
1165 return false;
1166 --unassigned_job_count_;
1167 return true;
1168}
1169
1170void ClientSocketPoolBaseHelper::Group::AddJob(ConnectJob* job,
1171 bool is_preconnect) {
1172 SanityCheck();
1173
1174 if (is_preconnect)
1175 ++unassigned_job_count_;
1176 jobs_.insert(job);
1177}
1178
1179void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
1180 SanityCheck();
1181
1182 jobs_.erase(job);
1183 size_t job_count = jobs_.size();
1184 if (job_count < unassigned_job_count_)
1185 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031186}
1187
[email protected]aed99ef02010-08-26 14:04:321188void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1189 std::string group_name,
1190 ClientSocketPoolBaseHelper* pool) {
1191 // If there are no more jobs pending, there is no work to do.
1192 // If we've done our cleanups correctly, this should not happen.
1193 if (jobs_.empty()) {
1194 NOTREACHED();
1195 return;
1196 }
1197
[email protected]8159a1c2012-06-07 00:00:101198 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321199 // right now due to limits, just reset the timer.
1200 if (pool->ReachedMaxSocketsLimit() ||
1201 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1202 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1203 StartBackupSocketTimer(group_name, pool);
1204 return;
1205 }
1206
[email protected]a9fc8fc2011-05-10 02:41:071207 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441208 return;
[email protected]4baaf9d2010-08-31 15:15:441209
[email protected]aed99ef02010-08-26 14:04:321210 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1211 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311212 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321213 SIMPLE_STATS_COUNTER("socket.backup_created");
1214 int rv = backup_job->Connect();
1215 pool->connecting_socket_count_++;
[email protected]8159a1c2012-06-07 00:00:101216 AddJob(backup_job, false);
[email protected]aed99ef02010-08-26 14:04:321217 if (rv != ERR_IO_PENDING)
1218 pool->OnConnectJobComplete(rv, backup_job);
1219}
1220
[email protected]8159a1c2012-06-07 00:00:101221void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1222 DCHECK_LE(unassigned_job_count_, jobs_.size());
1223}
1224
[email protected]aed99ef02010-08-26 14:04:321225void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101226 SanityCheck();
1227
[email protected]aed99ef02010-08-26 14:04:321228 // Delete active jobs.
1229 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101230 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321231
1232 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411233 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321234}
1235
[email protected]d80a4322009-08-14 07:07:491236} // namespace internal
1237
[email protected]ff579d42009-06-24 15:47:021238} // namespace net