blob: 028d606ed62b7f01b6a626bd36a4a261b9394283 [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
7#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:158#include "base/format_macros.h"
[email protected]5e6efa52011-06-27 17:26:419#include "base/logging.h"
[email protected]ff579d42009-06-24 15:47:0210#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/stats_counters.h"
[email protected]7286e3fc2011-07-19 22:13:2412#include "base/stl_util.h"
[email protected]fc9be5802013-06-11 10:56:5113#include "base/strings/string_util.h"
[email protected]ff579d42009-06-24 15:47:0214#include "base/time.h"
[email protected]9349cfb2010-08-31 18:00:5315#include "base/values.h"
[email protected]9e743cd2010-03-16 07:03:5316#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0217#include "net/base/net_errors.h"
18#include "net/socket/client_socket_handle.h"
19
20using base::TimeDelta;
21
22namespace {
23
[email protected]64770b7d2011-11-16 04:30:4124// Indicate whether we should enable idle socket cleanup timer. When timer is
25// disabled, sockets are closed next time a socket request is made.
26bool g_cleanup_timer_enabled = true;
27
[email protected]ff579d42009-06-24 15:47:0228// The timeout value, in seconds, used to clean up idle sockets that can't be
29// reused.
30//
31// Note: It's important to close idle sockets that have received data as soon
32// as possible because the received data may cause BSOD on Windows XP under
33// some conditions. See http://crbug.com/4606.
34const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
35
[email protected]c847c2a2011-04-08 13:56:1436// Indicate whether or not we should establish a new transport layer connection
37// after a certain timeout has passed without receiving an ACK.
[email protected]06d94042010-08-25 01:45:2238bool g_connect_backup_jobs_enabled = true;
39
[email protected]ff579d42009-06-24 15:47:0240} // namespace
41
42namespace net {
43
[email protected]2ab05b52009-07-01 23:57:5844ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3445 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3046 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5347 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5848 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3449 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5850 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0251 net_log_(net_log),
[email protected]8159a1c2012-06-07 00:00:1052 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5853 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5854 DCHECK(delegate);
[email protected]e9d7d252013-01-04 02:33:1755 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB,
56 NetLog::StringCallback("group_name", &group_name_));
[email protected]2ab05b52009-07-01 23:57:5857}
58
[email protected]fd7b7c92009-08-20 19:38:3059ConnectJob::~ConnectJob() {
[email protected]3aa4af042012-06-14 21:02:3160 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]fd7b7c92009-08-20 19:38:3061}
[email protected]2ab05b52009-07-01 23:57:5862
[email protected]974ebd62009-08-03 23:14:3463int ConnectJob::Connect() {
64 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0265 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3066
[email protected]a2006ece2010-04-23 16:44:0267 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3068
[email protected]06650c52010-06-03 00:49:1769 LogConnectStart();
70
[email protected]fd7b7c92009-08-20 19:38:3071 int rv = ConnectInternal();
72
73 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:1774 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3075 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:3076 }
77
78 return rv;
79}
80
[email protected]3268023f2011-05-05 00:08:1081void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:1782 if (socket) {
[email protected]3aa4af042012-06-14 21:02:3183 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
84 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:1785 }
86 socket_.reset(socket);
87}
88
[email protected]fd7b7c92009-08-20 19:38:3089void ConnectJob::NotifyDelegateOfCompletion(int rv) {
90 // The delegate will delete |this|.
[email protected]10833352013-02-12 19:39:5091 Delegate* delegate = delegate_;
[email protected]fd7b7c92009-08-20 19:38:3092 delegate_ = NULL;
93
[email protected]06650c52010-06-03 00:49:1794 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3095 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:3496}
97
[email protected]a796bcec2010-03-22 17:17:2698void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
99 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02100 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26101}
102
[email protected]06650c52010-06-03 00:49:17103void ConnectJob::LogConnectStart() {
[email protected]034df0f32013-01-07 23:17:48104 connect_timing_.connect_start = base::TimeTicks::Now();
[email protected]e9d7d252013-01-04 02:33:17105 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT);
[email protected]06650c52010-06-03 00:49:17106}
107
108void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]034df0f32013-01-07 23:17:48109 connect_timing_.connect_end = base::TimeTicks::Now();
[email protected]d7fd1782011-02-08 19:16:43110 net_log().EndEventWithNetErrorCode(
111 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17112}
113
[email protected]974ebd62009-08-03 23:14:34114void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40115 // Make sure the socket is NULL before calling into |delegate|.
116 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30117
[email protected]3aa4af042012-06-14 21:02:31118 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30119
120 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34121}
122
[email protected]d80a4322009-08-14 07:07:49123namespace internal {
124
[email protected]fd4fe0b2010-02-08 23:02:15125ClientSocketPoolBaseHelper::Request::Request(
126 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41127 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15128 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20129 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03130 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53131 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13132 : handle_(handle),
133 callback_(callback),
134 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20135 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03136 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53137 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15138
139ClientSocketPoolBaseHelper::Request::~Request() {}
140
[email protected]d80a4322009-08-14 07:07:49141ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53142 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02143 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16144 base::TimeDelta unused_idle_socket_timeout,
145 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38146 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02147 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53148 connecting_socket_count_(0),
149 handed_out_socket_count_(0),
150 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02151 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41152 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16153 unused_idle_socket_timeout_(unused_idle_socket_timeout),
154 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35155 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22156 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13157 pool_generation_number_(0),
[email protected]aa249b52013-04-30 01:04:32158 weak_factory_(this) {
[email protected]211d2172009-07-22 15:48:53159 DCHECK_LE(0, max_sockets_per_group);
160 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52161
[email protected]232a5812011-03-04 22:42:08162 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53163}
[email protected]ff579d42009-06-24 15:47:02164
[email protected]d80a4322009-08-14 07:07:49165ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13166 // Clean up any idle sockets and pending connect jobs. Assert that we have no
167 // remaining active sockets or pending requests. They should have all been
168 // cleaned up prior to |this| being destroyed.
[email protected]7af985a2012-12-14 22:40:42169 FlushWithError(ERR_ABORTED);
[email protected]2431756e2010-09-29 20:26:13170 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21171 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29172 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48173 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52174
[email protected]232a5812011-03-04 22:42:08175 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02176}
177
[email protected]2a848e0e2012-08-09 22:27:31178ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
179 : result(OK) {
180}
181
182ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
183 const CompletionCallback& callback_in, int result_in)
184 : callback(callback_in),
185 result(result_in) {
186}
187
[email protected]49639fa2011-12-20 23:22:41188ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
189
[email protected]ff579d42009-06-24 15:47:02190// InsertRequestIntoQueue inserts the request into the queue based on
191// priority. Highest priorities are closest to the front. Older requests are
192// prioritized over requests of equal priority.
193//
194// static
[email protected]d80a4322009-08-14 07:07:49195void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
196 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02197 RequestQueue::iterator it = pending_requests->begin();
[email protected]31ae7ab2012-04-24 21:09:05198 while (it != pending_requests->end() && r->priority() <= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02199 ++it;
200 pending_requests->insert(it, r);
201}
202
[email protected]fd7b7c92009-08-20 19:38:30203// static
204const ClientSocketPoolBaseHelper::Request*
205ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05206 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30207 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02208 group->mutable_pending_requests()->erase(it);
209 // If there are no more requests, we kill the backup timer.
210 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00211 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30212 return req;
213}
214
[email protected]51fdc7c2012-04-10 19:19:48215void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
216 CHECK(pool);
217 CHECK(!ContainsKey(higher_layer_pools_, pool));
218 higher_layer_pools_.insert(pool);
219}
220
221void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
222 CHECK(pool);
223 CHECK(ContainsKey(higher_layer_pools_, pool));
224 higher_layer_pools_.erase(pool);
225}
226
[email protected]d80a4322009-08-14 07:07:49227int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02228 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49229 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41230 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03231 CHECK(request->handle());
232
[email protected]64770b7d2011-11-16 04:30:41233 // Cleanup any timed-out idle sockets if no timer is used.
234 if (!use_cleanup_timer_)
235 CleanupIdleSockets(false);
236
[email protected]3aa4af042012-06-14 21:02:31237 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32238 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26239
[email protected]fd4fe0b2010-02-08 23:02:15240 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17241 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43242 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21243 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17244 delete request;
245 } else {
[email protected]aed99ef02010-08-26 14:04:32246 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]58e562f2013-04-22 17:32:20247 // Have to do this asynchronously, as closing sockets in higher level pools
248 // call back in to |this|, which will cause all sorts of fun and exciting
249 // re-entrancy issues if the socket pool is doing something else at the
250 // time.
251 if (group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
[email protected]2da659e2013-05-23 20:51:34252 base::MessageLoop::current()->PostTask(
[email protected]58e562f2013-04-22 17:32:20253 FROM_HERE,
254 base::Bind(
255 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
256 weak_factory_.GetWeakPtr()));
257 }
[email protected]e7e99322010-05-04 23:30:17258 }
[email protected]fd4fe0b2010-02-08 23:02:15259 return rv;
260}
261
[email protected]2c2bef152010-10-13 00:55:03262void ClientSocketPoolBaseHelper::RequestSockets(
263 const std::string& group_name,
264 const Request& request,
265 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41266 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03267 DCHECK(!request.handle());
268
[email protected]64770b7d2011-11-16 04:30:41269 // Cleanup any timed out idle sockets if no timer is used.
270 if (!use_cleanup_timer_)
271 CleanupIdleSockets(false);
272
[email protected]2c2bef152010-10-13 00:55:03273 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03274 num_sockets = max_sockets_per_group_;
275 }
276
277 request.net_log().BeginEvent(
278 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31279 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03280
281 Group* group = GetOrCreateGroup(group_name);
282
[email protected]3c819f522010-12-02 02:03:12283 // RequestSocketsInternal() may delete the group.
284 bool deleted_group = false;
285
[email protected]d7fd1782011-02-08 19:16:43286 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03287 for (int num_iterations_left = num_sockets;
288 group->NumActiveSocketSlots() < num_sockets &&
289 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43290 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03291 if (rv < 0 && rv != ERR_IO_PENDING) {
292 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12293 if (!ContainsKey(group_map_, group_name))
294 deleted_group = true;
295 break;
296 }
297 if (!ContainsKey(group_map_, group_name)) {
298 // Unexpected. The group should only be getting deleted on synchronous
299 // error.
300 NOTREACHED();
301 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03302 break;
303 }
304 }
305
[email protected]3c819f522010-12-02 02:03:12306 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03307 RemoveGroup(group_name);
308
[email protected]d7fd1782011-02-08 19:16:43309 if (rv == ERR_IO_PENDING)
310 rv = OK;
311 request.net_log().EndEventWithNetErrorCode(
312 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03313}
314
[email protected]fd4fe0b2010-02-08 23:02:15315int ClientSocketPoolBaseHelper::RequestSocketInternal(
316 const std::string& group_name,
317 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49318 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03319 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32320 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02321
[email protected]2c2bef152010-10-13 00:55:03322 if (!(request->flags() & NO_IDLE_SOCKETS)) {
323 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10324 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03325 return OK;
326 }
327
[email protected]8159a1c2012-06-07 00:00:10328 // If there are more ConnectJobs than pending requests, don't need to do
329 // anything. Can just wait for the extra job to connect, and then assign it
330 // to the request.
331 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03332 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10333
[email protected]43a21b82010-06-10 21:30:54334 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20335 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
336 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48337 // TODO(willchan): Consider whether or not we need to close a socket in a
338 // higher layered group. I don't think this makes sense since we would just
339 // reuse that socket then if we needed one and wouldn't make it down to this
340 // layer.
[email protected]43a21b82010-06-10 21:30:54341 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31342 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54343 return ERR_IO_PENDING;
344 }
345
[email protected]5acdce12011-03-30 13:00:20346 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48347 // NOTE(mmenke): Wonder if we really need different code for each case
348 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54349 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48350 // There's an idle socket in this pool. Either that's because there's
351 // still one in this group, but we got here due to preconnecting bypassing
352 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46353 bool closed = CloseOneIdleSocketExceptInGroup(group);
354 if (preconnecting && !closed)
355 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54356 } else {
[email protected]40dee91a2012-04-17 19:25:46357 // We could check if we really have a stalled group here, but it requires
358 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31359 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46360 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54361 }
362 }
363
[email protected]51fdc7c2012-04-10 19:19:48364 // We couldn't find a socket to reuse, and there's space to allocate one,
365 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58366 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17367 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02368
[email protected]2ab05b52009-07-01 23:57:58369 int rv = connect_job->Connect();
370 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17371 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03372 if (!preconnecting) {
373 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48374 connect_job->connect_timing(), handle, base::TimeDelta(),
375 group, request->net_log());
[email protected]2c2bef152010-10-13 00:55:03376 } else {
377 AddIdleSocket(connect_job->ReleaseSocket(), group);
378 }
[email protected]2ab05b52009-07-01 23:57:58379 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32380 // If we don't have any sockets in this group, set a timer for potentially
381 // creating a new one. If the SYN is lost, this backup socket may complete
382 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03383 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00384 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32385 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03386 }
[email protected]6b624c62010-03-14 08:37:32387
[email protected]211d2172009-07-22 15:48:53388 connecting_socket_count_++;
389
[email protected]8159a1c2012-06-07 00:00:10390 group->AddJob(connect_job.release(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02391 } else {
[email protected]06650c52010-06-03 00:49:17392 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10393 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52394 if (!preconnecting) {
395 DCHECK(handle);
396 connect_job->GetAdditionalErrorState(handle);
397 error_socket = connect_job->ReleaseSocket();
398 }
[email protected]e772db3f2010-07-12 18:11:13399 if (error_socket) {
[email protected]034df0f32013-01-07 23:17:48400 HandOutSocket(error_socket, false /* not reused */,
401 connect_job->connect_timing(), handle, base::TimeDelta(),
402 group, request->net_log());
[email protected]aed99ef02010-08-26 14:04:32403 } else if (group->IsEmpty()) {
404 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21405 }
[email protected]2ab05b52009-07-01 23:57:58406 }
[email protected]ff579d42009-06-24 15:47:02407
[email protected]2ab05b52009-07-01 23:57:58408 return rv;
[email protected]ff579d42009-06-24 15:47:02409}
410
[email protected]8159a1c2012-06-07 00:00:10411bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32412 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22413 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
414 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
415
416 // Iterate through the idle sockets forwards (oldest to newest)
417 // * Delete any disconnected ones.
418 // * If we find a used idle socket, assign to |idle_socket|. At the end,
419 // the |idle_socket_it| will be set to the newest used idle socket.
420 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
421 it != idle_sockets->end();) {
422 if (!it->socket->IsConnectedAndIdle()) {
423 DecrementIdleCount();
424 delete it->socket;
425 it = idle_sockets->erase(it);
426 continue;
[email protected]eb5a99382010-07-11 03:18:26427 }
[email protected]e1b54dc2010-10-06 21:27:22428
429 if (it->socket->WasEverUsed()) {
430 // We found one we can reuse!
[email protected]e86df8dc2013-03-30 13:18:28431 idle_socket_it = it;
[email protected]e1b54dc2010-10-06 21:27:22432 }
433
434 ++it;
[email protected]eb5a99382010-07-11 03:18:26435 }
[email protected]e1b54dc2010-10-06 21:27:22436
437 // If we haven't found an idle socket, that means there are no used idle
438 // sockets. Pick the oldest (first) idle socket (FIFO).
439
440 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
441 idle_socket_it = idle_sockets->begin();
442
443 if (idle_socket_it != idle_sockets->end()) {
444 DecrementIdleCount();
445 base::TimeDelta idle_time =
446 base::TimeTicks::Now() - idle_socket_it->start_time;
447 IdleSocket idle_socket = *idle_socket_it;
448 idle_sockets->erase(idle_socket_it);
449 HandOutSocket(
450 idle_socket.socket,
451 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48452 LoadTimingInfo::ConnectTiming(),
[email protected]e1b54dc2010-10-06 21:27:22453 request->handle(),
454 idle_time,
455 group,
456 request->net_log());
457 return true;
458 }
459
[email protected]eb5a99382010-07-11 03:18:26460 return false;
461}
462
[email protected]06650c52010-06-03 00:49:17463// static
464void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
465 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31466 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
467 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17468}
469
[email protected]d80a4322009-08-14 07:07:49470void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21471 const std::string& group_name, ClientSocketHandle* handle) {
472 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
473 if (callback_it != pending_callback_map_.end()) {
474 int result = callback_it->second.result;
475 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10476 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21477 if (socket) {
478 if (result != OK)
479 socket->Disconnect();
480 ReleaseSocket(handle->group_name(), socket, handle->id());
481 }
482 return;
483 }
[email protected]b6501d3d2010-06-03 23:53:34484
[email protected]ff579d42009-06-24 15:47:02485 CHECK(ContainsKey(group_map_, group_name));
486
[email protected]aed99ef02010-08-26 14:04:32487 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02488
[email protected]ff579d42009-06-24 15:47:02489 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32490 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
491 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49492 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03493 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31494 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
495 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26496
497 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32498 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
499 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26500 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34501 }
[email protected]eb5a99382010-07-11 03:18:26502 break;
[email protected]ff579d42009-06-24 15:47:02503 }
504 }
[email protected]ff579d42009-06-24 15:47:02505}
506
[email protected]2abfe90a2010-08-25 17:49:51507bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
508 return ContainsKey(group_map_, group_name);
509}
510
[email protected]d80a4322009-08-14 07:07:49511void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02512 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14513 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02514}
515
[email protected]d80a4322009-08-14 07:07:49516int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02517 const std::string& group_name) const {
518 GroupMap::const_iterator i = group_map_.find(group_name);
519 CHECK(i != group_map_.end());
520
[email protected]aed99ef02010-08-26 14:04:32521 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02522}
523
[email protected]d80a4322009-08-14 07:07:49524LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02525 const std::string& group_name,
526 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21527 if (ContainsKey(pending_callback_map_, handle))
528 return LOAD_STATE_CONNECTING;
529
[email protected]ff579d42009-06-24 15:47:02530 if (!ContainsKey(group_map_, group_name)) {
531 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
532 << " for handle: " << handle;
533 return LOAD_STATE_IDLE;
534 }
535
536 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32537 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02538
[email protected]ff579d42009-06-24 15:47:02539 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32540 RequestQueue::const_iterator it = group.pending_requests().begin();
541 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49542 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32543 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57544 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32545 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
546 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21547 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57548 }
549 return max_state;
550 } else {
551 // TODO(wtc): Add a state for being on the wait list.
[email protected]dba9bd22012-12-06 23:04:03552 // See http://crbug.com/5077.
[email protected]5fc08e32009-07-15 17:09:57553 return LOAD_STATE_IDLE;
554 }
[email protected]ff579d42009-06-24 15:47:02555 }
556 }
557
558 NOTREACHED();
559 return LOAD_STATE_IDLE;
560}
561
[email protected]10833352013-02-12 19:39:50562base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27563 const std::string& name, const std::string& type) const {
[email protected]10833352013-02-12 19:39:50564 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27565 dict->SetString("name", name);
566 dict->SetString("type", type);
567 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
568 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
569 dict->SetInteger("idle_socket_count", idle_socket_count_);
570 dict->SetInteger("max_socket_count", max_sockets_);
571 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
572 dict->SetInteger("pool_generation_number", pool_generation_number_);
573
574 if (group_map_.empty())
575 return dict;
576
[email protected]10833352013-02-12 19:39:50577 base::DictionaryValue* all_groups_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27578 for (GroupMap::const_iterator it = group_map_.begin();
579 it != group_map_.end(); it++) {
580 const Group* group = it->second;
[email protected]10833352013-02-12 19:39:50581 base::DictionaryValue* group_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27582
583 group_dict->SetInteger("pending_request_count",
584 group->pending_requests().size());
585 if (!group->pending_requests().empty()) {
586 group_dict->SetInteger("top_pending_priority",
587 group->TopPendingPriority());
588 }
589
590 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07591
[email protected]10833352013-02-12 19:39:50592 base::ListValue* idle_socket_list = new base::ListValue();
[email protected]e1b54dc2010-10-06 21:27:22593 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07594 for (idle_socket = group->idle_sockets().begin();
595 idle_socket != group->idle_sockets().end();
596 idle_socket++) {
597 int source_id = idle_socket->socket->NetLog().source().id;
[email protected]10833352013-02-12 19:39:50598 idle_socket_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07599 }
600 group_dict->Set("idle_sockets", idle_socket_list);
601
[email protected]10833352013-02-12 19:39:50602 base::ListValue* connect_jobs_list = new base::ListValue();
[email protected]2c2bef152010-10-13 00:55:03603 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07604 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
605 int source_id = (*job)->net_log().source().id;
[email protected]10833352013-02-12 19:39:50606 connect_jobs_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07607 }
608 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27609
610 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48611 group->IsStalledOnPoolMaxSockets(
612 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00613 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27614
615 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
616 }
617 dict->Set("groups", all_groups_dict);
618 return dict;
619}
620
[email protected]d80a4322009-08-14 07:07:49621bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16622 base::TimeTicks now,
623 base::TimeDelta timeout) const {
624 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01625 if (timed_out)
626 return true;
627 if (socket->WasEverUsed())
628 return !socket->IsConnectedAndIdle();
629 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02630}
631
[email protected]d80a4322009-08-14 07:07:49632void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02633 if (idle_socket_count_ == 0)
634 return;
635
636 // Current time value. Retrieving it once at the function start rather than
637 // inside the inner loop, since it shouldn't change by any meaningful amount.
638 base::TimeTicks now = base::TimeTicks::Now();
639
640 GroupMap::iterator i = group_map_.begin();
641 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32642 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02643
[email protected]e1b54dc2010-10-06 21:27:22644 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32645 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16646 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01647 j->socket->WasEverUsed() ?
648 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16649 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02650 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32651 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02652 DecrementIdleCount();
653 } else {
654 ++j;
655 }
656 }
657
658 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32659 if (group->IsEmpty()) {
660 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02661 } else {
662 ++i;
663 }
664 }
665}
666
[email protected]aed99ef02010-08-26 14:04:32667ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
668 const std::string& group_name) {
669 GroupMap::iterator it = group_map_.find(group_name);
670 if (it != group_map_.end())
671 return it->second;
672 Group* group = new Group;
673 group_map_[group_name] = group;
674 return group;
675}
676
677void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
678 GroupMap::iterator it = group_map_.find(group_name);
679 CHECK(it != group_map_.end());
680
681 RemoveGroup(it);
682}
683
684void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
685 delete it->second;
686 group_map_.erase(it);
687}
688
[email protected]06d94042010-08-25 01:45:22689// static
[email protected]2d6728692011-03-12 01:39:55690bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
691 return g_connect_backup_jobs_enabled;
692}
693
694// static
[email protected]636b8252011-04-08 19:56:54695bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
696 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22697 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54698 return old_value;
[email protected]06d94042010-08-25 01:45:22699}
700
701void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
702 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
703}
704
[email protected]d80a4322009-08-14 07:07:49705void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41706 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
707 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02708}
709
[email protected]d80a4322009-08-14 07:07:49710void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02711 if (--idle_socket_count_ == 0)
712 timer_.Stop();
713}
714
[email protected]64770b7d2011-11-16 04:30:41715// static
716bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
717 return g_cleanup_timer_enabled;
718}
719
720// static
721bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
722 bool old_value = g_cleanup_timer_enabled;
723 g_cleanup_timer_enabled = enabled;
724 return old_value;
725}
726
727void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
728 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
729 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
730}
731
[email protected]eb5a99382010-07-11 03:18:26732void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10733 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26734 int id) {
[email protected]ff579d42009-06-24 15:47:02735 GroupMap::iterator i = group_map_.find(group_name);
736 CHECK(i != group_map_.end());
737
[email protected]aed99ef02010-08-26 14:04:32738 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02739
[email protected]b1f031dd2010-03-02 23:19:33740 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53741 handed_out_socket_count_--;
742
[email protected]aed99ef02010-08-26 14:04:32743 CHECK_GT(group->active_socket_count(), 0);
744 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02745
[email protected]a7e38572010-06-07 18:22:24746 const bool can_reuse = socket->IsConnectedAndIdle() &&
747 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02748 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26749 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01750 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32751 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02752 } else {
753 delete socket;
754 }
[email protected]05ea9ff2010-07-15 19:08:21755
[email protected]eb5a99382010-07-11 03:18:26756 CheckForStalledSocketGroups();
757}
[email protected]ff579d42009-06-24 15:47:02758
[email protected]eb5a99382010-07-11 03:18:26759void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
760 // If we have idle sockets, see if we can give one to the top-stalled group.
761 std::string top_group_name;
762 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21763 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26764 return;
[email protected]4f2abec2010-02-03 18:10:16765
[email protected]eb5a99382010-07-11 03:18:26766 if (ReachedMaxSocketsLimit()) {
767 if (idle_socket_count() > 0) {
768 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54769 } else {
[email protected]eb5a99382010-07-11 03:18:26770 // We can't activate more sockets since we're already at our global
771 // limit.
[email protected]4f2abec2010-02-03 18:10:16772 return;
[email protected]d7027bb2010-05-10 18:58:54773 }
[email protected]4f2abec2010-02-03 18:10:16774 }
[email protected]eb5a99382010-07-11 03:18:26775
776 // Note: we don't loop on waking stalled groups. If the stalled group is at
777 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21778 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26779 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21780 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02781}
782
[email protected]211d2172009-07-22 15:48:53783// Search for the highest priority pending request, amongst the groups that
784// are not at the |max_sockets_per_group_| limit. Note: for requests with
785// the same priority, the winner is based on group hash ordering (and not
786// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48787bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
788 Group** group,
789 std::string* group_name) const {
790 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53791 Group* top_group = NULL;
792 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21793 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48794 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53795 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32796 Group* curr_group = i->second;
797 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53798 if (queue.empty())
799 continue;
[email protected]51fdc7c2012-04-10 19:19:48800 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
801 if (!group)
802 return true;
[email protected]05ea9ff2010-07-15 19:08:21803 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41804 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05805 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41806 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32807 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41808 top_group_name = &i->first;
809 }
[email protected]211d2172009-07-22 15:48:53810 }
811 }
[email protected]05ea9ff2010-07-15 19:08:21812
[email protected]211d2172009-07-22 15:48:53813 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48814 CHECK(group);
[email protected]211d2172009-07-22 15:48:53815 *group = top_group;
816 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48817 } else {
818 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53819 }
[email protected]05ea9ff2010-07-15 19:08:21820 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53821}
822
[email protected]d80a4322009-08-14 07:07:49823void ClientSocketPoolBaseHelper::OnConnectJobComplete(
824 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58825 DCHECK_NE(ERR_IO_PENDING, result);
826 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02827 GroupMap::iterator group_it = group_map_.find(group_name);
828 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32829 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02830
[email protected]3268023f2011-05-05 00:08:10831 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02832
[email protected]034df0f32013-01-07 23:17:48833 // Copies of these are needed because |job| may be deleted before they are
834 // accessed.
[email protected]9e743cd2010-03-16 07:03:53835 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48836 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57837
[email protected]4d3b05d2010-01-27 21:27:29838 if (result == OK) {
839 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32840 RemoveConnectJob(job, group);
841 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29842 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02843 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17844 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29845 HandOutSocket(
[email protected]034df0f32013-01-07 23:17:48846 socket.release(), false /* unused socket */, connect_timing,
847 r->handle(), base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31848 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21849 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57850 } else {
[email protected]0f873e82010-09-02 16:09:01851 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32852 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21853 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57854 }
[email protected]94c20472010-01-14 08:14:36855 } else {
[email protected]e772db3f2010-07-12 18:11:13856 // If we got a socket, it must contain error information so pass that
857 // up so that the caller can retrieve it.
858 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32859 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29860 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02861 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17862 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18863 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32864 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13865 if (socket.get()) {
866 handed_out_socket = true;
[email protected]034df0f32013-01-07 23:17:48867 HandOutSocket(socket.release(), false /* unused socket */,
868 connect_timing, r->handle(), base::TimeDelta(), group,
869 r->net_log());
[email protected]e772db3f2010-07-12 18:11:13870 }
[email protected]034df0f32013-01-07 23:17:48871 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result);
[email protected]05ea9ff2010-07-15 19:08:21872 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18873 } else {
[email protected]aed99ef02010-08-26 14:04:32874 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29875 }
[email protected]05ea9ff2010-07-15 19:08:21876 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32877 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21878 CheckForStalledSocketGroups();
879 }
[email protected]ff579d42009-06-24 15:47:02880 }
[email protected]ff579d42009-06-24 15:47:02881}
882
[email protected]66761b952010-06-25 21:30:38883void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42884 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38885}
886
[email protected]7af985a2012-12-14 22:40:42887void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24888 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14889 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52890 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42891 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52892}
893
[email protected]51fdc7c2012-04-10 19:19:48894bool ClientSocketPoolBaseHelper::IsStalled() const {
895 // If we are not using |max_sockets_|, then clearly we are not stalled
896 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
897 return false;
898 // So in order to be stalled we need to be using |max_sockets_| AND
899 // we need to have a request that is actually stalled on the global
900 // socket limit. To find such a request, we look for a group that
901 // a has more requests that jobs AND where the number of jobs is less
902 // than |max_sockets_per_group_|. (If the number of jobs is equal to
903 // |max_sockets_per_group_|, then the request is stalled on the group,
904 // which does not count.)
905 for (GroupMap::const_iterator it = group_map_.begin();
906 it != group_map_.end(); ++it) {
907 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
908 return true;
909 }
910 return false;
911}
912
[email protected]2c2bef152010-10-13 00:55:03913void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29914 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33915 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53916 connecting_socket_count_--;
917
[email protected]25eea382010-07-10 23:55:26918 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32919 DCHECK(ContainsKey(group->jobs(), job));
920 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26921
922 // If we've got no more jobs for this group, then we no longer need a
923 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32924 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00925 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26926
[email protected]8ae03f42010-07-07 19:08:10927 DCHECK(job);
928 delete job;
[email protected]2ab05b52009-07-01 23:57:58929}
[email protected]ff579d42009-06-24 15:47:02930
[email protected]8ae03f42010-07-07 19:08:10931void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21932 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51933 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21934 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32935 RemoveGroup(group_name);
936 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51937 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10938}
[email protected]06650c52010-06-03 00:49:17939
[email protected]8ae03f42010-07-07 19:08:10940void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21941 const std::string& group_name, Group* group) {
942 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32943 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21944 if (rv != ERR_IO_PENDING) {
945 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02946 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51947 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32948 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10949
[email protected]d7fd1782011-02-08 19:16:43950 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41951 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58952 }
953}
954
[email protected]d80a4322009-08-14 07:07:49955void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10956 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58957 bool reused,
[email protected]034df0f32013-01-07 23:17:48958 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:58959 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29960 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15961 Group* group,
[email protected]9e743cd2010-03-16 07:03:53962 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58963 DCHECK(socket);
964 handle->set_socket(socket);
965 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29966 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24967 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:48968 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:53969
[email protected]d13f51b2010-04-27 23:20:45970 if (reused) {
[email protected]ec11be62010-04-28 19:28:09971 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45972 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:31973 NetLog::IntegerCallback(
974 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:15975 }
[email protected]d13f51b2010-04-27 23:20:45976
[email protected]06650c52010-06-03 00:49:17977 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]3aa4af042012-06-14 21:02:31978 socket->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:15979
[email protected]211d2172009-07-22 15:48:53980 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:32981 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02982}
983
[email protected]d80a4322009-08-14 07:07:49984void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:10985 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:57986 DCHECK(socket);
987 IdleSocket idle_socket;
988 idle_socket.socket = socket;
989 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:57990
[email protected]aed99ef02010-08-26 14:04:32991 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:57992 IncrementIdleCount();
993}
994
[email protected]d80a4322009-08-14 07:07:49995void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:54996 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:32997 Group* group = i->second;
998 connecting_socket_count_ -= group->jobs().size();
999 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321000
[email protected]5fc08e32009-07-15 17:09:571001 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321002 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141003 // RemoveGroup() will call .erase() which will invalidate the iterator,
1004 // but i will already have been incremented to a valid iterator before
1005 // RemoveGroup() is called.
1006 RemoveGroup(i++);
1007 } else {
1008 ++i;
1009 }
1010 }
1011 DCHECK_EQ(0, connecting_socket_count_);
1012}
1013
[email protected]7af985a2012-12-14 22:40:421014void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141015 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1016 Group* group = i->second;
1017
1018 RequestQueue pending_requests;
1019 pending_requests.swap(*group->mutable_pending_requests());
1020 for (RequestQueue::iterator it2 = pending_requests.begin();
1021 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031022 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141023 InvokeUserCallbackLater(
[email protected]7af985a2012-12-14 22:40:421024 request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141025 }
1026
1027 // Delete group if no longer needed.
1028 if (group->IsEmpty()) {
1029 // RemoveGroup() will call .erase() which will invalidate the iterator,
1030 // but i will already have been incremented to a valid iterator before
1031 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321032 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541033 } else {
1034 ++i;
[email protected]5fc08e32009-07-15 17:09:571035 }
1036 }
1037}
1038
[email protected]d80a4322009-08-14 07:07:491039bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531040 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541041 int total = handed_out_socket_count_ + connecting_socket_count_ +
1042 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201043 // There can be more sockets than the limit since some requests can ignore
1044 // the limit
[email protected]c901f6d2010-04-27 17:48:281045 if (total < max_sockets_)
1046 return false;
[email protected]c901f6d2010-04-27 17:48:281047 return true;
[email protected]211d2172009-07-22 15:48:531048}
1049
[email protected]51fdc7c2012-04-10 19:19:481050bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1051 if (idle_socket_count() == 0)
1052 return false;
1053 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461054}
1055
1056bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1057 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541058 CHECK_GT(idle_socket_count(), 0);
1059
1060 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321061 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461062 if (exception_group == group)
1063 continue;
[email protected]e1b54dc2010-10-06 21:27:221064 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541065
[email protected]e1b54dc2010-10-06 21:27:221066 if (!idle_sockets->empty()) {
1067 delete idle_sockets->front().socket;
1068 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541069 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321070 if (group->IsEmpty())
1071 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541072
[email protected]dcbe168a2010-12-02 03:14:461073 return true;
[email protected]43a21b82010-06-10 21:30:541074 }
1075 }
1076
[email protected]51fdc7c2012-04-10 19:19:481077 return false;
1078}
[email protected]dcbe168a2010-12-02 03:14:461079
[email protected]51fdc7c2012-04-10 19:19:481080bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1081 // This pool doesn't have any idle sockets. It's possible that a pool at a
1082 // higher layer is holding one of this sockets active, but it's actually idle.
1083 // Query the higher layers.
1084 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1085 it != higher_layer_pools_.end(); ++it) {
1086 if ((*it)->CloseOneIdleConnection())
1087 return true;
1088 }
[email protected]dcbe168a2010-12-02 03:14:461089 return false;
[email protected]43a21b82010-06-10 21:30:541090}
1091
[email protected]05ea9ff2010-07-15 19:08:211092void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411093 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211094 CHECK(!ContainsKey(pending_callback_map_, handle));
1095 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
[email protected]2da659e2013-05-23 20:51:341096 base::MessageLoop::current()->PostTask(
[email protected]05ea9ff2010-07-15 19:08:211097 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411098 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1099 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211100}
1101
1102void ClientSocketPoolBaseHelper::InvokeUserCallback(
1103 ClientSocketHandle* handle) {
1104 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1105
1106 // Exit if the request has already been cancelled.
1107 if (it == pending_callback_map_.end())
1108 return;
1109
1110 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411111 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211112 int result = it->second.result;
1113 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411114 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211115}
1116
[email protected]58e562f2013-04-22 17:32:201117void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() {
1118 while (IsStalled()) {
1119 // Closing a socket will result in calling back into |this| to use the freed
1120 // socket slot, so nothing else is needed.
1121 if (!CloseOneIdleConnectionInLayeredPool())
1122 return;
1123 }
1124}
1125
[email protected]aed99ef02010-08-26 14:04:321126ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101127 : unassigned_job_count_(0),
1128 active_socket_count_(0),
[email protected]aa249b52013-04-30 01:04:321129 weak_factory_(this) {}
[email protected]aed99ef02010-08-26 14:04:321130
[email protected]e4d9a9722011-05-12 00:16:001131ClientSocketPoolBaseHelper::Group::~Group() {
1132 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101133 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321134}
1135
1136void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1137 const std::string& group_name,
1138 ClientSocketPoolBaseHelper* pool) {
1139 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411140 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321141 return;
1142
[email protected]2da659e2013-05-23 20:51:341143 base::MessageLoop::current()->PostDelayedTask(
[email protected]aed99ef02010-08-26 14:04:321144 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411145 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1146 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001147 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321148}
1149
[email protected]8159a1c2012-06-07 00:00:101150bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1151 SanityCheck();
1152
1153 if (unassigned_job_count_ == 0)
1154 return false;
1155 --unassigned_job_count_;
1156 return true;
1157}
1158
1159void ClientSocketPoolBaseHelper::Group::AddJob(ConnectJob* job,
1160 bool is_preconnect) {
1161 SanityCheck();
1162
1163 if (is_preconnect)
1164 ++unassigned_job_count_;
1165 jobs_.insert(job);
1166}
1167
1168void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
1169 SanityCheck();
1170
1171 jobs_.erase(job);
1172 size_t job_count = jobs_.size();
1173 if (job_count < unassigned_job_count_)
1174 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031175}
1176
[email protected]aed99ef02010-08-26 14:04:321177void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1178 std::string group_name,
1179 ClientSocketPoolBaseHelper* pool) {
1180 // If there are no more jobs pending, there is no work to do.
1181 // If we've done our cleanups correctly, this should not happen.
1182 if (jobs_.empty()) {
1183 NOTREACHED();
1184 return;
1185 }
1186
[email protected]8159a1c2012-06-07 00:00:101187 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321188 // right now due to limits, just reset the timer.
1189 if (pool->ReachedMaxSocketsLimit() ||
1190 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1191 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1192 StartBackupSocketTimer(group_name, pool);
1193 return;
1194 }
1195
[email protected]a9fc8fc2011-05-10 02:41:071196 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441197 return;
[email protected]4baaf9d2010-08-31 15:15:441198
[email protected]aed99ef02010-08-26 14:04:321199 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1200 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311201 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321202 SIMPLE_STATS_COUNTER("socket.backup_created");
1203 int rv = backup_job->Connect();
1204 pool->connecting_socket_count_++;
[email protected]8159a1c2012-06-07 00:00:101205 AddJob(backup_job, false);
[email protected]aed99ef02010-08-26 14:04:321206 if (rv != ERR_IO_PENDING)
1207 pool->OnConnectJobComplete(rv, backup_job);
1208}
1209
[email protected]8159a1c2012-06-07 00:00:101210void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1211 DCHECK_LE(unassigned_job_count_, jobs_.size());
1212}
1213
[email protected]aed99ef02010-08-26 14:04:321214void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101215 SanityCheck();
1216
[email protected]aed99ef02010-08-26 14:04:321217 // Delete active jobs.
1218 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101219 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321220
1221 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411222 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321223}
1224
[email protected]d80a4322009-08-14 07:07:491225} // namespace internal
1226
[email protected]ff579d42009-06-24 15:47:021227} // namespace net