blob: 3332e04a1714504147d8e89a0c76dc62c802e867 [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]fd7b7c92009-08-20 19:38:3068 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5369 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5870 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3471 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5872 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0273 net_log_(net_log),
[email protected]8159a1c2012-06-07 00:00:1074 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5875 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5876 DCHECK(delegate);
[email protected]e9d7d252013-01-04 02:33:1777 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB,
78 NetLog::StringCallback("group_name", &group_name_));
[email protected]2ab05b52009-07-01 23:57:5879}
80
[email protected]fd7b7c92009-08-20 19:38:3081ConnectJob::~ConnectJob() {
[email protected]3aa4af042012-06-14 21:02:3182 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]fd7b7c92009-08-20 19:38:3083}
[email protected]2ab05b52009-07-01 23:57:5884
[email protected]974ebd62009-08-03 23:14:3485int ConnectJob::Connect() {
86 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0287 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3088
[email protected]a2006ece2010-04-23 16:44:0289 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3090
[email protected]06650c52010-06-03 00:49:1791 LogConnectStart();
92
[email protected]fd7b7c92009-08-20 19:38:3093 int rv = ConnectInternal();
94
95 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:1796 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3097 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:3098 }
99
100 return rv;
101}
102
[email protected]3268023f2011-05-05 00:08:10103void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:17104 if (socket) {
[email protected]3aa4af042012-06-14 21:02:31105 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
106 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17107 }
108 socket_.reset(socket);
109}
110
[email protected]fd7b7c92009-08-20 19:38:30111void ConnectJob::NotifyDelegateOfCompletion(int rv) {
112 // The delegate will delete |this|.
[email protected]10833352013-02-12 19:39:50113 Delegate* delegate = delegate_;
[email protected]fd7b7c92009-08-20 19:38:30114 delegate_ = NULL;
115
[email protected]06650c52010-06-03 00:49:17116 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30117 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34118}
119
[email protected]a796bcec2010-03-22 17:17:26120void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
121 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02122 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26123}
124
[email protected]06650c52010-06-03 00:49:17125void ConnectJob::LogConnectStart() {
[email protected]034df0f32013-01-07 23:17:48126 connect_timing_.connect_start = base::TimeTicks::Now();
[email protected]e9d7d252013-01-04 02:33:17127 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT);
[email protected]06650c52010-06-03 00:49:17128}
129
130void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]034df0f32013-01-07 23:17:48131 connect_timing_.connect_end = base::TimeTicks::Now();
[email protected]d7fd1782011-02-08 19:16:43132 net_log().EndEventWithNetErrorCode(
133 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17134}
135
[email protected]974ebd62009-08-03 23:14:34136void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40137 // Make sure the socket is NULL before calling into |delegate|.
138 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30139
[email protected]3aa4af042012-06-14 21:02:31140 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30141
142 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34143}
144
[email protected]d80a4322009-08-14 07:07:49145namespace internal {
146
[email protected]fd4fe0b2010-02-08 23:02:15147ClientSocketPoolBaseHelper::Request::Request(
148 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41149 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15150 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20151 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03152 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53153 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13154 : handle_(handle),
155 callback_(callback),
156 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20157 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03158 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53159 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15160
161ClientSocketPoolBaseHelper::Request::~Request() {}
162
[email protected]d80a4322009-08-14 07:07:49163ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53164 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02165 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16166 base::TimeDelta unused_idle_socket_timeout,
167 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38168 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02169 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53170 connecting_socket_count_(0),
171 handed_out_socket_count_(0),
172 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02173 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41174 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16175 unused_idle_socket_timeout_(unused_idle_socket_timeout),
176 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35177 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22178 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13179 pool_generation_number_(0),
[email protected]aa249b52013-04-30 01:04:32180 weak_factory_(this) {
[email protected]211d2172009-07-22 15:48:53181 DCHECK_LE(0, max_sockets_per_group);
182 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52183
[email protected]232a5812011-03-04 22:42:08184 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53185}
[email protected]ff579d42009-06-24 15:47:02186
[email protected]d80a4322009-08-14 07:07:49187ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13188 // Clean up any idle sockets and pending connect jobs. Assert that we have no
189 // remaining active sockets or pending requests. They should have all been
190 // cleaned up prior to |this| being destroyed.
[email protected]7af985a2012-12-14 22:40:42191 FlushWithError(ERR_ABORTED);
[email protected]2431756e2010-09-29 20:26:13192 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21193 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29194 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48195 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52196
[email protected]232a5812011-03-04 22:42:08197 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02198}
199
[email protected]2a848e0e2012-08-09 22:27:31200ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
201 : result(OK) {
202}
203
204ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
205 const CompletionCallback& callback_in, int result_in)
206 : callback(callback_in),
207 result(result_in) {
208}
209
[email protected]49639fa2011-12-20 23:22:41210ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
211
[email protected]ff579d42009-06-24 15:47:02212// static
[email protected]d80a4322009-08-14 07:07:49213void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
214 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02215 RequestQueue::iterator it = pending_requests->begin();
[email protected]b021ece62013-06-11 11:06:33216 // TODO(mmenke): Should the network stack require requests with
217 // |ignore_limits| have the highest priority?
218 while (it != pending_requests->end() &&
219 CompareEffectiveRequestPriority(*r, *(*it)) <= 0) {
[email protected]ff579d42009-06-24 15:47:02220 ++it;
[email protected]b021ece62013-06-11 11:06:33221 }
[email protected]ff579d42009-06-24 15:47:02222 pending_requests->insert(it, r);
223}
224
[email protected]fd7b7c92009-08-20 19:38:30225// static
226const ClientSocketPoolBaseHelper::Request*
227ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05228 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30229 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02230 group->mutable_pending_requests()->erase(it);
231 // If there are no more requests, we kill the backup timer.
232 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00233 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30234 return req;
235}
236
[email protected]51fdc7c2012-04-10 19:19:48237void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
238 CHECK(pool);
239 CHECK(!ContainsKey(higher_layer_pools_, pool));
240 higher_layer_pools_.insert(pool);
241}
242
243void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
244 CHECK(pool);
245 CHECK(ContainsKey(higher_layer_pools_, pool));
246 higher_layer_pools_.erase(pool);
247}
248
[email protected]d80a4322009-08-14 07:07:49249int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02250 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49251 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41252 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03253 CHECK(request->handle());
254
[email protected]64770b7d2011-11-16 04:30:41255 // Cleanup any timed-out idle sockets if no timer is used.
256 if (!use_cleanup_timer_)
257 CleanupIdleSockets(false);
258
[email protected]3aa4af042012-06-14 21:02:31259 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32260 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26261
[email protected]fd4fe0b2010-02-08 23:02:15262 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17263 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43264 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21265 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17266 delete request;
267 } else {
[email protected]aed99ef02010-08-26 14:04:32268 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]58e562f2013-04-22 17:32:20269 // Have to do this asynchronously, as closing sockets in higher level pools
270 // call back in to |this|, which will cause all sorts of fun and exciting
271 // re-entrancy issues if the socket pool is doing something else at the
272 // time.
273 if (group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
[email protected]2da659e2013-05-23 20:51:34274 base::MessageLoop::current()->PostTask(
[email protected]58e562f2013-04-22 17:32:20275 FROM_HERE,
276 base::Bind(
277 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
278 weak_factory_.GetWeakPtr()));
279 }
[email protected]e7e99322010-05-04 23:30:17280 }
[email protected]fd4fe0b2010-02-08 23:02:15281 return rv;
282}
283
[email protected]2c2bef152010-10-13 00:55:03284void ClientSocketPoolBaseHelper::RequestSockets(
285 const std::string& group_name,
286 const Request& request,
287 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41288 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03289 DCHECK(!request.handle());
290
[email protected]64770b7d2011-11-16 04:30:41291 // Cleanup any timed out idle sockets if no timer is used.
292 if (!use_cleanup_timer_)
293 CleanupIdleSockets(false);
294
[email protected]2c2bef152010-10-13 00:55:03295 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03296 num_sockets = max_sockets_per_group_;
297 }
298
299 request.net_log().BeginEvent(
300 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31301 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03302
303 Group* group = GetOrCreateGroup(group_name);
304
[email protected]3c819f522010-12-02 02:03:12305 // RequestSocketsInternal() may delete the group.
306 bool deleted_group = false;
307
[email protected]d7fd1782011-02-08 19:16:43308 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03309 for (int num_iterations_left = num_sockets;
310 group->NumActiveSocketSlots() < num_sockets &&
311 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43312 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03313 if (rv < 0 && rv != ERR_IO_PENDING) {
314 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12315 if (!ContainsKey(group_map_, group_name))
316 deleted_group = true;
317 break;
318 }
319 if (!ContainsKey(group_map_, group_name)) {
320 // Unexpected. The group should only be getting deleted on synchronous
321 // error.
322 NOTREACHED();
323 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03324 break;
325 }
326 }
327
[email protected]3c819f522010-12-02 02:03:12328 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03329 RemoveGroup(group_name);
330
[email protected]d7fd1782011-02-08 19:16:43331 if (rv == ERR_IO_PENDING)
332 rv = OK;
333 request.net_log().EndEventWithNetErrorCode(
334 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03335}
336
[email protected]fd4fe0b2010-02-08 23:02:15337int ClientSocketPoolBaseHelper::RequestSocketInternal(
338 const std::string& group_name,
339 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49340 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03341 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32342 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02343
[email protected]2c2bef152010-10-13 00:55:03344 if (!(request->flags() & NO_IDLE_SOCKETS)) {
345 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10346 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03347 return OK;
348 }
349
[email protected]8159a1c2012-06-07 00:00:10350 // If there are more ConnectJobs than pending requests, don't need to do
351 // anything. Can just wait for the extra job to connect, and then assign it
352 // to the request.
353 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03354 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10355
[email protected]43a21b82010-06-10 21:30:54356 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20357 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
358 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48359 // TODO(willchan): Consider whether or not we need to close a socket in a
360 // higher layered group. I don't think this makes sense since we would just
361 // reuse that socket then if we needed one and wouldn't make it down to this
362 // layer.
[email protected]43a21b82010-06-10 21:30:54363 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31364 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54365 return ERR_IO_PENDING;
366 }
367
[email protected]5acdce12011-03-30 13:00:20368 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48369 // NOTE(mmenke): Wonder if we really need different code for each case
370 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54371 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48372 // There's an idle socket in this pool. Either that's because there's
373 // still one in this group, but we got here due to preconnecting bypassing
374 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46375 bool closed = CloseOneIdleSocketExceptInGroup(group);
376 if (preconnecting && !closed)
377 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54378 } else {
[email protected]40dee91a2012-04-17 19:25:46379 // We could check if we really have a stalled group here, but it requires
380 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31381 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46382 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54383 }
384 }
385
[email protected]51fdc7c2012-04-10 19:19:48386 // We couldn't find a socket to reuse, and there's space to allocate one,
387 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58388 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17389 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02390
[email protected]2ab05b52009-07-01 23:57:58391 int rv = connect_job->Connect();
392 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17393 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03394 if (!preconnecting) {
395 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48396 connect_job->connect_timing(), handle, base::TimeDelta(),
397 group, request->net_log());
[email protected]2c2bef152010-10-13 00:55:03398 } else {
399 AddIdleSocket(connect_job->ReleaseSocket(), group);
400 }
[email protected]2ab05b52009-07-01 23:57:58401 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32402 // If we don't have any sockets in this group, set a timer for potentially
403 // creating a new one. If the SYN is lost, this backup socket may complete
404 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03405 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00406 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32407 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03408 }
[email protected]6b624c62010-03-14 08:37:32409
[email protected]211d2172009-07-22 15:48:53410 connecting_socket_count_++;
411
[email protected]8159a1c2012-06-07 00:00:10412 group->AddJob(connect_job.release(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02413 } else {
[email protected]06650c52010-06-03 00:49:17414 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10415 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52416 if (!preconnecting) {
417 DCHECK(handle);
418 connect_job->GetAdditionalErrorState(handle);
419 error_socket = connect_job->ReleaseSocket();
420 }
[email protected]e772db3f2010-07-12 18:11:13421 if (error_socket) {
[email protected]034df0f32013-01-07 23:17:48422 HandOutSocket(error_socket, false /* not reused */,
423 connect_job->connect_timing(), handle, base::TimeDelta(),
424 group, request->net_log());
[email protected]aed99ef02010-08-26 14:04:32425 } else if (group->IsEmpty()) {
426 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21427 }
[email protected]2ab05b52009-07-01 23:57:58428 }
[email protected]ff579d42009-06-24 15:47:02429
[email protected]2ab05b52009-07-01 23:57:58430 return rv;
[email protected]ff579d42009-06-24 15:47:02431}
432
[email protected]8159a1c2012-06-07 00:00:10433bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32434 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22435 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
436 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
437
438 // Iterate through the idle sockets forwards (oldest to newest)
439 // * Delete any disconnected ones.
440 // * If we find a used idle socket, assign to |idle_socket|. At the end,
441 // the |idle_socket_it| will be set to the newest used idle socket.
442 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
443 it != idle_sockets->end();) {
444 if (!it->socket->IsConnectedAndIdle()) {
445 DecrementIdleCount();
446 delete it->socket;
447 it = idle_sockets->erase(it);
448 continue;
[email protected]eb5a99382010-07-11 03:18:26449 }
[email protected]e1b54dc2010-10-06 21:27:22450
451 if (it->socket->WasEverUsed()) {
452 // We found one we can reuse!
[email protected]e86df8dc2013-03-30 13:18:28453 idle_socket_it = it;
[email protected]e1b54dc2010-10-06 21:27:22454 }
455
456 ++it;
[email protected]eb5a99382010-07-11 03:18:26457 }
[email protected]e1b54dc2010-10-06 21:27:22458
459 // If we haven't found an idle socket, that means there are no used idle
460 // sockets. Pick the oldest (first) idle socket (FIFO).
461
462 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
463 idle_socket_it = idle_sockets->begin();
464
465 if (idle_socket_it != idle_sockets->end()) {
466 DecrementIdleCount();
467 base::TimeDelta idle_time =
468 base::TimeTicks::Now() - idle_socket_it->start_time;
469 IdleSocket idle_socket = *idle_socket_it;
470 idle_sockets->erase(idle_socket_it);
471 HandOutSocket(
472 idle_socket.socket,
473 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48474 LoadTimingInfo::ConnectTiming(),
[email protected]e1b54dc2010-10-06 21:27:22475 request->handle(),
476 idle_time,
477 group,
478 request->net_log());
479 return true;
480 }
481
[email protected]eb5a99382010-07-11 03:18:26482 return false;
483}
484
[email protected]06650c52010-06-03 00:49:17485// static
486void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
487 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31488 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
489 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17490}
491
[email protected]d80a4322009-08-14 07:07:49492void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21493 const std::string& group_name, ClientSocketHandle* handle) {
494 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
495 if (callback_it != pending_callback_map_.end()) {
496 int result = callback_it->second.result;
497 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10498 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21499 if (socket) {
500 if (result != OK)
501 socket->Disconnect();
502 ReleaseSocket(handle->group_name(), socket, handle->id());
503 }
504 return;
505 }
[email protected]b6501d3d2010-06-03 23:53:34506
[email protected]ff579d42009-06-24 15:47:02507 CHECK(ContainsKey(group_map_, group_name));
508
[email protected]aed99ef02010-08-26 14:04:32509 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02510
[email protected]ff579d42009-06-24 15:47:02511 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32512 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
513 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49514 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03515 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31516 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
517 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26518
[email protected]b021ece62013-06-11 11:06:33519 // We let the job run, unless we're at the socket limit and there is
520 // not another request waiting on the job.
521 if (group->jobs().size() > group->pending_requests().size() &&
522 ReachedMaxSocketsLimit()) {
[email protected]aed99ef02010-08-26 14:04:32523 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26524 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34525 }
[email protected]eb5a99382010-07-11 03:18:26526 break;
[email protected]ff579d42009-06-24 15:47:02527 }
528 }
[email protected]ff579d42009-06-24 15:47:02529}
530
[email protected]2abfe90a2010-08-25 17:49:51531bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
532 return ContainsKey(group_map_, group_name);
533}
534
[email protected]d80a4322009-08-14 07:07:49535void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02536 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14537 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02538}
539
[email protected]d80a4322009-08-14 07:07:49540int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02541 const std::string& group_name) const {
542 GroupMap::const_iterator i = group_map_.find(group_name);
543 CHECK(i != group_map_.end());
544
[email protected]aed99ef02010-08-26 14:04:32545 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02546}
547
[email protected]d80a4322009-08-14 07:07:49548LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02549 const std::string& group_name,
550 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21551 if (ContainsKey(pending_callback_map_, handle))
552 return LOAD_STATE_CONNECTING;
553
[email protected]ff579d42009-06-24 15:47:02554 if (!ContainsKey(group_map_, group_name)) {
555 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
556 << " for handle: " << handle;
557 return LOAD_STATE_IDLE;
558 }
559
560 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32561 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02562
[email protected]03b7c8c2013-07-20 04:38:55563 // Search the first group.jobs().size() |pending_requests| for |handle|.
564 // If it's farther back in the deque than that, it doesn't have a
565 // corresponding ConnectJob.
566 size_t connect_jobs = group.jobs().size();
[email protected]aed99ef02010-08-26 14:04:32567 RequestQueue::const_iterator it = group.pending_requests().begin();
[email protected]03b7c8c2013-07-20 04:38:55568 for (size_t i = 0; it != group.pending_requests().end() && i < connect_jobs;
569 ++it, ++i) {
570 if ((*it)->handle() != handle)
571 continue;
572
573 // Just return the state of the farthest along ConnectJob for the first
574 // group.jobs().size() pending requests.
575 LoadState max_state = LOAD_STATE_IDLE;
576 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
577 job_it != group.jobs().end(); ++job_it) {
578 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]ff579d42009-06-24 15:47:02579 }
[email protected]03b7c8c2013-07-20 04:38:55580 return max_state;
[email protected]ff579d42009-06-24 15:47:02581 }
582
[email protected]03b7c8c2013-07-20 04:38:55583 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_))
584 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL;
585 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET;
[email protected]ff579d42009-06-24 15:47:02586}
587
[email protected]10833352013-02-12 19:39:50588base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27589 const std::string& name, const std::string& type) const {
[email protected]10833352013-02-12 19:39:50590 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27591 dict->SetString("name", name);
592 dict->SetString("type", type);
593 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
594 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
595 dict->SetInteger("idle_socket_count", idle_socket_count_);
596 dict->SetInteger("max_socket_count", max_sockets_);
597 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
598 dict->SetInteger("pool_generation_number", pool_generation_number_);
599
600 if (group_map_.empty())
601 return dict;
602
[email protected]10833352013-02-12 19:39:50603 base::DictionaryValue* all_groups_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27604 for (GroupMap::const_iterator it = group_map_.begin();
605 it != group_map_.end(); it++) {
606 const Group* group = it->second;
[email protected]10833352013-02-12 19:39:50607 base::DictionaryValue* group_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27608
609 group_dict->SetInteger("pending_request_count",
610 group->pending_requests().size());
611 if (!group->pending_requests().empty()) {
612 group_dict->SetInteger("top_pending_priority",
613 group->TopPendingPriority());
614 }
615
616 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07617
[email protected]10833352013-02-12 19:39:50618 base::ListValue* idle_socket_list = new base::ListValue();
[email protected]e1b54dc2010-10-06 21:27:22619 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07620 for (idle_socket = group->idle_sockets().begin();
621 idle_socket != group->idle_sockets().end();
622 idle_socket++) {
623 int source_id = idle_socket->socket->NetLog().source().id;
[email protected]10833352013-02-12 19:39:50624 idle_socket_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07625 }
626 group_dict->Set("idle_sockets", idle_socket_list);
627
[email protected]10833352013-02-12 19:39:50628 base::ListValue* connect_jobs_list = new base::ListValue();
[email protected]2c2bef152010-10-13 00:55:03629 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07630 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
631 int source_id = (*job)->net_log().source().id;
[email protected]10833352013-02-12 19:39:50632 connect_jobs_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07633 }
634 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27635
636 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48637 group->IsStalledOnPoolMaxSockets(
638 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00639 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27640
641 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
642 }
643 dict->Set("groups", all_groups_dict);
644 return dict;
645}
646
[email protected]d80a4322009-08-14 07:07:49647bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16648 base::TimeTicks now,
649 base::TimeDelta timeout) const {
650 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01651 if (timed_out)
652 return true;
653 if (socket->WasEverUsed())
654 return !socket->IsConnectedAndIdle();
655 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02656}
657
[email protected]d80a4322009-08-14 07:07:49658void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02659 if (idle_socket_count_ == 0)
660 return;
661
662 // Current time value. Retrieving it once at the function start rather than
663 // inside the inner loop, since it shouldn't change by any meaningful amount.
664 base::TimeTicks now = base::TimeTicks::Now();
665
666 GroupMap::iterator i = group_map_.begin();
667 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32668 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02669
[email protected]e1b54dc2010-10-06 21:27:22670 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32671 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16672 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01673 j->socket->WasEverUsed() ?
674 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16675 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02676 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32677 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02678 DecrementIdleCount();
679 } else {
680 ++j;
681 }
682 }
683
684 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32685 if (group->IsEmpty()) {
686 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02687 } else {
688 ++i;
689 }
690 }
691}
692
[email protected]aed99ef02010-08-26 14:04:32693ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
694 const std::string& group_name) {
695 GroupMap::iterator it = group_map_.find(group_name);
696 if (it != group_map_.end())
697 return it->second;
698 Group* group = new Group;
699 group_map_[group_name] = group;
700 return group;
701}
702
703void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
704 GroupMap::iterator it = group_map_.find(group_name);
705 CHECK(it != group_map_.end());
706
707 RemoveGroup(it);
708}
709
710void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
711 delete it->second;
712 group_map_.erase(it);
713}
714
[email protected]06d94042010-08-25 01:45:22715// static
[email protected]2d6728692011-03-12 01:39:55716bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
717 return g_connect_backup_jobs_enabled;
718}
719
720// static
[email protected]636b8252011-04-08 19:56:54721bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
722 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22723 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54724 return old_value;
[email protected]06d94042010-08-25 01:45:22725}
726
727void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
728 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
729}
730
[email protected]d80a4322009-08-14 07:07:49731void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41732 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
733 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02734}
735
[email protected]d80a4322009-08-14 07:07:49736void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02737 if (--idle_socket_count_ == 0)
738 timer_.Stop();
739}
740
[email protected]64770b7d2011-11-16 04:30:41741// static
742bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
743 return g_cleanup_timer_enabled;
744}
745
746// static
747bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
748 bool old_value = g_cleanup_timer_enabled;
749 g_cleanup_timer_enabled = enabled;
750 return old_value;
751}
752
753void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
754 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
755 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
756}
757
[email protected]eb5a99382010-07-11 03:18:26758void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10759 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26760 int id) {
[email protected]ff579d42009-06-24 15:47:02761 GroupMap::iterator i = group_map_.find(group_name);
762 CHECK(i != group_map_.end());
763
[email protected]aed99ef02010-08-26 14:04:32764 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02765
[email protected]b1f031dd2010-03-02 23:19:33766 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53767 handed_out_socket_count_--;
768
[email protected]aed99ef02010-08-26 14:04:32769 CHECK_GT(group->active_socket_count(), 0);
770 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02771
[email protected]a7e38572010-06-07 18:22:24772 const bool can_reuse = socket->IsConnectedAndIdle() &&
773 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02774 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26775 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01776 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32777 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02778 } else {
779 delete socket;
780 }
[email protected]05ea9ff2010-07-15 19:08:21781
[email protected]eb5a99382010-07-11 03:18:26782 CheckForStalledSocketGroups();
783}
[email protected]ff579d42009-06-24 15:47:02784
[email protected]eb5a99382010-07-11 03:18:26785void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
786 // If we have idle sockets, see if we can give one to the top-stalled group.
787 std::string top_group_name;
788 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21789 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26790 return;
[email protected]4f2abec2010-02-03 18:10:16791
[email protected]eb5a99382010-07-11 03:18:26792 if (ReachedMaxSocketsLimit()) {
793 if (idle_socket_count() > 0) {
794 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54795 } else {
[email protected]eb5a99382010-07-11 03:18:26796 // We can't activate more sockets since we're already at our global
797 // limit.
[email protected]4f2abec2010-02-03 18:10:16798 return;
[email protected]d7027bb2010-05-10 18:58:54799 }
[email protected]4f2abec2010-02-03 18:10:16800 }
[email protected]eb5a99382010-07-11 03:18:26801
802 // Note: we don't loop on waking stalled groups. If the stalled group is at
803 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21804 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26805 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21806 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02807}
808
[email protected]211d2172009-07-22 15:48:53809// Search for the highest priority pending request, amongst the groups that
810// are not at the |max_sockets_per_group_| limit. Note: for requests with
811// the same priority, the winner is based on group hash ordering (and not
812// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48813bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
814 Group** group,
815 std::string* group_name) const {
816 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53817 Group* top_group = NULL;
818 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21819 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48820 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53821 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32822 Group* curr_group = i->second;
823 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53824 if (queue.empty())
825 continue;
[email protected]51fdc7c2012-04-10 19:19:48826 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
827 if (!group)
828 return true;
[email protected]05ea9ff2010-07-15 19:08:21829 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41830 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05831 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41832 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32833 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41834 top_group_name = &i->first;
835 }
[email protected]211d2172009-07-22 15:48:53836 }
837 }
[email protected]05ea9ff2010-07-15 19:08:21838
[email protected]211d2172009-07-22 15:48:53839 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48840 CHECK(group);
[email protected]211d2172009-07-22 15:48:53841 *group = top_group;
842 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48843 } else {
844 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53845 }
[email protected]05ea9ff2010-07-15 19:08:21846 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53847}
848
[email protected]d80a4322009-08-14 07:07:49849void ClientSocketPoolBaseHelper::OnConnectJobComplete(
850 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58851 DCHECK_NE(ERR_IO_PENDING, result);
852 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02853 GroupMap::iterator group_it = group_map_.find(group_name);
854 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32855 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02856
[email protected]3268023f2011-05-05 00:08:10857 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02858
[email protected]034df0f32013-01-07 23:17:48859 // Copies of these are needed because |job| may be deleted before they are
860 // accessed.
[email protected]9e743cd2010-03-16 07:03:53861 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48862 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57863
[email protected]4d3b05d2010-01-27 21:27:29864 if (result == OK) {
865 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32866 RemoveConnectJob(job, group);
867 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29868 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02869 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17870 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29871 HandOutSocket(
[email protected]034df0f32013-01-07 23:17:48872 socket.release(), false /* unused socket */, connect_timing,
873 r->handle(), base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31874 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21875 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57876 } else {
[email protected]0f873e82010-09-02 16:09:01877 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32878 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21879 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57880 }
[email protected]94c20472010-01-14 08:14:36881 } else {
[email protected]e772db3f2010-07-12 18:11:13882 // If we got a socket, it must contain error information so pass that
883 // up so that the caller can retrieve it.
884 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32885 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29886 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02887 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17888 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18889 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32890 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13891 if (socket.get()) {
892 handed_out_socket = true;
[email protected]034df0f32013-01-07 23:17:48893 HandOutSocket(socket.release(), false /* unused socket */,
894 connect_timing, r->handle(), base::TimeDelta(), group,
895 r->net_log());
[email protected]e772db3f2010-07-12 18:11:13896 }
[email protected]034df0f32013-01-07 23:17:48897 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result);
[email protected]05ea9ff2010-07-15 19:08:21898 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18899 } else {
[email protected]aed99ef02010-08-26 14:04:32900 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29901 }
[email protected]05ea9ff2010-07-15 19:08:21902 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32903 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21904 CheckForStalledSocketGroups();
905 }
[email protected]ff579d42009-06-24 15:47:02906 }
[email protected]ff579d42009-06-24 15:47:02907}
908
[email protected]66761b952010-06-25 21:30:38909void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42910 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38911}
912
[email protected]7af985a2012-12-14 22:40:42913void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24914 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14915 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52916 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42917 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52918}
919
[email protected]51fdc7c2012-04-10 19:19:48920bool ClientSocketPoolBaseHelper::IsStalled() const {
921 // If we are not using |max_sockets_|, then clearly we are not stalled
922 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
923 return false;
924 // So in order to be stalled we need to be using |max_sockets_| AND
925 // we need to have a request that is actually stalled on the global
926 // socket limit. To find such a request, we look for a group that
927 // a has more requests that jobs AND where the number of jobs is less
928 // than |max_sockets_per_group_|. (If the number of jobs is equal to
929 // |max_sockets_per_group_|, then the request is stalled on the group,
930 // which does not count.)
931 for (GroupMap::const_iterator it = group_map_.begin();
932 it != group_map_.end(); ++it) {
933 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
934 return true;
935 }
936 return false;
937}
938
[email protected]2c2bef152010-10-13 00:55:03939void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29940 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33941 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53942 connecting_socket_count_--;
943
[email protected]25eea382010-07-10 23:55:26944 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32945 DCHECK(ContainsKey(group->jobs(), job));
946 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26947
948 // If we've got no more jobs for this group, then we no longer need a
949 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32950 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00951 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26952
[email protected]8ae03f42010-07-07 19:08:10953 DCHECK(job);
954 delete job;
[email protected]2ab05b52009-07-01 23:57:58955}
[email protected]ff579d42009-06-24 15:47:02956
[email protected]8ae03f42010-07-07 19:08:10957void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21958 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51959 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21960 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32961 RemoveGroup(group_name);
962 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51963 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10964}
[email protected]06650c52010-06-03 00:49:17965
[email protected]8ae03f42010-07-07 19:08:10966void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21967 const std::string& group_name, Group* group) {
968 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32969 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21970 if (rv != ERR_IO_PENDING) {
971 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02972 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51973 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32974 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10975
[email protected]d7fd1782011-02-08 19:16:43976 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41977 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58978 }
979}
980
[email protected]d80a4322009-08-14 07:07:49981void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10982 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58983 bool reused,
[email protected]034df0f32013-01-07 23:17:48984 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:58985 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29986 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15987 Group* group,
[email protected]9e743cd2010-03-16 07:03:53988 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58989 DCHECK(socket);
990 handle->set_socket(socket);
991 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29992 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24993 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:48994 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:53995
[email protected]d13f51b2010-04-27 23:20:45996 if (reused) {
[email protected]ec11be62010-04-28 19:28:09997 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45998 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:31999 NetLog::IntegerCallback(
1000 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:151001 }
[email protected]d13f51b2010-04-27 23:20:451002
[email protected]06650c52010-06-03 00:49:171003 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311004 socket->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:151005
[email protected]211d2172009-07-22 15:48:531006 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321007 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021008}
1009
[email protected]d80a4322009-08-14 07:07:491010void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:101011 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:571012 DCHECK(socket);
1013 IdleSocket idle_socket;
1014 idle_socket.socket = socket;
1015 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571016
[email protected]aed99ef02010-08-26 14:04:321017 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571018 IncrementIdleCount();
1019}
1020
[email protected]d80a4322009-08-14 07:07:491021void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541022 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321023 Group* group = i->second;
1024 connecting_socket_count_ -= group->jobs().size();
1025 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321026
[email protected]5fc08e32009-07-15 17:09:571027 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321028 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141029 // RemoveGroup() will call .erase() which will invalidate the iterator,
1030 // but i will already have been incremented to a valid iterator before
1031 // RemoveGroup() is called.
1032 RemoveGroup(i++);
1033 } else {
1034 ++i;
1035 }
1036 }
1037 DCHECK_EQ(0, connecting_socket_count_);
1038}
1039
[email protected]7af985a2012-12-14 22:40:421040void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141041 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1042 Group* group = i->second;
1043
1044 RequestQueue pending_requests;
1045 pending_requests.swap(*group->mutable_pending_requests());
1046 for (RequestQueue::iterator it2 = pending_requests.begin();
1047 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031048 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141049 InvokeUserCallbackLater(
[email protected]7af985a2012-12-14 22:40:421050 request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141051 }
1052
1053 // Delete group if no longer needed.
1054 if (group->IsEmpty()) {
1055 // RemoveGroup() will call .erase() which will invalidate the iterator,
1056 // but i will already have been incremented to a valid iterator before
1057 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321058 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541059 } else {
1060 ++i;
[email protected]5fc08e32009-07-15 17:09:571061 }
1062 }
1063}
1064
[email protected]d80a4322009-08-14 07:07:491065bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531066 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541067 int total = handed_out_socket_count_ + connecting_socket_count_ +
1068 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201069 // There can be more sockets than the limit since some requests can ignore
1070 // the limit
[email protected]c901f6d2010-04-27 17:48:281071 if (total < max_sockets_)
1072 return false;
[email protected]c901f6d2010-04-27 17:48:281073 return true;
[email protected]211d2172009-07-22 15:48:531074}
1075
[email protected]51fdc7c2012-04-10 19:19:481076bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1077 if (idle_socket_count() == 0)
1078 return false;
1079 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461080}
1081
1082bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1083 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541084 CHECK_GT(idle_socket_count(), 0);
1085
1086 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321087 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461088 if (exception_group == group)
1089 continue;
[email protected]e1b54dc2010-10-06 21:27:221090 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541091
[email protected]e1b54dc2010-10-06 21:27:221092 if (!idle_sockets->empty()) {
1093 delete idle_sockets->front().socket;
1094 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541095 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321096 if (group->IsEmpty())
1097 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541098
[email protected]dcbe168a2010-12-02 03:14:461099 return true;
[email protected]43a21b82010-06-10 21:30:541100 }
1101 }
1102
[email protected]51fdc7c2012-04-10 19:19:481103 return false;
1104}
[email protected]dcbe168a2010-12-02 03:14:461105
[email protected]51fdc7c2012-04-10 19:19:481106bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1107 // This pool doesn't have any idle sockets. It's possible that a pool at a
1108 // higher layer is holding one of this sockets active, but it's actually idle.
1109 // Query the higher layers.
1110 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1111 it != higher_layer_pools_.end(); ++it) {
1112 if ((*it)->CloseOneIdleConnection())
1113 return true;
1114 }
[email protected]dcbe168a2010-12-02 03:14:461115 return false;
[email protected]43a21b82010-06-10 21:30:541116}
1117
[email protected]05ea9ff2010-07-15 19:08:211118void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411119 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211120 CHECK(!ContainsKey(pending_callback_map_, handle));
1121 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
[email protected]2da659e2013-05-23 20:51:341122 base::MessageLoop::current()->PostTask(
[email protected]05ea9ff2010-07-15 19:08:211123 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411124 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1125 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211126}
1127
1128void ClientSocketPoolBaseHelper::InvokeUserCallback(
1129 ClientSocketHandle* handle) {
1130 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1131
1132 // Exit if the request has already been cancelled.
1133 if (it == pending_callback_map_.end())
1134 return;
1135
1136 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411137 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211138 int result = it->second.result;
1139 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411140 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211141}
1142
[email protected]58e562f2013-04-22 17:32:201143void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() {
1144 while (IsStalled()) {
1145 // Closing a socket will result in calling back into |this| to use the freed
1146 // socket slot, so nothing else is needed.
1147 if (!CloseOneIdleConnectionInLayeredPool())
1148 return;
1149 }
1150}
1151
[email protected]aed99ef02010-08-26 14:04:321152ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101153 : unassigned_job_count_(0),
1154 active_socket_count_(0),
[email protected]aa249b52013-04-30 01:04:321155 weak_factory_(this) {}
[email protected]aed99ef02010-08-26 14:04:321156
[email protected]e4d9a9722011-05-12 00:16:001157ClientSocketPoolBaseHelper::Group::~Group() {
1158 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101159 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321160}
1161
1162void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1163 const std::string& group_name,
1164 ClientSocketPoolBaseHelper* pool) {
1165 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411166 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321167 return;
1168
[email protected]2da659e2013-05-23 20:51:341169 base::MessageLoop::current()->PostDelayedTask(
[email protected]aed99ef02010-08-26 14:04:321170 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411171 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1172 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001173 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321174}
1175
[email protected]8159a1c2012-06-07 00:00:101176bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1177 SanityCheck();
1178
1179 if (unassigned_job_count_ == 0)
1180 return false;
1181 --unassigned_job_count_;
1182 return true;
1183}
1184
1185void ClientSocketPoolBaseHelper::Group::AddJob(ConnectJob* job,
1186 bool is_preconnect) {
1187 SanityCheck();
1188
1189 if (is_preconnect)
1190 ++unassigned_job_count_;
1191 jobs_.insert(job);
1192}
1193
1194void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
1195 SanityCheck();
1196
1197 jobs_.erase(job);
1198 size_t job_count = jobs_.size();
1199 if (job_count < unassigned_job_count_)
1200 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031201}
1202
[email protected]aed99ef02010-08-26 14:04:321203void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1204 std::string group_name,
1205 ClientSocketPoolBaseHelper* pool) {
1206 // If there are no more jobs pending, there is no work to do.
1207 // If we've done our cleanups correctly, this should not happen.
1208 if (jobs_.empty()) {
1209 NOTREACHED();
1210 return;
1211 }
1212
[email protected]8159a1c2012-06-07 00:00:101213 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321214 // right now due to limits, just reset the timer.
1215 if (pool->ReachedMaxSocketsLimit() ||
1216 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1217 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1218 StartBackupSocketTimer(group_name, pool);
1219 return;
1220 }
1221
[email protected]a9fc8fc2011-05-10 02:41:071222 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441223 return;
[email protected]4baaf9d2010-08-31 15:15:441224
[email protected]aed99ef02010-08-26 14:04:321225 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1226 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311227 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321228 SIMPLE_STATS_COUNTER("socket.backup_created");
1229 int rv = backup_job->Connect();
1230 pool->connecting_socket_count_++;
[email protected]8159a1c2012-06-07 00:00:101231 AddJob(backup_job, false);
[email protected]aed99ef02010-08-26 14:04:321232 if (rv != ERR_IO_PENDING)
1233 pool->OnConnectJobComplete(rv, backup_job);
1234}
1235
[email protected]8159a1c2012-06-07 00:00:101236void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1237 DCHECK_LE(unassigned_job_count_, jobs_.size());
1238}
1239
[email protected]aed99ef02010-08-26 14:04:321240void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101241 SanityCheck();
1242
[email protected]aed99ef02010-08-26 14:04:321243 // Delete active jobs.
1244 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101245 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321246
1247 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411248 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321249}
1250
[email protected]d80a4322009-08-14 07:07:491251} // namespace internal
1252
[email protected]ff579d42009-06-24 15:47:021253} // namespace net