blob: 3c9023a09fd4a1433cc2116059c02e36b6905d6b [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]ff579d42009-06-24 15:47:02227// static
[email protected]d80a4322009-08-14 07:07:49228void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
229 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02230 RequestQueue::iterator it = pending_requests->begin();
[email protected]b021ece62013-06-11 11:06:33231 // TODO(mmenke): Should the network stack require requests with
232 // |ignore_limits| have the highest priority?
233 while (it != pending_requests->end() &&
234 CompareEffectiveRequestPriority(*r, *(*it)) <= 0) {
[email protected]ff579d42009-06-24 15:47:02235 ++it;
[email protected]b021ece62013-06-11 11:06:33236 }
[email protected]ff579d42009-06-24 15:47:02237 pending_requests->insert(it, r);
238}
239
[email protected]fd7b7c92009-08-20 19:38:30240// static
241const ClientSocketPoolBaseHelper::Request*
242ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05243 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30244 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02245 group->mutable_pending_requests()->erase(it);
246 // If there are no more requests, we kill the backup timer.
247 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00248 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30249 return req;
250}
251
[email protected]043b68c82013-08-22 23:41:52252bool ClientSocketPoolBaseHelper::IsStalled() const {
253 // If a lower layer pool is stalled, consider |this| stalled as well.
254 for (std::set<LowerLayeredPool*>::const_iterator it = lower_pools_.begin();
255 it != lower_pools_.end();
256 ++it) {
257 if ((*it)->IsStalled())
258 return true;
259 }
260
261 // If fewer than |max_sockets_| are in use, then clearly |this| is not
262 // stalled.
263 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
264 return false;
265 // So in order to be stalled, |this| must be using at least |max_sockets_| AND
266 // |this| must have a request that is actually stalled on the global socket
267 // limit. To find such a request, look for a group that has more requests
268 // than jobs AND where the number of sockets is less than
269 // |max_sockets_per_group_|. (If the number of sockets is equal to
270 // |max_sockets_per_group_|, then the request is stalled on the group limit,
271 // which does not count.)
272 for (GroupMap::const_iterator it = group_map_.begin();
273 it != group_map_.end(); ++it) {
274 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
275 return true;
276 }
277 return false;
[email protected]51fdc7c2012-04-10 19:19:48278}
279
[email protected]043b68c82013-08-22 23:41:52280void ClientSocketPoolBaseHelper::AddLowerLayeredPool(
281 LowerLayeredPool* lower_pool) {
282 DCHECK(pool_);
283 CHECK(!ContainsKey(lower_pools_, lower_pool));
284 lower_pools_.insert(lower_pool);
285 lower_pool->AddHigherLayeredPool(pool_);
286}
287
288void ClientSocketPoolBaseHelper::AddHigherLayeredPool(
289 HigherLayeredPool* higher_pool) {
290 CHECK(higher_pool);
291 CHECK(!ContainsKey(higher_pools_, higher_pool));
292 higher_pools_.insert(higher_pool);
293}
294
295void ClientSocketPoolBaseHelper::RemoveHigherLayeredPool(
296 HigherLayeredPool* higher_pool) {
297 CHECK(higher_pool);
298 CHECK(ContainsKey(higher_pools_, higher_pool));
299 higher_pools_.erase(higher_pool);
[email protected]51fdc7c2012-04-10 19:19:48300}
301
[email protected]d80a4322009-08-14 07:07:49302int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02303 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49304 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41305 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03306 CHECK(request->handle());
307
[email protected]64770b7d2011-11-16 04:30:41308 // Cleanup any timed-out idle sockets if no timer is used.
309 if (!use_cleanup_timer_)
310 CleanupIdleSockets(false);
311
[email protected]3aa4af042012-06-14 21:02:31312 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32313 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26314
[email protected]fd4fe0b2010-02-08 23:02:15315 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17316 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43317 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21318 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17319 delete request;
320 } else {
[email protected]aed99ef02010-08-26 14:04:32321 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]58e562f2013-04-22 17:32:20322 // Have to do this asynchronously, as closing sockets in higher level pools
323 // call back in to |this|, which will cause all sorts of fun and exciting
324 // re-entrancy issues if the socket pool is doing something else at the
325 // time.
326 if (group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
[email protected]2da659e2013-05-23 20:51:34327 base::MessageLoop::current()->PostTask(
[email protected]58e562f2013-04-22 17:32:20328 FROM_HERE,
329 base::Bind(
330 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
331 weak_factory_.GetWeakPtr()));
332 }
[email protected]e7e99322010-05-04 23:30:17333 }
[email protected]fd4fe0b2010-02-08 23:02:15334 return rv;
335}
336
[email protected]2c2bef152010-10-13 00:55:03337void ClientSocketPoolBaseHelper::RequestSockets(
338 const std::string& group_name,
339 const Request& request,
340 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41341 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03342 DCHECK(!request.handle());
343
[email protected]64770b7d2011-11-16 04:30:41344 // Cleanup any timed out idle sockets if no timer is used.
345 if (!use_cleanup_timer_)
346 CleanupIdleSockets(false);
347
[email protected]2c2bef152010-10-13 00:55:03348 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03349 num_sockets = max_sockets_per_group_;
350 }
351
352 request.net_log().BeginEvent(
353 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31354 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03355
356 Group* group = GetOrCreateGroup(group_name);
357
[email protected]3c819f522010-12-02 02:03:12358 // RequestSocketsInternal() may delete the group.
359 bool deleted_group = false;
360
[email protected]d7fd1782011-02-08 19:16:43361 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03362 for (int num_iterations_left = num_sockets;
363 group->NumActiveSocketSlots() < num_sockets &&
364 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43365 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03366 if (rv < 0 && rv != ERR_IO_PENDING) {
367 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12368 if (!ContainsKey(group_map_, group_name))
369 deleted_group = true;
370 break;
371 }
372 if (!ContainsKey(group_map_, group_name)) {
373 // Unexpected. The group should only be getting deleted on synchronous
374 // error.
375 NOTREACHED();
376 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03377 break;
378 }
379 }
380
[email protected]3c819f522010-12-02 02:03:12381 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03382 RemoveGroup(group_name);
383
[email protected]d7fd1782011-02-08 19:16:43384 if (rv == ERR_IO_PENDING)
385 rv = OK;
386 request.net_log().EndEventWithNetErrorCode(
387 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03388}
389
[email protected]fd4fe0b2010-02-08 23:02:15390int ClientSocketPoolBaseHelper::RequestSocketInternal(
391 const std::string& group_name,
392 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49393 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03394 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32395 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02396
[email protected]2c2bef152010-10-13 00:55:03397 if (!(request->flags() & NO_IDLE_SOCKETS)) {
398 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10399 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03400 return OK;
401 }
402
[email protected]8159a1c2012-06-07 00:00:10403 // If there are more ConnectJobs than pending requests, don't need to do
404 // anything. Can just wait for the extra job to connect, and then assign it
405 // to the request.
406 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03407 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10408
[email protected]43a21b82010-06-10 21:30:54409 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20410 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
411 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48412 // TODO(willchan): Consider whether or not we need to close a socket in a
413 // higher layered group. I don't think this makes sense since we would just
414 // reuse that socket then if we needed one and wouldn't make it down to this
415 // layer.
[email protected]43a21b82010-06-10 21:30:54416 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31417 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54418 return ERR_IO_PENDING;
419 }
420
[email protected]5acdce12011-03-30 13:00:20421 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48422 // NOTE(mmenke): Wonder if we really need different code for each case
423 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54424 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48425 // There's an idle socket in this pool. Either that's because there's
426 // still one in this group, but we got here due to preconnecting bypassing
427 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46428 bool closed = CloseOneIdleSocketExceptInGroup(group);
429 if (preconnecting && !closed)
430 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54431 } else {
[email protected]40dee91a2012-04-17 19:25:46432 // We could check if we really have a stalled group here, but it requires
433 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31434 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46435 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54436 }
437 }
438
[email protected]51fdc7c2012-04-10 19:19:48439 // We couldn't find a socket to reuse, and there's space to allocate one,
440 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58441 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17442 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02443
[email protected]2ab05b52009-07-01 23:57:58444 int rv = connect_job->Connect();
445 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17446 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03447 if (!preconnecting) {
[email protected]18ccfdb2013-08-15 00:13:44448 HandOutSocket(connect_job->PassSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48449 connect_job->connect_timing(), handle, base::TimeDelta(),
450 group, request->net_log());
[email protected]2c2bef152010-10-13 00:55:03451 } else {
[email protected]18ccfdb2013-08-15 00:13:44452 AddIdleSocket(connect_job->PassSocket(), group);
[email protected]2c2bef152010-10-13 00:55:03453 }
[email protected]2ab05b52009-07-01 23:57:58454 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32455 // If we don't have any sockets in this group, set a timer for potentially
456 // creating a new one. If the SYN is lost, this backup socket may complete
457 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03458 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00459 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32460 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03461 }
[email protected]6b624c62010-03-14 08:37:32462
[email protected]211d2172009-07-22 15:48:53463 connecting_socket_count_++;
464
[email protected]18ccfdb2013-08-15 00:13:44465 group->AddJob(connect_job.Pass(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02466 } else {
[email protected]06650c52010-06-03 00:49:17467 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]18ccfdb2013-08-15 00:13:44468 scoped_ptr<StreamSocket> error_socket;
[email protected]fd2e53e2011-01-14 20:40:52469 if (!preconnecting) {
470 DCHECK(handle);
471 connect_job->GetAdditionalErrorState(handle);
[email protected]18ccfdb2013-08-15 00:13:44472 error_socket = connect_job->PassSocket();
[email protected]fd2e53e2011-01-14 20:40:52473 }
[email protected]e772db3f2010-07-12 18:11:13474 if (error_socket) {
[email protected]18ccfdb2013-08-15 00:13:44475 HandOutSocket(error_socket.Pass(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48476 connect_job->connect_timing(), handle, base::TimeDelta(),
477 group, request->net_log());
[email protected]aed99ef02010-08-26 14:04:32478 } else if (group->IsEmpty()) {
479 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21480 }
[email protected]2ab05b52009-07-01 23:57:58481 }
[email protected]ff579d42009-06-24 15:47:02482
[email protected]2ab05b52009-07-01 23:57:58483 return rv;
[email protected]ff579d42009-06-24 15:47:02484}
485
[email protected]8159a1c2012-06-07 00:00:10486bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32487 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22488 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
489 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
490
491 // Iterate through the idle sockets forwards (oldest to newest)
492 // * Delete any disconnected ones.
493 // * If we find a used idle socket, assign to |idle_socket|. At the end,
494 // the |idle_socket_it| will be set to the newest used idle socket.
495 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
496 it != idle_sockets->end();) {
497 if (!it->socket->IsConnectedAndIdle()) {
498 DecrementIdleCount();
499 delete it->socket;
500 it = idle_sockets->erase(it);
501 continue;
[email protected]eb5a99382010-07-11 03:18:26502 }
[email protected]e1b54dc2010-10-06 21:27:22503
504 if (it->socket->WasEverUsed()) {
505 // We found one we can reuse!
[email protected]e86df8dc2013-03-30 13:18:28506 idle_socket_it = it;
[email protected]e1b54dc2010-10-06 21:27:22507 }
508
509 ++it;
[email protected]eb5a99382010-07-11 03:18:26510 }
[email protected]e1b54dc2010-10-06 21:27:22511
512 // If we haven't found an idle socket, that means there are no used idle
513 // sockets. Pick the oldest (first) idle socket (FIFO).
514
515 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
516 idle_socket_it = idle_sockets->begin();
517
518 if (idle_socket_it != idle_sockets->end()) {
519 DecrementIdleCount();
520 base::TimeDelta idle_time =
521 base::TimeTicks::Now() - idle_socket_it->start_time;
522 IdleSocket idle_socket = *idle_socket_it;
523 idle_sockets->erase(idle_socket_it);
524 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44525 scoped_ptr<StreamSocket>(idle_socket.socket),
[email protected]e1b54dc2010-10-06 21:27:22526 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48527 LoadTimingInfo::ConnectTiming(),
[email protected]e1b54dc2010-10-06 21:27:22528 request->handle(),
529 idle_time,
530 group,
531 request->net_log());
532 return true;
533 }
534
[email protected]eb5a99382010-07-11 03:18:26535 return false;
536}
537
[email protected]06650c52010-06-03 00:49:17538// static
539void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
540 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31541 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
542 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17543}
544
[email protected]d80a4322009-08-14 07:07:49545void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21546 const std::string& group_name, ClientSocketHandle* handle) {
547 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
548 if (callback_it != pending_callback_map_.end()) {
549 int result = callback_it->second.result;
550 pending_callback_map_.erase(callback_it);
[email protected]18ccfdb2013-08-15 00:13:44551 scoped_ptr<StreamSocket> socket = handle->PassSocket();
[email protected]05ea9ff2010-07-15 19:08:21552 if (socket) {
553 if (result != OK)
554 socket->Disconnect();
[email protected]18ccfdb2013-08-15 00:13:44555 ReleaseSocket(handle->group_name(), socket.Pass(), handle->id());
[email protected]05ea9ff2010-07-15 19:08:21556 }
557 return;
558 }
[email protected]b6501d3d2010-06-03 23:53:34559
[email protected]ff579d42009-06-24 15:47:02560 CHECK(ContainsKey(group_map_, group_name));
561
[email protected]aed99ef02010-08-26 14:04:32562 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02563
[email protected]ff579d42009-06-24 15:47:02564 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32565 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
566 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49567 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03568 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31569 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
570 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26571
[email protected]b021ece62013-06-11 11:06:33572 // We let the job run, unless we're at the socket limit and there is
573 // not another request waiting on the job.
574 if (group->jobs().size() > group->pending_requests().size() &&
575 ReachedMaxSocketsLimit()) {
[email protected]aed99ef02010-08-26 14:04:32576 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26577 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34578 }
[email protected]eb5a99382010-07-11 03:18:26579 break;
[email protected]ff579d42009-06-24 15:47:02580 }
581 }
[email protected]ff579d42009-06-24 15:47:02582}
583
[email protected]2abfe90a2010-08-25 17:49:51584bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
585 return ContainsKey(group_map_, group_name);
586}
587
[email protected]d80a4322009-08-14 07:07:49588void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02589 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14590 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02591}
592
[email protected]d80a4322009-08-14 07:07:49593int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02594 const std::string& group_name) const {
595 GroupMap::const_iterator i = group_map_.find(group_name);
596 CHECK(i != group_map_.end());
597
[email protected]aed99ef02010-08-26 14:04:32598 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02599}
600
[email protected]d80a4322009-08-14 07:07:49601LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02602 const std::string& group_name,
603 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21604 if (ContainsKey(pending_callback_map_, handle))
605 return LOAD_STATE_CONNECTING;
606
[email protected]ff579d42009-06-24 15:47:02607 if (!ContainsKey(group_map_, group_name)) {
608 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
609 << " for handle: " << handle;
610 return LOAD_STATE_IDLE;
611 }
612
613 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32614 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02615
[email protected]03b7c8c2013-07-20 04:38:55616 // Search the first group.jobs().size() |pending_requests| for |handle|.
617 // If it's farther back in the deque than that, it doesn't have a
618 // corresponding ConnectJob.
619 size_t connect_jobs = group.jobs().size();
[email protected]aed99ef02010-08-26 14:04:32620 RequestQueue::const_iterator it = group.pending_requests().begin();
[email protected]03b7c8c2013-07-20 04:38:55621 for (size_t i = 0; it != group.pending_requests().end() && i < connect_jobs;
622 ++it, ++i) {
623 if ((*it)->handle() != handle)
624 continue;
625
626 // Just return the state of the farthest along ConnectJob for the first
627 // group.jobs().size() pending requests.
628 LoadState max_state = LOAD_STATE_IDLE;
629 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
630 job_it != group.jobs().end(); ++job_it) {
631 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]ff579d42009-06-24 15:47:02632 }
[email protected]03b7c8c2013-07-20 04:38:55633 return max_state;
[email protected]ff579d42009-06-24 15:47:02634 }
635
[email protected]03b7c8c2013-07-20 04:38:55636 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_))
637 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL;
638 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET;
[email protected]ff579d42009-06-24 15:47:02639}
640
[email protected]10833352013-02-12 19:39:50641base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27642 const std::string& name, const std::string& type) const {
[email protected]10833352013-02-12 19:39:50643 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27644 dict->SetString("name", name);
645 dict->SetString("type", type);
646 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
647 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
648 dict->SetInteger("idle_socket_count", idle_socket_count_);
649 dict->SetInteger("max_socket_count", max_sockets_);
650 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
651 dict->SetInteger("pool_generation_number", pool_generation_number_);
652
653 if (group_map_.empty())
654 return dict;
655
[email protected]10833352013-02-12 19:39:50656 base::DictionaryValue* all_groups_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27657 for (GroupMap::const_iterator it = group_map_.begin();
658 it != group_map_.end(); it++) {
659 const Group* group = it->second;
[email protected]10833352013-02-12 19:39:50660 base::DictionaryValue* group_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27661
662 group_dict->SetInteger("pending_request_count",
663 group->pending_requests().size());
664 if (!group->pending_requests().empty()) {
665 group_dict->SetInteger("top_pending_priority",
666 group->TopPendingPriority());
667 }
668
669 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07670
[email protected]10833352013-02-12 19:39:50671 base::ListValue* idle_socket_list = new base::ListValue();
[email protected]e1b54dc2010-10-06 21:27:22672 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07673 for (idle_socket = group->idle_sockets().begin();
674 idle_socket != group->idle_sockets().end();
675 idle_socket++) {
676 int source_id = idle_socket->socket->NetLog().source().id;
[email protected]10833352013-02-12 19:39:50677 idle_socket_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07678 }
679 group_dict->Set("idle_sockets", idle_socket_list);
680
[email protected]10833352013-02-12 19:39:50681 base::ListValue* connect_jobs_list = new base::ListValue();
[email protected]2c2bef152010-10-13 00:55:03682 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07683 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
684 int source_id = (*job)->net_log().source().id;
[email protected]10833352013-02-12 19:39:50685 connect_jobs_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07686 }
687 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27688
689 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48690 group->IsStalledOnPoolMaxSockets(
691 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00692 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27693
694 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
695 }
696 dict->Set("groups", all_groups_dict);
697 return dict;
698}
699
[email protected]d80a4322009-08-14 07:07:49700bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16701 base::TimeTicks now,
702 base::TimeDelta timeout) const {
703 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01704 if (timed_out)
705 return true;
706 if (socket->WasEverUsed())
707 return !socket->IsConnectedAndIdle();
708 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02709}
710
[email protected]d80a4322009-08-14 07:07:49711void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02712 if (idle_socket_count_ == 0)
713 return;
714
715 // Current time value. Retrieving it once at the function start rather than
716 // inside the inner loop, since it shouldn't change by any meaningful amount.
717 base::TimeTicks now = base::TimeTicks::Now();
718
719 GroupMap::iterator i = group_map_.begin();
720 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32721 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02722
[email protected]e1b54dc2010-10-06 21:27:22723 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32724 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16725 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01726 j->socket->WasEverUsed() ?
727 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16728 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02729 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32730 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02731 DecrementIdleCount();
732 } else {
733 ++j;
734 }
735 }
736
737 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32738 if (group->IsEmpty()) {
739 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02740 } else {
741 ++i;
742 }
743 }
744}
745
[email protected]aed99ef02010-08-26 14:04:32746ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
747 const std::string& group_name) {
748 GroupMap::iterator it = group_map_.find(group_name);
749 if (it != group_map_.end())
750 return it->second;
751 Group* group = new Group;
752 group_map_[group_name] = group;
753 return group;
754}
755
756void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
757 GroupMap::iterator it = group_map_.find(group_name);
758 CHECK(it != group_map_.end());
759
760 RemoveGroup(it);
761}
762
763void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
764 delete it->second;
765 group_map_.erase(it);
766}
767
[email protected]06d94042010-08-25 01:45:22768// static
[email protected]2d6728692011-03-12 01:39:55769bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
770 return g_connect_backup_jobs_enabled;
771}
772
773// static
[email protected]636b8252011-04-08 19:56:54774bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
775 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22776 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54777 return old_value;
[email protected]06d94042010-08-25 01:45:22778}
779
780void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
781 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
782}
783
[email protected]d80a4322009-08-14 07:07:49784void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41785 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
786 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02787}
788
[email protected]d80a4322009-08-14 07:07:49789void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02790 if (--idle_socket_count_ == 0)
791 timer_.Stop();
792}
793
[email protected]64770b7d2011-11-16 04:30:41794// static
795bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
796 return g_cleanup_timer_enabled;
797}
798
799// static
800bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
801 bool old_value = g_cleanup_timer_enabled;
802 g_cleanup_timer_enabled = enabled;
803 return old_value;
804}
805
806void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
807 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
808 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
809}
810
[email protected]eb5a99382010-07-11 03:18:26811void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]18ccfdb2013-08-15 00:13:44812 scoped_ptr<StreamSocket> socket,
[email protected]eb5a99382010-07-11 03:18:26813 int id) {
[email protected]ff579d42009-06-24 15:47:02814 GroupMap::iterator i = group_map_.find(group_name);
815 CHECK(i != group_map_.end());
816
[email protected]aed99ef02010-08-26 14:04:32817 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02818
[email protected]b1f031dd2010-03-02 23:19:33819 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53820 handed_out_socket_count_--;
821
[email protected]aed99ef02010-08-26 14:04:32822 CHECK_GT(group->active_socket_count(), 0);
823 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02824
[email protected]a7e38572010-06-07 18:22:24825 const bool can_reuse = socket->IsConnectedAndIdle() &&
826 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02827 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26828 // Add it to the idle list.
[email protected]18ccfdb2013-08-15 00:13:44829 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32830 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02831 } else {
[email protected]18ccfdb2013-08-15 00:13:44832 socket.reset();
[email protected]ff579d42009-06-24 15:47:02833 }
[email protected]05ea9ff2010-07-15 19:08:21834
[email protected]eb5a99382010-07-11 03:18:26835 CheckForStalledSocketGroups();
836}
[email protected]ff579d42009-06-24 15:47:02837
[email protected]eb5a99382010-07-11 03:18:26838void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
839 // If we have idle sockets, see if we can give one to the top-stalled group.
840 std::string top_group_name;
841 Group* top_group = NULL;
[email protected]043b68c82013-08-22 23:41:52842 if (!FindTopStalledGroup(&top_group, &top_group_name)) {
843 // There may still be a stalled group in a lower level pool.
844 for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin();
845 it != lower_pools_.end();
846 ++it) {
847 if ((*it)->IsStalled()) {
848 CloseOneIdleSocket();
849 break;
850 }
851 }
[email protected]eb5a99382010-07-11 03:18:26852 return;
[email protected]043b68c82013-08-22 23:41:52853 }
[email protected]4f2abec2010-02-03 18:10:16854
[email protected]eb5a99382010-07-11 03:18:26855 if (ReachedMaxSocketsLimit()) {
856 if (idle_socket_count() > 0) {
857 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54858 } else {
[email protected]eb5a99382010-07-11 03:18:26859 // We can't activate more sockets since we're already at our global
860 // limit.
[email protected]4f2abec2010-02-03 18:10:16861 return;
[email protected]d7027bb2010-05-10 18:58:54862 }
[email protected]4f2abec2010-02-03 18:10:16863 }
[email protected]eb5a99382010-07-11 03:18:26864
865 // Note: we don't loop on waking stalled groups. If the stalled group is at
866 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21867 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26868 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21869 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02870}
871
[email protected]211d2172009-07-22 15:48:53872// Search for the highest priority pending request, amongst the groups that
873// are not at the |max_sockets_per_group_| limit. Note: for requests with
874// the same priority, the winner is based on group hash ordering (and not
875// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48876bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
877 Group** group,
878 std::string* group_name) const {
879 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53880 Group* top_group = NULL;
881 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21882 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48883 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53884 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32885 Group* curr_group = i->second;
886 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53887 if (queue.empty())
888 continue;
[email protected]51fdc7c2012-04-10 19:19:48889 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
890 if (!group)
891 return true;
[email protected]05ea9ff2010-07-15 19:08:21892 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41893 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05894 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41895 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32896 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41897 top_group_name = &i->first;
898 }
[email protected]211d2172009-07-22 15:48:53899 }
900 }
[email protected]05ea9ff2010-07-15 19:08:21901
[email protected]211d2172009-07-22 15:48:53902 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48903 CHECK(group);
[email protected]211d2172009-07-22 15:48:53904 *group = top_group;
905 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48906 } else {
907 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53908 }
[email protected]05ea9ff2010-07-15 19:08:21909 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53910}
911
[email protected]d80a4322009-08-14 07:07:49912void ClientSocketPoolBaseHelper::OnConnectJobComplete(
913 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58914 DCHECK_NE(ERR_IO_PENDING, result);
915 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02916 GroupMap::iterator group_it = group_map_.find(group_name);
917 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32918 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02919
[email protected]18ccfdb2013-08-15 00:13:44920 scoped_ptr<StreamSocket> socket = job->PassSocket();
[email protected]ff579d42009-06-24 15:47:02921
[email protected]034df0f32013-01-07 23:17:48922 // Copies of these are needed because |job| may be deleted before they are
923 // accessed.
[email protected]9e743cd2010-03-16 07:03:53924 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48925 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57926
[email protected]18ccfdb2013-08-15 00:13:44927 // RemoveConnectJob(job, _) must be called by all branches below;
928 // otherwise, |job| will be leaked.
929
[email protected]4d3b05d2010-01-27 21:27:29930 if (result == OK) {
931 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32932 RemoveConnectJob(job, group);
933 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29934 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02935 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17936 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29937 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44938 socket.Pass(), false /* unused socket */, connect_timing,
[email protected]034df0f32013-01-07 23:17:48939 r->handle(), base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31940 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21941 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57942 } else {
[email protected]18ccfdb2013-08-15 00:13:44943 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32944 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21945 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57946 }
[email protected]94c20472010-01-14 08:14:36947 } else {
[email protected]e772db3f2010-07-12 18:11:13948 // If we got a socket, it must contain error information so pass that
949 // up so that the caller can retrieve it.
950 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32951 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29952 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02953 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17954 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18955 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32956 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13957 if (socket.get()) {
958 handed_out_socket = true;
[email protected]18ccfdb2013-08-15 00:13:44959 HandOutSocket(socket.Pass(), false /* unused socket */,
[email protected]034df0f32013-01-07 23:17:48960 connect_timing, r->handle(), base::TimeDelta(), group,
961 r->net_log());
[email protected]e772db3f2010-07-12 18:11:13962 }
[email protected]034df0f32013-01-07 23:17:48963 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result);
[email protected]05ea9ff2010-07-15 19:08:21964 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18965 } else {
[email protected]aed99ef02010-08-26 14:04:32966 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29967 }
[email protected]05ea9ff2010-07-15 19:08:21968 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32969 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21970 CheckForStalledSocketGroups();
971 }
[email protected]ff579d42009-06-24 15:47:02972 }
[email protected]ff579d42009-06-24 15:47:02973}
974
[email protected]66761b952010-06-25 21:30:38975void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42976 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38977}
978
[email protected]7af985a2012-12-14 22:40:42979void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24980 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14981 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52982 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42983 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52984}
985
[email protected]2c2bef152010-10-13 00:55:03986void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29987 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33988 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53989 connecting_socket_count_--;
990
[email protected]25eea382010-07-10 23:55:26991 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32992 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26993
994 // If we've got no more jobs for this group, then we no longer need a
995 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32996 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00997 group->CleanupBackupJob();
[email protected]2ab05b52009-07-01 23:57:58998}
[email protected]ff579d42009-06-24 15:47:02999
[email protected]8ae03f42010-07-07 19:08:101000void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:211001 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:511002 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:211003 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:321004 RemoveGroup(group_name);
1005 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:511006 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:101007}
[email protected]06650c52010-06-03 00:49:171008
[email protected]8ae03f42010-07-07 19:08:101009void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:211010 const std::string& group_name, Group* group) {
1011 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:321012 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:211013 if (rv != ERR_IO_PENDING) {
1014 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:021015 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:511016 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:321017 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:101018
[email protected]d7fd1782011-02-08 19:16:431019 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:411020 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:581021 }
1022}
1023
[email protected]d80a4322009-08-14 07:07:491024void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:441025 scoped_ptr<StreamSocket> socket,
[email protected]2ab05b52009-07-01 23:57:581026 bool reused,
[email protected]034df0f32013-01-07 23:17:481027 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:581028 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:291029 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:151030 Group* group,
[email protected]9e743cd2010-03-16 07:03:531031 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:581032 DCHECK(socket);
[email protected]18ccfdb2013-08-15 00:13:441033 handle->SetSocket(socket.Pass());
[email protected]2ab05b52009-07-01 23:57:581034 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:291035 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:241036 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:481037 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:531038
[email protected]d13f51b2010-04-27 23:20:451039 if (reused) {
[email protected]ec11be62010-04-28 19:28:091040 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:451041 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311042 NetLog::IntegerCallback(
1043 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:151044 }
[email protected]d13f51b2010-04-27 23:20:451045
[email protected]18ccfdb2013-08-15 00:13:441046 net_log.AddEvent(
1047 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
1048 handle->socket()->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:151049
[email protected]211d2172009-07-22 15:48:531050 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321051 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021052}
1053
[email protected]d80a4322009-08-14 07:07:491054void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]18ccfdb2013-08-15 00:13:441055 scoped_ptr<StreamSocket> socket,
1056 Group* group) {
[email protected]5fc08e32009-07-15 17:09:571057 DCHECK(socket);
1058 IdleSocket idle_socket;
[email protected]18ccfdb2013-08-15 00:13:441059 idle_socket.socket = socket.release();
[email protected]5fc08e32009-07-15 17:09:571060 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571061
[email protected]aed99ef02010-08-26 14:04:321062 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571063 IncrementIdleCount();
1064}
1065
[email protected]d80a4322009-08-14 07:07:491066void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541067 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321068 Group* group = i->second;
1069 connecting_socket_count_ -= group->jobs().size();
1070 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321071
[email protected]5fc08e32009-07-15 17:09:571072 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321073 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141074 // RemoveGroup() will call .erase() which will invalidate the iterator,
1075 // but i will already have been incremented to a valid iterator before
1076 // RemoveGroup() is called.
1077 RemoveGroup(i++);
1078 } else {
1079 ++i;
1080 }
1081 }
1082 DCHECK_EQ(0, connecting_socket_count_);
1083}
1084
[email protected]7af985a2012-12-14 22:40:421085void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141086 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1087 Group* group = i->second;
1088
1089 RequestQueue pending_requests;
1090 pending_requests.swap(*group->mutable_pending_requests());
1091 for (RequestQueue::iterator it2 = pending_requests.begin();
1092 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031093 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141094 InvokeUserCallbackLater(
[email protected]7af985a2012-12-14 22:40:421095 request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141096 }
1097
1098 // Delete group if no longer needed.
1099 if (group->IsEmpty()) {
1100 // RemoveGroup() will call .erase() which will invalidate the iterator,
1101 // but i will already have been incremented to a valid iterator before
1102 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321103 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541104 } else {
1105 ++i;
[email protected]5fc08e32009-07-15 17:09:571106 }
1107 }
1108}
1109
[email protected]d80a4322009-08-14 07:07:491110bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531111 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541112 int total = handed_out_socket_count_ + connecting_socket_count_ +
1113 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201114 // There can be more sockets than the limit since some requests can ignore
1115 // the limit
[email protected]c901f6d2010-04-27 17:48:281116 if (total < max_sockets_)
1117 return false;
[email protected]c901f6d2010-04-27 17:48:281118 return true;
[email protected]211d2172009-07-22 15:48:531119}
1120
[email protected]51fdc7c2012-04-10 19:19:481121bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1122 if (idle_socket_count() == 0)
1123 return false;
1124 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461125}
1126
1127bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1128 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541129 CHECK_GT(idle_socket_count(), 0);
1130
1131 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321132 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461133 if (exception_group == group)
1134 continue;
[email protected]e1b54dc2010-10-06 21:27:221135 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541136
[email protected]e1b54dc2010-10-06 21:27:221137 if (!idle_sockets->empty()) {
1138 delete idle_sockets->front().socket;
1139 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541140 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321141 if (group->IsEmpty())
1142 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541143
[email protected]dcbe168a2010-12-02 03:14:461144 return true;
[email protected]43a21b82010-06-10 21:30:541145 }
1146 }
1147
[email protected]51fdc7c2012-04-10 19:19:481148 return false;
1149}
[email protected]dcbe168a2010-12-02 03:14:461150
[email protected]043b68c82013-08-22 23:41:521151bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInHigherLayeredPool() {
[email protected]51fdc7c2012-04-10 19:19:481152 // This pool doesn't have any idle sockets. It's possible that a pool at a
1153 // higher layer is holding one of this sockets active, but it's actually idle.
1154 // Query the higher layers.
[email protected]043b68c82013-08-22 23:41:521155 for (std::set<HigherLayeredPool*>::const_iterator it = higher_pools_.begin();
1156 it != higher_pools_.end(); ++it) {
[email protected]51fdc7c2012-04-10 19:19:481157 if ((*it)->CloseOneIdleConnection())
1158 return true;
1159 }
[email protected]dcbe168a2010-12-02 03:14:461160 return false;
[email protected]43a21b82010-06-10 21:30:541161}
1162
[email protected]05ea9ff2010-07-15 19:08:211163void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411164 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211165 CHECK(!ContainsKey(pending_callback_map_, handle));
1166 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
[email protected]2da659e2013-05-23 20:51:341167 base::MessageLoop::current()->PostTask(
[email protected]05ea9ff2010-07-15 19:08:211168 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411169 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1170 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211171}
1172
1173void ClientSocketPoolBaseHelper::InvokeUserCallback(
1174 ClientSocketHandle* handle) {
1175 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1176
1177 // Exit if the request has already been cancelled.
1178 if (it == pending_callback_map_.end())
1179 return;
1180
1181 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411182 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211183 int result = it->second.result;
1184 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411185 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211186}
1187
[email protected]58e562f2013-04-22 17:32:201188void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() {
1189 while (IsStalled()) {
1190 // Closing a socket will result in calling back into |this| to use the freed
1191 // socket slot, so nothing else is needed.
[email protected]043b68c82013-08-22 23:41:521192 if (!CloseOneIdleConnectionInHigherLayeredPool())
[email protected]58e562f2013-04-22 17:32:201193 return;
1194 }
1195}
1196
[email protected]aed99ef02010-08-26 14:04:321197ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101198 : unassigned_job_count_(0),
1199 active_socket_count_(0),
[email protected]aa249b52013-04-30 01:04:321200 weak_factory_(this) {}
[email protected]aed99ef02010-08-26 14:04:321201
[email protected]e4d9a9722011-05-12 00:16:001202ClientSocketPoolBaseHelper::Group::~Group() {
1203 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101204 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321205}
1206
1207void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1208 const std::string& group_name,
1209 ClientSocketPoolBaseHelper* pool) {
1210 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411211 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321212 return;
1213
[email protected]2da659e2013-05-23 20:51:341214 base::MessageLoop::current()->PostDelayedTask(
[email protected]aed99ef02010-08-26 14:04:321215 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411216 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1217 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001218 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321219}
1220
[email protected]8159a1c2012-06-07 00:00:101221bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1222 SanityCheck();
1223
1224 if (unassigned_job_count_ == 0)
1225 return false;
1226 --unassigned_job_count_;
1227 return true;
1228}
1229
[email protected]18ccfdb2013-08-15 00:13:441230void ClientSocketPoolBaseHelper::Group::AddJob(scoped_ptr<ConnectJob> job,
[email protected]8159a1c2012-06-07 00:00:101231 bool is_preconnect) {
1232 SanityCheck();
1233
1234 if (is_preconnect)
1235 ++unassigned_job_count_;
[email protected]18ccfdb2013-08-15 00:13:441236 jobs_.insert(job.release());
[email protected]8159a1c2012-06-07 00:00:101237}
1238
1239void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
[email protected]ee6e0b62013-08-14 21:55:421240 scoped_ptr<ConnectJob> owned_job(job);
[email protected]8159a1c2012-06-07 00:00:101241 SanityCheck();
1242
[email protected]ee6e0b62013-08-14 21:55:421243 std::set<ConnectJob*>::iterator it = jobs_.find(job);
1244 if (it != jobs_.end()) {
1245 jobs_.erase(it);
1246 } else {
1247 NOTREACHED();
1248 }
[email protected]8159a1c2012-06-07 00:00:101249 size_t job_count = jobs_.size();
1250 if (job_count < unassigned_job_count_)
1251 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031252}
1253
[email protected]aed99ef02010-08-26 14:04:321254void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1255 std::string group_name,
1256 ClientSocketPoolBaseHelper* pool) {
1257 // If there are no more jobs pending, there is no work to do.
1258 // If we've done our cleanups correctly, this should not happen.
1259 if (jobs_.empty()) {
1260 NOTREACHED();
1261 return;
1262 }
1263
[email protected]8159a1c2012-06-07 00:00:101264 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321265 // right now due to limits, just reset the timer.
1266 if (pool->ReachedMaxSocketsLimit() ||
1267 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1268 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1269 StartBackupSocketTimer(group_name, pool);
1270 return;
1271 }
1272
[email protected]a9fc8fc2011-05-10 02:41:071273 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441274 return;
[email protected]4baaf9d2010-08-31 15:15:441275
[email protected]18ccfdb2013-08-15 00:13:441276 scoped_ptr<ConnectJob> backup_job =
1277 pool->connect_job_factory_->NewConnectJob(
1278 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311279 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321280 SIMPLE_STATS_COUNTER("socket.backup_created");
1281 int rv = backup_job->Connect();
1282 pool->connecting_socket_count_++;
[email protected]18ccfdb2013-08-15 00:13:441283 ConnectJob* raw_backup_job = backup_job.get();
1284 AddJob(backup_job.Pass(), false);
[email protected]aed99ef02010-08-26 14:04:321285 if (rv != ERR_IO_PENDING)
[email protected]18ccfdb2013-08-15 00:13:441286 pool->OnConnectJobComplete(rv, raw_backup_job);
[email protected]aed99ef02010-08-26 14:04:321287}
1288
[email protected]8159a1c2012-06-07 00:00:101289void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1290 DCHECK_LE(unassigned_job_count_, jobs_.size());
1291}
1292
[email protected]aed99ef02010-08-26 14:04:321293void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101294 SanityCheck();
1295
[email protected]aed99ef02010-08-26 14:04:321296 // Delete active jobs.
1297 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101298 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321299
1300 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411301 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321302}
1303
[email protected]d80a4322009-08-14 07:07:491304} // namespace internal
1305
[email protected]ff579d42009-06-24 15:47:021306} // namespace net