blob: cec7956a0ee26a6a5c7e80e4ddf72822aeb1cc8f [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]18b577412013-07-18 04:19:1510#include "base/message_loop/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]f002abb2013-06-28 02:30:2114#include "base/time/time.h"
[email protected]9349cfb2010-08-31 18:00:5315#include "base/values.h"
[email protected]ff579d42009-06-24 15:47:0216#include "net/base/net_errors.h"
[email protected]f002abb2013-06-28 02:30:2117#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0218#include "net/socket/client_socket_handle.h"
19
20using base::TimeDelta;
21
[email protected]b021ece62013-06-11 11:06:3322namespace net {
23
[email protected]ff579d42009-06-24 15:47:0224namespace {
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]b021ece62013-06-11 11:06:3342// Compares the effective priority of two results, and returns 1 if |request1|
43// has greater effective priority than |request2|, 0 if they have the same
44// effective priority, and -1 if |request2| has the greater effective priority.
45// Requests with |ignore_limits| set have higher effective priority than those
46// without. If both requests have |ignore_limits| set/unset, then the request
47// with the highest Pririoty has the highest effective priority. Does not take
48// into account the fact that Requests are serviced in FIFO order if they would
49// otherwise have the same priority.
50int CompareEffectiveRequestPriority(
51 const internal::ClientSocketPoolBaseHelper::Request& request1,
52 const internal::ClientSocketPoolBaseHelper::Request& request2) {
53 if (request1.ignore_limits() && !request2.ignore_limits())
54 return 1;
55 if (!request1.ignore_limits() && request2.ignore_limits())
56 return -1;
57 if (request1.priority() > request2.priority())
58 return 1;
59 if (request1.priority() < request2.priority())
60 return -1;
61 return 0;
62}
[email protected]ff579d42009-06-24 15:47:0263
[email protected]b021ece62013-06-11 11:06:3364} // namespace
[email protected]ff579d42009-06-24 15:47:0265
[email protected]2ab05b52009-07-01 23:57:5866ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3467 base::TimeDelta timeout_duration,
[email protected]3f6007ab2013-08-22 19:45:3968 RequestPriority priority,
[email protected]fd7b7c92009-08-20 19:38:3069 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5370 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5871 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3472 timeout_duration_(timeout_duration),
[email protected]3f6007ab2013-08-22 19:45:3973 priority_(priority),
[email protected]2ab05b52009-07-01 23:57:5874 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0275 net_log_(net_log),
[email protected]8159a1c2012-06-07 00:00:1076 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5877 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5878 DCHECK(delegate);
[email protected]e9d7d252013-01-04 02:33:1779 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB,
80 NetLog::StringCallback("group_name", &group_name_));
[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]18ccfdb2013-08-15 00:13:4487scoped_ptr<StreamSocket> ConnectJob::PassSocket() {
88 return socket_.Pass();
89}
90
[email protected]974ebd62009-08-03 23:14:3491int ConnectJob::Connect() {
92 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0293 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3094
[email protected]a2006ece2010-04-23 16:44:0295 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3096
[email protected]06650c52010-06-03 00:49:1797 LogConnectStart();
98
[email protected]fd7b7c92009-08-20 19:38:3099 int rv = ConnectInternal();
100
101 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:17102 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30103 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:30104 }
105
106 return rv;
107}
108
[email protected]18ccfdb2013-08-15 00:13:44109void ConnectJob::SetSocket(scoped_ptr<StreamSocket> socket) {
[email protected]06650c52010-06-03 00:49:17110 if (socket) {
[email protected]3aa4af042012-06-14 21:02:31111 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
112 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17113 }
[email protected]18ccfdb2013-08-15 00:13:44114 socket_ = socket.Pass();
[email protected]06650c52010-06-03 00:49:17115}
116
[email protected]fd7b7c92009-08-20 19:38:30117void ConnectJob::NotifyDelegateOfCompletion(int rv) {
[email protected]18ccfdb2013-08-15 00:13:44118 // The delegate will own |this|.
[email protected]10833352013-02-12 19:39:50119 Delegate* delegate = delegate_;
[email protected]fd7b7c92009-08-20 19:38:30120 delegate_ = NULL;
121
[email protected]06650c52010-06-03 00:49:17122 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30123 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34124}
125
[email protected]a796bcec2010-03-22 17:17:26126void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
127 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02128 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26129}
130
[email protected]06650c52010-06-03 00:49:17131void ConnectJob::LogConnectStart() {
[email protected]034df0f32013-01-07 23:17:48132 connect_timing_.connect_start = base::TimeTicks::Now();
[email protected]e9d7d252013-01-04 02:33:17133 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT);
[email protected]06650c52010-06-03 00:49:17134}
135
136void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]034df0f32013-01-07 23:17:48137 connect_timing_.connect_end = base::TimeTicks::Now();
[email protected]d7fd1782011-02-08 19:16:43138 net_log().EndEventWithNetErrorCode(
139 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17140}
141
[email protected]974ebd62009-08-03 23:14:34142void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40143 // Make sure the socket is NULL before calling into |delegate|.
[email protected]18ccfdb2013-08-15 00:13:44144 SetSocket(scoped_ptr<StreamSocket>());
[email protected]fd7b7c92009-08-20 19:38:30145
[email protected]3aa4af042012-06-14 21:02:31146 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30147
148 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34149}
150
[email protected]d80a4322009-08-14 07:07:49151namespace internal {
152
[email protected]fd4fe0b2010-02-08 23:02:15153ClientSocketPoolBaseHelper::Request::Request(
154 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41155 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15156 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20157 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03158 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53159 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13160 : handle_(handle),
161 callback_(callback),
162 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20163 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03164 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53165 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15166
167ClientSocketPoolBaseHelper::Request::~Request() {}
168
[email protected]d80a4322009-08-14 07:07:49169ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]043b68c82013-08-22 23:41:52170 HigherLayeredPool* pool,
[email protected]211d2172009-07-22 15:48:53171 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02172 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16173 base::TimeDelta unused_idle_socket_timeout,
174 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38175 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02176 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53177 connecting_socket_count_(0),
178 handed_out_socket_count_(0),
179 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02180 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41181 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16182 unused_idle_socket_timeout_(unused_idle_socket_timeout),
183 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35184 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22185 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13186 pool_generation_number_(0),
[email protected]043b68c82013-08-22 23:41:52187 pool_(pool),
[email protected]aa249b52013-04-30 01:04:32188 weak_factory_(this) {
[email protected]211d2172009-07-22 15:48:53189 DCHECK_LE(0, max_sockets_per_group);
190 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52191
[email protected]232a5812011-03-04 22:42:08192 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53193}
[email protected]ff579d42009-06-24 15:47:02194
[email protected]d80a4322009-08-14 07:07:49195ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13196 // Clean up any idle sockets and pending connect jobs. Assert that we have no
197 // remaining active sockets or pending requests. They should have all been
198 // cleaned up prior to |this| being destroyed.
[email protected]7af985a2012-12-14 22:40:42199 FlushWithError(ERR_ABORTED);
[email protected]2431756e2010-09-29 20:26:13200 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21201 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29202 DCHECK_EQ(0, connecting_socket_count_);
[email protected]043b68c82013-08-22 23:41:52203 CHECK(higher_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52204
[email protected]232a5812011-03-04 22:42:08205 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]043b68c82013-08-22 23:41:52206
207 // Remove from lower layer pools.
208 for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin();
209 it != lower_pools_.end();
210 ++it) {
211 (*it)->RemoveHigherLayeredPool(pool_);
212 }
[email protected]ff579d42009-06-24 15:47:02213}
214
[email protected]2a848e0e2012-08-09 22:27:31215ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
216 : result(OK) {
217}
218
219ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
220 const CompletionCallback& callback_in, int result_in)
221 : callback(callback_in),
222 result(result_in) {
223}
224
[email protected]49639fa2011-12-20 23:22:41225ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
226
[email protected]043b68c82013-08-22 23:41:52227bool ClientSocketPoolBaseHelper::IsStalled() const {
228 // If a lower layer pool is stalled, consider |this| stalled as well.
229 for (std::set<LowerLayeredPool*>::const_iterator it = lower_pools_.begin();
230 it != lower_pools_.end();
231 ++it) {
232 if ((*it)->IsStalled())
233 return true;
234 }
235
236 // If fewer than |max_sockets_| are in use, then clearly |this| is not
237 // stalled.
238 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
239 return false;
240 // So in order to be stalled, |this| must be using at least |max_sockets_| AND
241 // |this| must have a request that is actually stalled on the global socket
242 // limit. To find such a request, look for a group that has more requests
243 // than jobs AND where the number of sockets is less than
244 // |max_sockets_per_group_|. (If the number of sockets is equal to
245 // |max_sockets_per_group_|, then the request is stalled on the group limit,
246 // which does not count.)
247 for (GroupMap::const_iterator it = group_map_.begin();
248 it != group_map_.end(); ++it) {
249 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
250 return true;
251 }
252 return false;
[email protected]51fdc7c2012-04-10 19:19:48253}
254
[email protected]043b68c82013-08-22 23:41:52255void ClientSocketPoolBaseHelper::AddLowerLayeredPool(
256 LowerLayeredPool* lower_pool) {
257 DCHECK(pool_);
258 CHECK(!ContainsKey(lower_pools_, lower_pool));
259 lower_pools_.insert(lower_pool);
260 lower_pool->AddHigherLayeredPool(pool_);
261}
262
263void ClientSocketPoolBaseHelper::AddHigherLayeredPool(
264 HigherLayeredPool* higher_pool) {
265 CHECK(higher_pool);
266 CHECK(!ContainsKey(higher_pools_, higher_pool));
267 higher_pools_.insert(higher_pool);
268}
269
270void ClientSocketPoolBaseHelper::RemoveHigherLayeredPool(
271 HigherLayeredPool* higher_pool) {
272 CHECK(higher_pool);
273 CHECK(ContainsKey(higher_pools_, higher_pool));
274 higher_pools_.erase(higher_pool);
[email protected]51fdc7c2012-04-10 19:19:48275}
276
[email protected]d80a4322009-08-14 07:07:49277int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02278 const std::string& group_name,
[email protected]b74d4bd2013-08-23 00:13:36279 scoped_ptr<const Request> request) {
[email protected]49639fa2011-12-20 23:22:41280 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03281 CHECK(request->handle());
282
[email protected]64770b7d2011-11-16 04:30:41283 // Cleanup any timed-out idle sockets if no timer is used.
284 if (!use_cleanup_timer_)
285 CleanupIdleSockets(false);
286
[email protected]3aa4af042012-06-14 21:02:31287 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32288 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26289
[email protected]b74d4bd2013-08-23 00:13:36290 int rv = RequestSocketInternal(group_name, *request);
[email protected]e7e99322010-05-04 23:30:17291 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43292 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21293 CHECK(!request->handle()->is_initialized());
[email protected]b74d4bd2013-08-23 00:13:36294 request.reset();
[email protected]e7e99322010-05-04 23:30:17295 } else {
[email protected]b74d4bd2013-08-23 00:13:36296 group->InsertPendingRequest(request.Pass());
[email protected]58e562f2013-04-22 17:32:20297 // Have to do this asynchronously, as closing sockets in higher level pools
298 // call back in to |this|, which will cause all sorts of fun and exciting
299 // re-entrancy issues if the socket pool is doing something else at the
300 // time.
301 if (group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
[email protected]2da659e2013-05-23 20:51:34302 base::MessageLoop::current()->PostTask(
[email protected]58e562f2013-04-22 17:32:20303 FROM_HERE,
304 base::Bind(
305 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
306 weak_factory_.GetWeakPtr()));
307 }
[email protected]e7e99322010-05-04 23:30:17308 }
[email protected]fd4fe0b2010-02-08 23:02:15309 return rv;
310}
311
[email protected]2c2bef152010-10-13 00:55:03312void ClientSocketPoolBaseHelper::RequestSockets(
313 const std::string& group_name,
314 const Request& request,
315 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41316 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03317 DCHECK(!request.handle());
318
[email protected]64770b7d2011-11-16 04:30:41319 // Cleanup any timed out idle sockets if no timer is used.
320 if (!use_cleanup_timer_)
321 CleanupIdleSockets(false);
322
[email protected]2c2bef152010-10-13 00:55:03323 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03324 num_sockets = max_sockets_per_group_;
325 }
326
327 request.net_log().BeginEvent(
328 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31329 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03330
331 Group* group = GetOrCreateGroup(group_name);
332
[email protected]3c819f522010-12-02 02:03:12333 // RequestSocketsInternal() may delete the group.
334 bool deleted_group = false;
335
[email protected]d7fd1782011-02-08 19:16:43336 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03337 for (int num_iterations_left = num_sockets;
338 group->NumActiveSocketSlots() < num_sockets &&
339 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]b74d4bd2013-08-23 00:13:36340 rv = RequestSocketInternal(group_name, request);
[email protected]2c2bef152010-10-13 00:55:03341 if (rv < 0 && rv != ERR_IO_PENDING) {
342 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12343 if (!ContainsKey(group_map_, group_name))
344 deleted_group = true;
345 break;
346 }
347 if (!ContainsKey(group_map_, group_name)) {
348 // Unexpected. The group should only be getting deleted on synchronous
349 // error.
350 NOTREACHED();
351 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03352 break;
353 }
354 }
355
[email protected]3c819f522010-12-02 02:03:12356 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03357 RemoveGroup(group_name);
358
[email protected]d7fd1782011-02-08 19:16:43359 if (rv == ERR_IO_PENDING)
360 rv = OK;
361 request.net_log().EndEventWithNetErrorCode(
362 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03363}
364
[email protected]fd4fe0b2010-02-08 23:02:15365int ClientSocketPoolBaseHelper::RequestSocketInternal(
366 const std::string& group_name,
[email protected]b74d4bd2013-08-23 00:13:36367 const Request& request) {
368 ClientSocketHandle* const handle = request.handle();
[email protected]2c2bef152010-10-13 00:55:03369 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32370 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02371
[email protected]b74d4bd2013-08-23 00:13:36372 if (!(request.flags() & NO_IDLE_SOCKETS)) {
[email protected]2c2bef152010-10-13 00:55:03373 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10374 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03375 return OK;
376 }
377
[email protected]8159a1c2012-06-07 00:00:10378 // If there are more ConnectJobs than pending requests, don't need to do
379 // anything. Can just wait for the extra job to connect, and then assign it
380 // to the request.
381 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03382 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10383
[email protected]43a21b82010-06-10 21:30:54384 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20385 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
[email protected]b74d4bd2013-08-23 00:13:36386 !request.ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48387 // TODO(willchan): Consider whether or not we need to close a socket in a
388 // higher layered group. I don't think this makes sense since we would just
389 // reuse that socket then if we needed one and wouldn't make it down to this
390 // layer.
[email protected]b74d4bd2013-08-23 00:13:36391 request.net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31392 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54393 return ERR_IO_PENDING;
394 }
395
[email protected]b74d4bd2013-08-23 00:13:36396 if (ReachedMaxSocketsLimit() && !request.ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48397 // NOTE(mmenke): Wonder if we really need different code for each case
398 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54399 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48400 // There's an idle socket in this pool. Either that's because there's
401 // still one in this group, but we got here due to preconnecting bypassing
402 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46403 bool closed = CloseOneIdleSocketExceptInGroup(group);
404 if (preconnecting && !closed)
405 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54406 } else {
[email protected]40dee91a2012-04-17 19:25:46407 // We could check if we really have a stalled group here, but it requires
408 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]b74d4bd2013-08-23 00:13:36409 request.net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46410 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54411 }
412 }
413
[email protected]51fdc7c2012-04-10 19:19:48414 // We couldn't find a socket to reuse, and there's space to allocate one,
415 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58416 scoped_ptr<ConnectJob> connect_job(
[email protected]b74d4bd2013-08-23 00:13:36417 connect_job_factory_->NewConnectJob(group_name, request, this));
[email protected]ff579d42009-06-24 15:47:02418
[email protected]2ab05b52009-07-01 23:57:58419 int rv = connect_job->Connect();
420 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17421 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03422 if (!preconnecting) {
[email protected]18ccfdb2013-08-15 00:13:44423 HandOutSocket(connect_job->PassSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48424 connect_job->connect_timing(), handle, base::TimeDelta(),
[email protected]b74d4bd2013-08-23 00:13:36425 group, request.net_log());
[email protected]2c2bef152010-10-13 00:55:03426 } else {
[email protected]18ccfdb2013-08-15 00:13:44427 AddIdleSocket(connect_job->PassSocket(), group);
[email protected]2c2bef152010-10-13 00:55:03428 }
[email protected]2ab05b52009-07-01 23:57:58429 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32430 // If we don't have any sockets in this group, set a timer for potentially
431 // creating a new one. If the SYN is lost, this backup socket may complete
432 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03433 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00434 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32435 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03436 }
[email protected]6b624c62010-03-14 08:37:32437
[email protected]211d2172009-07-22 15:48:53438 connecting_socket_count_++;
439
[email protected]18ccfdb2013-08-15 00:13:44440 group->AddJob(connect_job.Pass(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02441 } else {
[email protected]06650c52010-06-03 00:49:17442 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]18ccfdb2013-08-15 00:13:44443 scoped_ptr<StreamSocket> error_socket;
[email protected]fd2e53e2011-01-14 20:40:52444 if (!preconnecting) {
445 DCHECK(handle);
446 connect_job->GetAdditionalErrorState(handle);
[email protected]18ccfdb2013-08-15 00:13:44447 error_socket = connect_job->PassSocket();
[email protected]fd2e53e2011-01-14 20:40:52448 }
[email protected]e772db3f2010-07-12 18:11:13449 if (error_socket) {
[email protected]18ccfdb2013-08-15 00:13:44450 HandOutSocket(error_socket.Pass(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48451 connect_job->connect_timing(), handle, base::TimeDelta(),
[email protected]b74d4bd2013-08-23 00:13:36452 group, request.net_log());
[email protected]aed99ef02010-08-26 14:04:32453 } else if (group->IsEmpty()) {
454 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21455 }
[email protected]2ab05b52009-07-01 23:57:58456 }
[email protected]ff579d42009-06-24 15:47:02457
[email protected]2ab05b52009-07-01 23:57:58458 return rv;
[email protected]ff579d42009-06-24 15:47:02459}
460
[email protected]8159a1c2012-06-07 00:00:10461bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]b74d4bd2013-08-23 00:13:36462 const Request& request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22463 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
464 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
465
466 // Iterate through the idle sockets forwards (oldest to newest)
467 // * Delete any disconnected ones.
468 // * If we find a used idle socket, assign to |idle_socket|. At the end,
469 // the |idle_socket_it| will be set to the newest used idle socket.
470 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
471 it != idle_sockets->end();) {
472 if (!it->socket->IsConnectedAndIdle()) {
473 DecrementIdleCount();
474 delete it->socket;
475 it = idle_sockets->erase(it);
476 continue;
[email protected]eb5a99382010-07-11 03:18:26477 }
[email protected]e1b54dc2010-10-06 21:27:22478
479 if (it->socket->WasEverUsed()) {
480 // We found one we can reuse!
[email protected]e86df8dc2013-03-30 13:18:28481 idle_socket_it = it;
[email protected]e1b54dc2010-10-06 21:27:22482 }
483
484 ++it;
[email protected]eb5a99382010-07-11 03:18:26485 }
[email protected]e1b54dc2010-10-06 21:27:22486
487 // If we haven't found an idle socket, that means there are no used idle
488 // sockets. Pick the oldest (first) idle socket (FIFO).
489
490 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
491 idle_socket_it = idle_sockets->begin();
492
493 if (idle_socket_it != idle_sockets->end()) {
494 DecrementIdleCount();
495 base::TimeDelta idle_time =
496 base::TimeTicks::Now() - idle_socket_it->start_time;
497 IdleSocket idle_socket = *idle_socket_it;
498 idle_sockets->erase(idle_socket_it);
499 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44500 scoped_ptr<StreamSocket>(idle_socket.socket),
[email protected]e1b54dc2010-10-06 21:27:22501 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48502 LoadTimingInfo::ConnectTiming(),
[email protected]b74d4bd2013-08-23 00:13:36503 request.handle(),
[email protected]e1b54dc2010-10-06 21:27:22504 idle_time,
505 group,
[email protected]b74d4bd2013-08-23 00:13:36506 request.net_log());
[email protected]e1b54dc2010-10-06 21:27:22507 return true;
508 }
509
[email protected]eb5a99382010-07-11 03:18:26510 return false;
511}
512
[email protected]06650c52010-06-03 00:49:17513// static
514void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
[email protected]b74d4bd2013-08-23 00:13:36515 const NetLog::Source& connect_job_source, const Request& request) {
516 request.net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
517 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17518}
519
[email protected]d80a4322009-08-14 07:07:49520void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21521 const std::string& group_name, ClientSocketHandle* handle) {
522 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
523 if (callback_it != pending_callback_map_.end()) {
524 int result = callback_it->second.result;
525 pending_callback_map_.erase(callback_it);
[email protected]18ccfdb2013-08-15 00:13:44526 scoped_ptr<StreamSocket> socket = handle->PassSocket();
[email protected]05ea9ff2010-07-15 19:08:21527 if (socket) {
528 if (result != OK)
529 socket->Disconnect();
[email protected]18ccfdb2013-08-15 00:13:44530 ReleaseSocket(handle->group_name(), socket.Pass(), handle->id());
[email protected]05ea9ff2010-07-15 19:08:21531 }
532 return;
533 }
[email protected]b6501d3d2010-06-03 23:53:34534
[email protected]ff579d42009-06-24 15:47:02535 CHECK(ContainsKey(group_map_, group_name));
536
[email protected]aed99ef02010-08-26 14:04:32537 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02538
[email protected]ff579d42009-06-24 15:47:02539 // Search pending_requests for matching handle.
[email protected]b74d4bd2013-08-23 00:13:36540 scoped_ptr<const Request> request =
541 group->FindAndRemovePendingRequest(handle);
542 if (request) {
543 request->net_log().AddEvent(NetLog::TYPE_CANCELLED);
544 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26545
[email protected]b74d4bd2013-08-23 00:13:36546 // We let the job run, unless we're at the socket limit and there is
547 // not another request waiting on the job.
548 if (group->jobs().size() > group->pending_request_count() &&
549 ReachedMaxSocketsLimit()) {
550 RemoveConnectJob(*group->jobs().begin(), group);
551 CheckForStalledSocketGroups();
[email protected]ff579d42009-06-24 15:47:02552 }
553 }
[email protected]ff579d42009-06-24 15:47:02554}
555
[email protected]2abfe90a2010-08-25 17:49:51556bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
557 return ContainsKey(group_map_, group_name);
558}
559
[email protected]d80a4322009-08-14 07:07:49560void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02561 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14562 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02563}
564
[email protected]d80a4322009-08-14 07:07:49565int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02566 const std::string& group_name) const {
567 GroupMap::const_iterator i = group_map_.find(group_name);
568 CHECK(i != group_map_.end());
569
[email protected]aed99ef02010-08-26 14:04:32570 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02571}
572
[email protected]d80a4322009-08-14 07:07:49573LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02574 const std::string& group_name,
575 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21576 if (ContainsKey(pending_callback_map_, handle))
577 return LOAD_STATE_CONNECTING;
578
[email protected]ff579d42009-06-24 15:47:02579 if (!ContainsKey(group_map_, group_name)) {
580 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
581 << " for handle: " << handle;
582 return LOAD_STATE_IDLE;
583 }
584
585 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32586 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02587
[email protected]b74d4bd2013-08-23 00:13:36588 if (group.HasConnectJobForHandle(handle)) {
[email protected]03b7c8c2013-07-20 04:38:55589 // Just return the state of the farthest along ConnectJob for the first
590 // group.jobs().size() pending requests.
591 LoadState max_state = LOAD_STATE_IDLE;
592 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
593 job_it != group.jobs().end(); ++job_it) {
594 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]ff579d42009-06-24 15:47:02595 }
[email protected]03b7c8c2013-07-20 04:38:55596 return max_state;
[email protected]ff579d42009-06-24 15:47:02597 }
598
[email protected]03b7c8c2013-07-20 04:38:55599 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_))
600 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL;
601 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET;
[email protected]ff579d42009-06-24 15:47:02602}
603
[email protected]10833352013-02-12 19:39:50604base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27605 const std::string& name, const std::string& type) const {
[email protected]10833352013-02-12 19:39:50606 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27607 dict->SetString("name", name);
608 dict->SetString("type", type);
609 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
610 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
611 dict->SetInteger("idle_socket_count", idle_socket_count_);
612 dict->SetInteger("max_socket_count", max_sockets_);
613 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
614 dict->SetInteger("pool_generation_number", pool_generation_number_);
615
616 if (group_map_.empty())
617 return dict;
618
[email protected]10833352013-02-12 19:39:50619 base::DictionaryValue* all_groups_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27620 for (GroupMap::const_iterator it = group_map_.begin();
621 it != group_map_.end(); it++) {
622 const Group* group = it->second;
[email protected]10833352013-02-12 19:39:50623 base::DictionaryValue* group_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27624
625 group_dict->SetInteger("pending_request_count",
[email protected]b74d4bd2013-08-23 00:13:36626 group->pending_request_count());
627 if (group->has_pending_requests()) {
[email protected]59d7a5a2010-08-30 16:44:27628 group_dict->SetInteger("top_pending_priority",
629 group->TopPendingPriority());
630 }
631
632 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07633
[email protected]10833352013-02-12 19:39:50634 base::ListValue* idle_socket_list = new base::ListValue();
[email protected]e1b54dc2010-10-06 21:27:22635 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07636 for (idle_socket = group->idle_sockets().begin();
637 idle_socket != group->idle_sockets().end();
638 idle_socket++) {
639 int source_id = idle_socket->socket->NetLog().source().id;
[email protected]10833352013-02-12 19:39:50640 idle_socket_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07641 }
642 group_dict->Set("idle_sockets", idle_socket_list);
643
[email protected]10833352013-02-12 19:39:50644 base::ListValue* connect_jobs_list = new base::ListValue();
[email protected]2c2bef152010-10-13 00:55:03645 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07646 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
647 int source_id = (*job)->net_log().source().id;
[email protected]10833352013-02-12 19:39:50648 connect_jobs_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07649 }
650 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27651
652 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48653 group->IsStalledOnPoolMaxSockets(
654 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00655 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27656
657 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
658 }
659 dict->Set("groups", all_groups_dict);
660 return dict;
661}
662
[email protected]d80a4322009-08-14 07:07:49663bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16664 base::TimeTicks now,
665 base::TimeDelta timeout) const {
666 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01667 if (timed_out)
668 return true;
669 if (socket->WasEverUsed())
670 return !socket->IsConnectedAndIdle();
671 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02672}
673
[email protected]d80a4322009-08-14 07:07:49674void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02675 if (idle_socket_count_ == 0)
676 return;
677
678 // Current time value. Retrieving it once at the function start rather than
679 // inside the inner loop, since it shouldn't change by any meaningful amount.
680 base::TimeTicks now = base::TimeTicks::Now();
681
682 GroupMap::iterator i = group_map_.begin();
683 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32684 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02685
[email protected]e1b54dc2010-10-06 21:27:22686 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32687 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16688 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01689 j->socket->WasEverUsed() ?
690 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16691 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02692 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32693 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02694 DecrementIdleCount();
695 } else {
696 ++j;
697 }
698 }
699
700 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32701 if (group->IsEmpty()) {
702 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02703 } else {
704 ++i;
705 }
706 }
707}
708
[email protected]aed99ef02010-08-26 14:04:32709ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
710 const std::string& group_name) {
711 GroupMap::iterator it = group_map_.find(group_name);
712 if (it != group_map_.end())
713 return it->second;
714 Group* group = new Group;
715 group_map_[group_name] = group;
716 return group;
717}
718
719void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
720 GroupMap::iterator it = group_map_.find(group_name);
721 CHECK(it != group_map_.end());
722
723 RemoveGroup(it);
724}
725
726void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
727 delete it->second;
728 group_map_.erase(it);
729}
730
[email protected]06d94042010-08-25 01:45:22731// static
[email protected]2d6728692011-03-12 01:39:55732bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
733 return g_connect_backup_jobs_enabled;
734}
735
736// static
[email protected]636b8252011-04-08 19:56:54737bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
738 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22739 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54740 return old_value;
[email protected]06d94042010-08-25 01:45:22741}
742
743void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
744 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
745}
746
[email protected]d80a4322009-08-14 07:07:49747void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41748 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
749 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02750}
751
[email protected]d80a4322009-08-14 07:07:49752void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02753 if (--idle_socket_count_ == 0)
754 timer_.Stop();
755}
756
[email protected]64770b7d2011-11-16 04:30:41757// static
758bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
759 return g_cleanup_timer_enabled;
760}
761
762// static
763bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
764 bool old_value = g_cleanup_timer_enabled;
765 g_cleanup_timer_enabled = enabled;
766 return old_value;
767}
768
769void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
770 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
771 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
772}
773
[email protected]eb5a99382010-07-11 03:18:26774void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]18ccfdb2013-08-15 00:13:44775 scoped_ptr<StreamSocket> socket,
[email protected]eb5a99382010-07-11 03:18:26776 int id) {
[email protected]ff579d42009-06-24 15:47:02777 GroupMap::iterator i = group_map_.find(group_name);
778 CHECK(i != group_map_.end());
779
[email protected]aed99ef02010-08-26 14:04:32780 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02781
[email protected]b1f031dd2010-03-02 23:19:33782 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53783 handed_out_socket_count_--;
784
[email protected]aed99ef02010-08-26 14:04:32785 CHECK_GT(group->active_socket_count(), 0);
786 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02787
[email protected]a7e38572010-06-07 18:22:24788 const bool can_reuse = socket->IsConnectedAndIdle() &&
789 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02790 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26791 // Add it to the idle list.
[email protected]18ccfdb2013-08-15 00:13:44792 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32793 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02794 } else {
[email protected]18ccfdb2013-08-15 00:13:44795 socket.reset();
[email protected]ff579d42009-06-24 15:47:02796 }
[email protected]05ea9ff2010-07-15 19:08:21797
[email protected]eb5a99382010-07-11 03:18:26798 CheckForStalledSocketGroups();
799}
[email protected]ff579d42009-06-24 15:47:02800
[email protected]eb5a99382010-07-11 03:18:26801void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
802 // If we have idle sockets, see if we can give one to the top-stalled group.
803 std::string top_group_name;
804 Group* top_group = NULL;
[email protected]043b68c82013-08-22 23:41:52805 if (!FindTopStalledGroup(&top_group, &top_group_name)) {
806 // There may still be a stalled group in a lower level pool.
807 for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin();
808 it != lower_pools_.end();
809 ++it) {
810 if ((*it)->IsStalled()) {
811 CloseOneIdleSocket();
812 break;
813 }
814 }
[email protected]eb5a99382010-07-11 03:18:26815 return;
[email protected]043b68c82013-08-22 23:41:52816 }
[email protected]4f2abec2010-02-03 18:10:16817
[email protected]eb5a99382010-07-11 03:18:26818 if (ReachedMaxSocketsLimit()) {
819 if (idle_socket_count() > 0) {
820 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54821 } else {
[email protected]eb5a99382010-07-11 03:18:26822 // We can't activate more sockets since we're already at our global
823 // limit.
[email protected]4f2abec2010-02-03 18:10:16824 return;
[email protected]d7027bb2010-05-10 18:58:54825 }
[email protected]4f2abec2010-02-03 18:10:16826 }
[email protected]eb5a99382010-07-11 03:18:26827
828 // Note: we don't loop on waking stalled groups. If the stalled group is at
829 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21830 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26831 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21832 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02833}
834
[email protected]211d2172009-07-22 15:48:53835// Search for the highest priority pending request, amongst the groups that
836// are not at the |max_sockets_per_group_| limit. Note: for requests with
837// the same priority, the winner is based on group hash ordering (and not
838// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48839bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
840 Group** group,
841 std::string* group_name) const {
842 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53843 Group* top_group = NULL;
844 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21845 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48846 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53847 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32848 Group* curr_group = i->second;
[email protected]b74d4bd2013-08-23 00:13:36849 if (!curr_group->has_pending_requests())
[email protected]211d2172009-07-22 15:48:53850 continue;
[email protected]51fdc7c2012-04-10 19:19:48851 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
852 if (!group)
853 return true;
[email protected]05ea9ff2010-07-15 19:08:21854 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41855 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05856 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41857 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32858 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41859 top_group_name = &i->first;
860 }
[email protected]211d2172009-07-22 15:48:53861 }
862 }
[email protected]05ea9ff2010-07-15 19:08:21863
[email protected]211d2172009-07-22 15:48:53864 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48865 CHECK(group);
[email protected]211d2172009-07-22 15:48:53866 *group = top_group;
867 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48868 } else {
869 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53870 }
[email protected]05ea9ff2010-07-15 19:08:21871 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53872}
873
[email protected]d80a4322009-08-14 07:07:49874void ClientSocketPoolBaseHelper::OnConnectJobComplete(
875 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58876 DCHECK_NE(ERR_IO_PENDING, result);
877 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02878 GroupMap::iterator group_it = group_map_.find(group_name);
879 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32880 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02881
[email protected]18ccfdb2013-08-15 00:13:44882 scoped_ptr<StreamSocket> socket = job->PassSocket();
[email protected]ff579d42009-06-24 15:47:02883
[email protected]034df0f32013-01-07 23:17:48884 // Copies of these are needed because |job| may be deleted before they are
885 // accessed.
[email protected]9e743cd2010-03-16 07:03:53886 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48887 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57888
[email protected]18ccfdb2013-08-15 00:13:44889 // RemoveConnectJob(job, _) must be called by all branches below;
890 // otherwise, |job| will be leaked.
891
[email protected]4d3b05d2010-01-27 21:27:29892 if (result == OK) {
893 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32894 RemoveConnectJob(job, group);
[email protected]b74d4bd2013-08-23 00:13:36895 scoped_ptr<const Request> request = group->PopNextPendingRequest();
896 if (request) {
897 LogBoundConnectJobToRequest(job_log.source(), *request);
[email protected]4d3b05d2010-01-27 21:27:29898 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44899 socket.Pass(), false /* unused socket */, connect_timing,
[email protected]b74d4bd2013-08-23 00:13:36900 request->handle(), base::TimeDelta(), group, request->net_log());
901 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
902 InvokeUserCallbackLater(request->handle(), request->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57903 } else {
[email protected]18ccfdb2013-08-15 00:13:44904 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32905 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21906 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57907 }
[email protected]94c20472010-01-14 08:14:36908 } else {
[email protected]e772db3f2010-07-12 18:11:13909 // If we got a socket, it must contain error information so pass that
910 // up so that the caller can retrieve it.
911 bool handed_out_socket = false;
[email protected]b74d4bd2013-08-23 00:13:36912 scoped_ptr<const Request> request = group->PopNextPendingRequest();
913 if (request) {
914 LogBoundConnectJobToRequest(job_log.source(), *request);
915 job->GetAdditionalErrorState(request->handle());
[email protected]aed99ef02010-08-26 14:04:32916 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13917 if (socket.get()) {
918 handed_out_socket = true;
[email protected]18ccfdb2013-08-15 00:13:44919 HandOutSocket(socket.Pass(), false /* unused socket */,
[email protected]b74d4bd2013-08-23 00:13:36920 connect_timing, request->handle(), base::TimeDelta(),
921 group, request->net_log());
[email protected]e772db3f2010-07-12 18:11:13922 }
[email protected]b74d4bd2013-08-23 00:13:36923 request->net_log().EndEventWithNetErrorCode(
924 NetLog::TYPE_SOCKET_POOL, result);
925 InvokeUserCallbackLater(request->handle(), request->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18926 } else {
[email protected]aed99ef02010-08-26 14:04:32927 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29928 }
[email protected]05ea9ff2010-07-15 19:08:21929 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32930 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21931 CheckForStalledSocketGroups();
932 }
[email protected]ff579d42009-06-24 15:47:02933 }
[email protected]ff579d42009-06-24 15:47:02934}
935
[email protected]66761b952010-06-25 21:30:38936void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42937 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38938}
939
[email protected]7af985a2012-12-14 22:40:42940void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24941 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14942 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52943 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42944 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52945}
946
[email protected]2c2bef152010-10-13 00:55:03947void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29948 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33949 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53950 connecting_socket_count_--;
951
[email protected]25eea382010-07-10 23:55:26952 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32953 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26954
955 // If we've got no more jobs for this group, then we no longer need a
956 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32957 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00958 group->CleanupBackupJob();
[email protected]2ab05b52009-07-01 23:57:58959}
[email protected]ff579d42009-06-24 15:47:02960
[email protected]8ae03f42010-07-07 19:08:10961void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21962 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51963 DCHECK(ContainsKey(group_map_, group_name));
[email protected]b74d4bd2013-08-23 00:13:36964 if (group->IsEmpty()) {
[email protected]aed99ef02010-08-26 14:04:32965 RemoveGroup(group_name);
[email protected]b74d4bd2013-08-23 00:13:36966 } else if (group->has_pending_requests()) {
[email protected]2abfe90a2010-08-25 17:49:51967 ProcessPendingRequest(group_name, group);
[email protected]b74d4bd2013-08-23 00:13:36968 }
[email protected]8ae03f42010-07-07 19:08:10969}
[email protected]06650c52010-06-03 00:49:17970
[email protected]8ae03f42010-07-07 19:08:10971void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21972 const std::string& group_name, Group* group) {
[email protected]b74d4bd2013-08-23 00:13:36973 const Request* next_request = group->GetNextPendingRequest();
974 DCHECK(next_request);
975 int rv = RequestSocketInternal(group_name, *next_request);
[email protected]05ea9ff2010-07-15 19:08:21976 if (rv != ERR_IO_PENDING) {
[email protected]b74d4bd2013-08-23 00:13:36977 scoped_ptr<const Request> request = group->PopNextPendingRequest();
978 DCHECK(request);
[email protected]2abfe90a2010-08-25 17:49:51979 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32980 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10981
[email protected]d7fd1782011-02-08 19:16:43982 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41983 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58984 }
985}
986
[email protected]d80a4322009-08-14 07:07:49987void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44988 scoped_ptr<StreamSocket> socket,
[email protected]2ab05b52009-07-01 23:57:58989 bool reused,
[email protected]034df0f32013-01-07 23:17:48990 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:58991 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29992 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15993 Group* group,
[email protected]9e743cd2010-03-16 07:03:53994 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58995 DCHECK(socket);
[email protected]18ccfdb2013-08-15 00:13:44996 handle->SetSocket(socket.Pass());
[email protected]2ab05b52009-07-01 23:57:58997 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29998 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24999 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:481000 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:531001
[email protected]d13f51b2010-04-27 23:20:451002 if (reused) {
[email protected]ec11be62010-04-28 19:28:091003 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:451004 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311005 NetLog::IntegerCallback(
1006 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:151007 }
[email protected]d13f51b2010-04-27 23:20:451008
[email protected]18ccfdb2013-08-15 00:13:441009 net_log.AddEvent(
1010 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
1011 handle->socket()->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:151012
[email protected]211d2172009-07-22 15:48:531013 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321014 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021015}
1016
[email protected]d80a4322009-08-14 07:07:491017void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]18ccfdb2013-08-15 00:13:441018 scoped_ptr<StreamSocket> socket,
1019 Group* group) {
[email protected]5fc08e32009-07-15 17:09:571020 DCHECK(socket);
1021 IdleSocket idle_socket;
[email protected]18ccfdb2013-08-15 00:13:441022 idle_socket.socket = socket.release();
[email protected]5fc08e32009-07-15 17:09:571023 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571024
[email protected]aed99ef02010-08-26 14:04:321025 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571026 IncrementIdleCount();
1027}
1028
[email protected]d80a4322009-08-14 07:07:491029void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541030 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321031 Group* group = i->second;
1032 connecting_socket_count_ -= group->jobs().size();
1033 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321034
[email protected]5fc08e32009-07-15 17:09:571035 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321036 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141037 // RemoveGroup() will call .erase() which will invalidate the iterator,
1038 // but i will already have been incremented to a valid iterator before
1039 // RemoveGroup() is called.
1040 RemoveGroup(i++);
1041 } else {
1042 ++i;
1043 }
1044 }
1045 DCHECK_EQ(0, connecting_socket_count_);
1046}
1047
[email protected]7af985a2012-12-14 22:40:421048void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141049 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1050 Group* group = i->second;
1051
[email protected]b74d4bd2013-08-23 00:13:361052 while (true) {
1053 scoped_ptr<const Request> request = group->PopNextPendingRequest();
1054 if (!request)
1055 break;
1056 InvokeUserCallbackLater(request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141057 }
1058
1059 // Delete group if no longer needed.
1060 if (group->IsEmpty()) {
1061 // RemoveGroup() will call .erase() which will invalidate the iterator,
1062 // but i will already have been incremented to a valid iterator before
1063 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321064 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541065 } else {
1066 ++i;
[email protected]5fc08e32009-07-15 17:09:571067 }
1068 }
1069}
1070
[email protected]d80a4322009-08-14 07:07:491071bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531072 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541073 int total = handed_out_socket_count_ + connecting_socket_count_ +
1074 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201075 // There can be more sockets than the limit since some requests can ignore
1076 // the limit
[email protected]c901f6d2010-04-27 17:48:281077 if (total < max_sockets_)
1078 return false;
[email protected]c901f6d2010-04-27 17:48:281079 return true;
[email protected]211d2172009-07-22 15:48:531080}
1081
[email protected]51fdc7c2012-04-10 19:19:481082bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1083 if (idle_socket_count() == 0)
1084 return false;
1085 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461086}
1087
1088bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1089 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541090 CHECK_GT(idle_socket_count(), 0);
1091
1092 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321093 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461094 if (exception_group == group)
1095 continue;
[email protected]e1b54dc2010-10-06 21:27:221096 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541097
[email protected]e1b54dc2010-10-06 21:27:221098 if (!idle_sockets->empty()) {
1099 delete idle_sockets->front().socket;
1100 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541101 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321102 if (group->IsEmpty())
1103 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541104
[email protected]dcbe168a2010-12-02 03:14:461105 return true;
[email protected]43a21b82010-06-10 21:30:541106 }
1107 }
1108
[email protected]51fdc7c2012-04-10 19:19:481109 return false;
1110}
[email protected]dcbe168a2010-12-02 03:14:461111
[email protected]043b68c82013-08-22 23:41:521112bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInHigherLayeredPool() {
[email protected]51fdc7c2012-04-10 19:19:481113 // This pool doesn't have any idle sockets. It's possible that a pool at a
1114 // higher layer is holding one of this sockets active, but it's actually idle.
1115 // Query the higher layers.
[email protected]043b68c82013-08-22 23:41:521116 for (std::set<HigherLayeredPool*>::const_iterator it = higher_pools_.begin();
1117 it != higher_pools_.end(); ++it) {
[email protected]51fdc7c2012-04-10 19:19:481118 if ((*it)->CloseOneIdleConnection())
1119 return true;
1120 }
[email protected]dcbe168a2010-12-02 03:14:461121 return false;
[email protected]43a21b82010-06-10 21:30:541122}
1123
[email protected]05ea9ff2010-07-15 19:08:211124void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411125 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211126 CHECK(!ContainsKey(pending_callback_map_, handle));
1127 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
[email protected]2da659e2013-05-23 20:51:341128 base::MessageLoop::current()->PostTask(
[email protected]05ea9ff2010-07-15 19:08:211129 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411130 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1131 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211132}
1133
1134void ClientSocketPoolBaseHelper::InvokeUserCallback(
1135 ClientSocketHandle* handle) {
1136 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1137
1138 // Exit if the request has already been cancelled.
1139 if (it == pending_callback_map_.end())
1140 return;
1141
1142 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411143 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211144 int result = it->second.result;
1145 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411146 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211147}
1148
[email protected]58e562f2013-04-22 17:32:201149void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() {
1150 while (IsStalled()) {
1151 // Closing a socket will result in calling back into |this| to use the freed
1152 // socket slot, so nothing else is needed.
[email protected]043b68c82013-08-22 23:41:521153 if (!CloseOneIdleConnectionInHigherLayeredPool())
[email protected]58e562f2013-04-22 17:32:201154 return;
1155 }
1156}
1157
[email protected]aed99ef02010-08-26 14:04:321158ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101159 : unassigned_job_count_(0),
1160 active_socket_count_(0),
[email protected]aa249b52013-04-30 01:04:321161 weak_factory_(this) {}
[email protected]aed99ef02010-08-26 14:04:321162
[email protected]e4d9a9722011-05-12 00:16:001163ClientSocketPoolBaseHelper::Group::~Group() {
1164 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101165 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321166}
1167
1168void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1169 const std::string& group_name,
1170 ClientSocketPoolBaseHelper* pool) {
1171 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411172 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321173 return;
1174
[email protected]2da659e2013-05-23 20:51:341175 base::MessageLoop::current()->PostDelayedTask(
[email protected]aed99ef02010-08-26 14:04:321176 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411177 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1178 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001179 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321180}
1181
[email protected]8159a1c2012-06-07 00:00:101182bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1183 SanityCheck();
1184
1185 if (unassigned_job_count_ == 0)
1186 return false;
1187 --unassigned_job_count_;
1188 return true;
1189}
1190
[email protected]18ccfdb2013-08-15 00:13:441191void ClientSocketPoolBaseHelper::Group::AddJob(scoped_ptr<ConnectJob> job,
[email protected]8159a1c2012-06-07 00:00:101192 bool is_preconnect) {
1193 SanityCheck();
1194
1195 if (is_preconnect)
1196 ++unassigned_job_count_;
[email protected]18ccfdb2013-08-15 00:13:441197 jobs_.insert(job.release());
[email protected]8159a1c2012-06-07 00:00:101198}
1199
1200void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
[email protected]ee6e0b62013-08-14 21:55:421201 scoped_ptr<ConnectJob> owned_job(job);
[email protected]8159a1c2012-06-07 00:00:101202 SanityCheck();
1203
[email protected]ee6e0b62013-08-14 21:55:421204 std::set<ConnectJob*>::iterator it = jobs_.find(job);
1205 if (it != jobs_.end()) {
1206 jobs_.erase(it);
1207 } else {
1208 NOTREACHED();
1209 }
[email protected]8159a1c2012-06-07 00:00:101210 size_t job_count = jobs_.size();
1211 if (job_count < unassigned_job_count_)
1212 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031213}
1214
[email protected]aed99ef02010-08-26 14:04:321215void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1216 std::string group_name,
1217 ClientSocketPoolBaseHelper* pool) {
1218 // If there are no more jobs pending, there is no work to do.
1219 // If we've done our cleanups correctly, this should not happen.
1220 if (jobs_.empty()) {
1221 NOTREACHED();
1222 return;
1223 }
1224
[email protected]8159a1c2012-06-07 00:00:101225 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321226 // right now due to limits, just reset the timer.
1227 if (pool->ReachedMaxSocketsLimit() ||
1228 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1229 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1230 StartBackupSocketTimer(group_name, pool);
1231 return;
1232 }
1233
[email protected]a9fc8fc2011-05-10 02:41:071234 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441235 return;
[email protected]4baaf9d2010-08-31 15:15:441236
[email protected]18ccfdb2013-08-15 00:13:441237 scoped_ptr<ConnectJob> backup_job =
1238 pool->connect_job_factory_->NewConnectJob(
1239 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311240 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321241 SIMPLE_STATS_COUNTER("socket.backup_created");
1242 int rv = backup_job->Connect();
1243 pool->connecting_socket_count_++;
[email protected]18ccfdb2013-08-15 00:13:441244 ConnectJob* raw_backup_job = backup_job.get();
1245 AddJob(backup_job.Pass(), false);
[email protected]aed99ef02010-08-26 14:04:321246 if (rv != ERR_IO_PENDING)
[email protected]18ccfdb2013-08-15 00:13:441247 pool->OnConnectJobComplete(rv, raw_backup_job);
[email protected]aed99ef02010-08-26 14:04:321248}
1249
[email protected]8159a1c2012-06-07 00:00:101250void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1251 DCHECK_LE(unassigned_job_count_, jobs_.size());
1252}
1253
[email protected]aed99ef02010-08-26 14:04:321254void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101255 SanityCheck();
1256
[email protected]aed99ef02010-08-26 14:04:321257 // Delete active jobs.
1258 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101259 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321260
1261 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411262 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321263}
1264
[email protected]b74d4bd2013-08-23 00:13:361265const ClientSocketPoolBaseHelper::Request*
1266ClientSocketPoolBaseHelper::Group::GetNextPendingRequest() const {
1267 return pending_requests_.empty() ? NULL : *pending_requests_.begin();
1268}
1269
1270bool ClientSocketPoolBaseHelper::Group::HasConnectJobForHandle(
1271 const ClientSocketHandle* handle) const {
1272 // Search the first |jobs_.size()| pending requests for |handle|.
1273 // If it's farther back in the deque than that, it doesn't have a
1274 // corresponding ConnectJob.
1275 size_t i = 0;
1276 for (RequestQueue::const_iterator it = pending_requests_.begin();
1277 it != pending_requests_.end() && i < jobs_.size(); ++it, ++i) {
1278 if ((*it)->handle() == handle)
1279 return true;
1280 }
1281 return false;
1282}
1283
1284void ClientSocketPoolBaseHelper::Group::InsertPendingRequest(
1285 scoped_ptr<const Request> r) {
1286 RequestQueue::iterator it = pending_requests_.begin();
1287 // TODO(mmenke): Should the network stack require requests with
1288 // |ignore_limits| have the highest priority?
1289 while (it != pending_requests_.end() &&
1290 CompareEffectiveRequestPriority(*r, *(*it)) <= 0) {
1291 ++it;
1292 }
1293 pending_requests_.insert(it, r.release());
1294}
1295
1296scoped_ptr<const ClientSocketPoolBaseHelper::Request>
1297ClientSocketPoolBaseHelper::Group::PopNextPendingRequest() {
1298 if (pending_requests_.empty())
1299 return scoped_ptr<const ClientSocketPoolBaseHelper::Request>();
1300 return RemovePendingRequest(pending_requests_.begin());
1301}
1302
1303scoped_ptr<const ClientSocketPoolBaseHelper::Request>
1304ClientSocketPoolBaseHelper::Group::FindAndRemovePendingRequest(
1305 ClientSocketHandle* handle) {
1306 for (RequestQueue::iterator it = pending_requests_.begin();
1307 it != pending_requests_.end(); ++it) {
1308 if ((*it)->handle() == handle) {
1309 scoped_ptr<const Request> request = RemovePendingRequest(it);
1310 return request.Pass();
1311 }
1312 }
1313 return scoped_ptr<const ClientSocketPoolBaseHelper::Request>();
1314}
1315
1316scoped_ptr<const ClientSocketPoolBaseHelper::Request>
1317ClientSocketPoolBaseHelper::Group::RemovePendingRequest(
1318 const RequestQueue::iterator& it) {
1319 scoped_ptr<const Request> request(*it);
1320 pending_requests_.erase(it);
1321 // If there are no more requests, kill the backup timer.
1322 if (pending_requests_.empty())
1323 CleanupBackupJob();
1324 return request.Pass();
1325}
1326
[email protected]d80a4322009-08-14 07:07:491327} // namespace internal
1328
[email protected]ff579d42009-06-24 15:47:021329} // namespace net